Google Cloud Functions에서 Firestore apps를 초기화하려고 하면 ValueError가 됩니다.
어려움
Google Cloud Functions에서 데이터를 검색하여 Firestore에 저장하는 프로세스를 만들고,
Cloud Scheduler에서 몇 분 정도 작동합니다.
로컬 환경에서도 클라우드에서의 실행 테스트도 OK이었지만, 막상 스케줄 실행하면,
다음과 같이 실행 결과의 상태가 'ok'와 'crash'를 반복하게 되었습니다.

환경
원래 구현
공식 해설 대로 구현
이렇게 기재하는 것만으로 같은 계정 내에서 Firestore에 액세스 할 수있는 것 같습니다. 쉽게 할 수 있었다!
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
# Use the application default credentials
cred = credentials.ApplicationDefault()
firebase_admin.initialize_app(cred, {
'projectId': project_id,
})
db = firestore.client()
그러나 막상 스케줄을 타면 서두의 현상에. .
에러의 내용을 조사해 보면, 이하의 ValueError가 토해지고 있었습니다.
ValueError: The default Firebase app already exists.
This means you called initialize_app() more than once without providing an app name as the second argument.
initialize_app()를 두 번 부르지 말고 화가 난 것 같다.
코드를 재검토해 보았지만, 한번의 실행 중에는 한번밖에 부르지 않을 것이다. . .
해결된 구현
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
if not firebase_admin._apps:
cred = credentials.ApplicationDefault()
firebase_admin.initialize_app(cred, {
'projectId': project_id,
})
db = firestore.client()
stack overflow 에 같은 오류로 곤란한 사람이 있었으므로, 그것을 참고로.
간단하지만 이미 응용 프로그램을 초기화했는지 확인하는 if 문을 넣어 해결했습니다.
Cloud Functions와 Cloud Scheduler의 움직임을 잘 모르겠지만,
시간을 비우지 않고 실행하면 이전 실행에서 생성된 인스턴스를 재사용하는 움직임일까.
로컬에서는 연속으로 실행해도 아무 문제도 없었기 때문에, 빠져 버렸습니다.
Reference
이 문제에 관하여(Google Cloud Functions에서 Firestore apps를 초기화하려고 하면 ValueError가 됩니다.), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/yotsu_t/items/5ab72ecfc84dec94108f텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)