出于某些工作目的,我需要在两个不同Firestore实例的两个集合之间重定向数据。为此,我创建了一个回调,并将其作为参数传递给collection.on_snapshot方法。我无法显示整个函数,但它与下面的代码非常接近,至于如何运行它,我遵循了google exemple
# Create a callback on_snapshot function to capture changes
def redirect_data(col_snapshot, changes, read_time):
for change in changes:
# Document update
if change.type.name == 'MODIFIED':
ts = datetime.datetime.now().astimezone()
document_data = change.document.to_dict()
document_id = change.document.id
document_data['updated_at'] = ts
for k, v in document_data.items():
data_id = document_id + k
data_to_add = {'updated_at': ts,
'is_active': v['is_active']}
logging.info('Updating slot : {0}'.format(data_id))
try :
# Updating data to another project
FIRESTORE_DATABASE.collection(COLLECTION) \
.document(data_id) \
.update(data_to_add)
except:
# If the document does not exist we create it without
# forgetting to add the slot type
DB.collection(COLLECTION) \
.document(data_id) \
.set(data_to_add)
callback_done.set()
当我运行代码时,一切都正常,至少一开始是这样。但是,经过一段时间(可能是几个小时或几天)后,处理此集合快照的线程将退出并不再启动
以下是一些日志:
2021-06-11 15:28:49,102| INFO | root| redirect_data()| L89 | Updating slot : slot_00002| call_trace=path_to_python_file/script.py| L89
2021-06-11 15:28:49,161| INFO | root| redirect_data()| L89 | Updating slot : slot_00004| call_trace=path_to_python_file/script.py| L89
2021-06-11 15:28:50,307| INFO | google.api_core.bidi| _thread_main()| L676 | Thread-ConsumeBidirectionalStream exiting| call_trace=path_to_venv/venv/lib/python3.7/site-packages/google/api_core/bidi.py| L676
2021-06-11 16:03:35,309| INFO | google.api_core.bidi| _reopen()| L487 | Re-established stream| call_trace=path_to_venv/venv/lib/python3.7/site-packages/google/api_core/bidi.py| L487
2021-06-11 16:09:45,092| INFO | google.api_core.bidi| _reopen()| L487 | Re-established stream| call_trace=path_to_venv/venv/lib/python3.7/site-packages/google/api_core/bidi.py| L487
2021-06-11 16:09:45,190| INFO | google.api_core.bidi| _reopen()| L487 | Re-established stream| call_trace=path_to_venv/venv/lib/python3.7/site-packages/google/api_core/bidi.py| L487
在这个事件之后,我看到的只是一些试图重新打开线程的失败尝试
2021-06-11 16:27:25,161| INFO | google.api_core.bidi| _reopen()| L487 | Re-established stream| call_trace=path_to_venv/venv/lib/python3.7/site-packages/google/api_core/bidi.py| L487
2021-06-11 16:27:25,235| INFO | google.api_core.bidi| _reopen()| L487 | Re-established stream| call_trace=path_to_venv/venv/lib/python3.7/site-packages/google/api_core/bidi.py| L487
2021-06-11 17:03:35,433| INFO | google.api_core.bidi| _reopen()| L487 | Re-established stream| call_trace=path_to_venv/venv/lib/python3.7/site-packages/google/api_core/bidi.py| L487
2021-06-11 17:03:35,507| INFO | google.api_core.bidi| _reopen()| L487 | Re-established stream| call_trace=path_to_venv/venv/lib/python3.7/site-packages/google/api_core/bidi.py| L487
2021-06-11 17:09:45,731| INFO | google.api_core.bidi| _reopen()| L487 | Re-established stream| call_trace=path_to_venv/venv/lib/python3.7/site-packages/google/api_core/bidi.py| L487
2021-06-11 17:09:45,810| INFO | google.api_core.bidi| _reopen()| L487 | Re-established stream| call_trace=path_to_venv/venv/lib/python3.7/site-packages/google/api_core/bidi.py| L487
我很难理解是什么导致了这种行为,因为它没有固定的模式,我无法获得更详细的日志
此脚本通过sudo nohup ... &
命令在使用Debian GNU/Linux 10的1CPU/2GB虚拟机上运行。
我使用python 3.7.3,以下是我的venv规范:
Package Version
------------------------ ---------
CacheControl 0.12.6
cachetools 4.2.0
certifi 2020.12.5
cffi 1.14.4
chardet 4.0.0
firebase-admin 4.5.0
google-api-core 1.24.1
google-api-python-client 1.12.8
google-auth 1.24.0
google-auth-httplib2 0.0.4
google-cloud-core 1.5.0
google-cloud-firestore 2.0.2
google-cloud-storage 1.35.0
google-crc32c 1.1.0
google-resumable-media 1.2.0
googleapis-common-protos 1.52.0
grpcio 1.34.0
httplib2 0.18.1
idna 2.10
msgpack 1.0.2
pip 21.1.2
pkg-resources 0.0.0
proto-plus 1.13.0
protobuf 3.14.0
pyasn1 0.4.8
pyasn1-modules 0.2.8
pycparser 2.20
python-dotenv 0.17.1
pytz 2020.5
requests 2.25.1
rsa 4.7
setuptools 40.8.0
six 1.15.0
uritemplate 3.0.1
urllib3 1.26.2
更新
我有一个开发环境和一个生产环境,所以脚本要运行两次,每个实例都在一个特定的VM上。 当我在生产环境中ram脚本时,问题开始出现。 多谢各位
目前没有回答
相关问题 更多 >
编程相关推荐