【问题标题】:Python Firestore insert return error 503 DNS resolution failedPython Firestore 插入返回错误 503 DNS 解析失败
【发布时间】:2021-01-20 08:33:51
【问题描述】:

我在从 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 执行它时,它返回以下错误:

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

我真的不明白问题出在哪里,因为每次脚本以两种方式启动时,数据库的连接都会正常工作。

这类问题有解决办法吗?

【问题讨论】:

  • 我认为可能有几个原因...这仅适用于set 还是get 有效?你能准备最小的例子吗?这是整个错误堆栈吗?
  • 好吧,我插入日志错误文件的另一部分。实际上,firestore 连接正在工作,当它从 crontab 启动时也是如此。请给我一份发生这种情况的原因列表,好吗?

标签: python-3.x google-cloud-firestore debian


【解决方案1】:

我发现了一些可能有用的东西。有很好的troubleshooting guide 并且那里有一部分,这似乎是相关的:

如果您的命令通过调用像 python some-command.py 这样的运行时起作用,请执行一些检查以确定运行时 版本和环境是正确的。每个语言运行时都有怪癖 这可能会导致 crontab 下出现意外行为。

对于 python,您可能会发现您的网络应用程序正在使用虚拟 您需要在 crontab 中调用的环境。

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

【讨论】:

    【解决方案2】:

    我找到了解决方案。
    出现问题是因为超时 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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-03
      • 2020-09-18
      • 2020-05-19
      • 2020-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多