2

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

Guanaco Devs
  • 1,822
  • 2
  • 21
  • 38
  • This pattern should work, is it possible that you have an error elsewhere? Can you include your `main.py` and `tools.py`? Are you able to run this locally and verify that it workrs? You can run it locally with the Functions Framework: https://pypi.org/project/functions-framework/ – Dustin Ingram May 07 '20 at 00:40
  • @DustinIngram I have added `main.py` and `tools.py`. I have not tried locally, I do not get errors on deployment but the function crashes when triggered without any `log`. `vscode` does give me a warning about the import. `__init__.py` is empty. – Guanaco Devs May 07 '20 at 01:00
  • @DustinIngram I just tried the `functions-framework` and it works as expected, although I just modified my scripts to check for the class being imported appropriately – Guanaco Devs May 07 '20 at 01:22
  • What makes you think the import is failing? If it deploys successfully, but fails when it is triggered, that indicates that something in the function itself is causing the crash. When you say "crashes with no logs", what do you see that indicates a crash? – Dustin Ingram May 07 '20 at 16:29
  • @DustinIngram I really don't know is the import is the one that is failing. But the log shows only that `Function execution started` and then in the next line `Function execution took 1168 ms, finished with status: 'crash'` with no other info. I've put some print statements and they don't show up, not even after the `entry-point`, I have one right after the `user_clean_up` definition. – Guanaco Devs May 07 '20 at 20:29
  • @DustinIngram the issue was that I did not added a `f` format to the `databaseURL` when initializing firebase. But I was able to debug that only with a `try/catch`, the logs only shows `... finished with status: 'crash'` – Guanaco Devs Jun 07 '20 at 15:42
  • 1
    This is a known issue that's in the process of being fixed: https://issuetracker.google.com/issues/155215191 – Dustin Ingram Jun 07 '20 at 19:57

0 Answers0