首页 > 解决方案 > Firebase 更新查询被随机中断

问题描述

我们将数据存储在 Cloud Firestore 中。我们正在尝试为根目录下的每个孩子更新一列。但是,更新会随机中断。有时代码会更新 50 个孩子,然后更新 100 个。没有一致性。当我们从它离开的地方再次运行更新时,错误的孩子被更新了。

我们必须保留一Updated列,因为我们不知道更新了哪些孩子。这是应用在调试模式下运行时的错误:

Firebase update() id:DW7OUeam1SAOPkZiWa8pdict:{'sil': 'Sil'}
Traceback (most recent call last):
  File "D:\mysource\test\venv\lib\site-packages\google\api_core\grpc_helpers.py", line 57, in error_remapped_callable
    return callable_(*args, **kwargs)
  File "D:\mysource\test\venv\lib\site-packages\grpc\_channel.py", line 826, in __call__
    return _end_unary_response_blocking(state, call, False, None)
  File "D:\mysource\test\venv\lib\site-packages\grpc\_channel.py", line 729, in _end_unary_response_blocking
    raise _InactiveRpcError(state)
grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with:
status = StatusCode.UNKNOWN
details = "Stream removed"
debug_error_string = "{"created":"@1594884770.722000000","description":"Error received from peer ipv4:*****:443","file":"src/core/lib/surface/call.cc","file_line":1056,"grpc_message":"Stream removed","grpc_status":2}"
>

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

Traceback (most recent call last):
  File "D:/mysource/test/src/arbibot/firestore/ArbotDbUtility.py", line 131, in <module>
    arbotDbUtility.add_qty_to_cross_trades()
  File "D:/mysource/test/src/arbibot/firestore/ArbotDbUtility.py", line 108, in add_qty_to_cross_trades
    self.firebase.update('cross_trades', trade['id'], cross_trade_dto)
  File "D:\mysource\test\src\arbibot\firestore\Firebase.py", line 50, in update
    doc_ref.update(dict)
  File "D:\mysource\test\venv\lib\site-packages\google\cloud\firestore_v1\document.py", line 382, in update
    write_results = batch.commit()
  File "D:\mysource\test\venv\lib\site-packages\google\cloud\firestore_v1\batch.py", line 147, in commit
    metadata=self._client._rpc_metadata,
  File "D:\mysource\test\venv\lib\site-packages\google\cloud\firestore_v1\gapic\firestore_client.py", line 1033, in commit
    request, retry=retry, timeout=timeout, metadata=metadata
  File "D:\mysource\test\venv\lib\site-packages\google\api_core\gapic_v1\method.py", line 143, in __call__
    return wrapped_func(*args, **kwargs)
  File "D:\mysource\test\venv\lib\site-packages\google\api_core\retry.py", line 286, in retry_wrapped_func
    on_error=on_error,
  File "D:\mysource\test\venv\lib\site-packages\google\api_core\retry.py", line 184, in retry_target
    return target()
  File "D:\mysource\test\venv\lib\site-packages\google\api_core\timeout.py", line 214, in func_with_timeout
    return func(*args, **kwargs)
  File "D:\mysource\test\venv\lib\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.Unknown: None Stream removed

Process finished with exit code 1

我们如何才能不中断地更新每个孩子?

编辑:

这是我们用来更新的代码。该sleep()代码用于预防。我们认为更新太多记录时 Firebase 可能会阻止我们。我们不确定这是否有必要。

def change_process_status(self):
    path_ref = self.firebase.db.collection('processes')

    docs = path_ref.stream()
    for process_doc in docs:
        process_dto = process_doc.to_dict()
        process_dto['status'] = 'COMPLETED'
        self.firebase.update('processes', process_doc.id, process_dto)
        time.sleep(0.2) 

标签: pythonfirebasegoogle-cloud-firestore

解决方案


推荐阅读