I have a few google-cloud-functions
that has common code between them.
So I chose to make a package and place it on each of the folders for my functions.
ending with the following directory structure
google-cloud-function
├── main.py
├── requirements.txt
└── helpers
├── tools.py
└── __init__.py
I followed Packaging Local Dependencies from the docs to make my package helpers
.
With my requirements.txt
as follow:
firebase-admin==4.0.0
google-cloud-storage==1.26.0
google-cloud-firestore==1.6.2
The function deploys properly but when triggered crashes with no logs.
This is the import statement.
from helpers.tools import Event, CleanUpUser
How can I deploy this python
google-cloud-function
appropiately?
EDIT:
main.py
import firebase_admin
from google.cloud import firestore
from helpers.tools import Event
firebase_admin.initialize_app(options={'databaseURL': 'https://database_name.firebaseio.com/'})
firestore_db = firestore.Client()
EVENTS_NAME = 'database-events'
def user_cleanup(data, context):
if Event(EVENTS_NAME).check_event(context):
return 'Done'
user_id = data['uid']
user_ref = firestore_db.document(f'user_data/{user_id}')
return 'Done'
tools.py
import logging
import sys
import traceback
from datetime import datetime
import firebase_admin
import pytz
from google.cloud import firestore
class Event:
def __init__(self, database_name: str):
init_logger()
firebase_admin.initialize_app(options={'databaseURL': 'https://{database_name}.firebaseio.com/'})
def check_event(self, context):
event_id = context.event_id.replace('/', '--')
timestamp = context.timestamp
timestamp = timestamp[0:23] if len(timestamp) >= 24 else timestamp[0:19]
try:
millis = datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S.%f').replace(tzinfo=pytz.utc).timestamp()*1000
except ValueError:
millis = datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S').replace(tzinfo=pytz.utc).timestamp()*1000
event_ref = firebase_admin.db.reference().child(event_id)
event = event_ref.get()
if not event:
event_ref.set({
'timestamp': millis
})
return False
else:
logging.warning('Event already in the list, exiting this instance.')
return True
EDIT: My issue is that I wasn't formatting the databaseURL
by prepending an f
, still I was able to debug that using a try/catch
the logs never showed the cause of the error only ...finished with status: 'crash'
, opened an issue in the GitHub