Python Firestore 插入 return 错误 503 DNS 解析失败

Python Firestore insert return error 503 DNS resolution failed


我在 crontab 执行我的 python 脚本时遇到问题,该脚本包含 firestore 数据库中的插入操作。
db.collection(u'ab').document(str(row["Name"])).collection(str(row["id"])).document(str(row2["id"])).set(self.packStructure(row2))

当我用 python3 script.py 命令正常执行时它工作,但是当我从 crontab 执行它时它 return 下面错误:

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/axatel/angel_bridge/esportazione_firebase/main.py", line 23, in <module>
    dato.getDati(dato, db, cursor, cursor2, fdb, select, anagrafica)
  File "/home/axatel/angel_bridge/esportazione_firebase/dati.py", line 19, in getDati 
db.collection(u'ab').document(str(row["Name"])).collection(str(row["id"])).document(str(row2["id"])).set(self.packStructure(row2))
  File "/home/axatel/.local/lib/python3.7/site-packages/google/cloud/firestore_v1/document.py", line 234, in set
    write_results = batch.commit()
  File "/home/axatel/.local/lib/python3.7/site-packages/google/cloud/firestore_v1/batch.py", line 147, in commit
    metadata=self._client._rpc_metadata,
  File "/home/axatel/.local/lib/python3.7/site-packages/google/cloud/firestore_v1/gapic/firestore_client.py", line 1121, in commit
    request, retry=retry, timeout=timeout, metadata=metadata
  File "/home/axatel/.local/lib/python3.7/site-packages/google/api_core/gapic_v1/method.py", line 145, in __call__
    return wrapped_func(*args, **kwargs)
  File "/home/axatel/.local/lib/python3.7/site-packages/google/api_core/retry.py", line 286, in retry_wrapped_func
    on_error=on_error,
  File "/home/axatel/.local/lib/python3.7/site-packages/google/api_core/retry.py", line 184, in retry_target
    return target()
  File "/home/axatel/.local/lib/python3.7/site-packages/google/api_core/timeout.py", line 214, in func_with_timeout
    return func(*args, **kwargs)
  File "/home/axatel/.local/lib/python3.7/site-packages/google/api_core/grpc_helpers.py", line 59, in error_remapped_callable
    six.raise_from(exceptions.from_grpc_error(exc), exc)
  File "<string>", line 3, in raise_from
google.api_core.exceptions.ServiceUnavailable: 503 DNS resolution failed for service: firestore.googleapis.com:443

我真的不明白这是什么问题,因为每次以两种方式启动脚本时,数据库的连接都有效。

是否有解决此类问题的方法?

我找到了一些可能有用的东西。有nicetroubleshooting guide还有一段好像有关系:

If your command works by invoking a runtime like python some-command.py perform a few checks to determine that the runtime version and environment is correct. Each language runtime has quirks that can cause unexpected behavior under crontab.

For python you might find that your web app is using a virtual environment you need to invoke in your crontab.

我没有看到这样的错误 运行 Firestore API,但这似乎与您的问题相符。

我找到了解决方案。
出现问题是因为超时 sleep() 值低于预期,因此数据库连接功能在机器启动阶段过早启动。将此值增加到 45 或 60 秒可解决问题。

#time.sleep(10) # old version
time.sleep(60) # working version

fdb = firebaseConnection()

def firebaseConnection():
    # firebase connection
    cred = credentials.Certificate('/database/axatel.json')
    firebase_admin.initialize_app(cred)
    fdb = firestore.client()
    if fdb:
        return fdb
    else:
        print("Error")
        sys.exit()