1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Adding Node to Python Firebase Function

Discussão em 'Python' iniciado por Stack, Abril 17, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to deploy a firebase function in python. In the main.py file I define a basic Flask server, this main.py imports fetch.py (another file -same directory- with my source code). The fetch.py file uses the python javascript package. I added javascript to requirements.txt too. This package needs Node.js installed in the environment. Is there any simple way I can add Node.js?

    I saw these Docs but I feel like its not what I need because I am not calling any Node/JS code, rather Node just needs to be setup in the same environment. Calling this deploy command

    gcloud functions deploy fetchPython \
    --gen2 \
    --runtime=python311 \
    --region=us-central1 \
    --entry-point=app \
    --trigger-http \
    --allow-unauthenticated


    results in this


    ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Could not create or update Cloud Run service fetchpython, Container Healthcheck failed. Revision 'fetchpython-00003-muh' is not ready and cannot serve traffic. The user-provided container failed to start and listen on the port defined provided by the PORT=8080 environment variable within the allocated timeout. This can happen when the container port is misconfigured or if the timeout is too short. The health check timeout can be extended. Logs for this revision might contain more information.
    ​

    running gcloud functions logs read fetchPython --region=us-central1 I get:

    LEVEL NAME EXECUTION_ID TIME_UTC LOG
    E fetchpython 2025-04-17 21:54:58.739 Default STARTUP TCP probe failed 1 time consecutively for container "worker" on port 8080. The instance was not started.
    Connection failed with status CANCELLED.
    fetchpython 2025-04-17 21:54:58.649 Exception: Timed out accessing 'console'
    WARNING fetchpython 2025-04-17 21:54:58.649 Container called exit(1).
    fetchpython 2025-04-17 21:54:58.345 raise Exception(f"Timed out accessing '{attr}'")
    fetchpython 2025-04-17 21:54:58.345 File "/layers/google.python.pip/pip/lib/python3.11/site-packages/javascript/proxy.py", line 43, in ipc
    fetchpython 2025-04-17 21:54:58.345 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    fetchpython 2025-04-17 21:54:58.345 resp = self.ipc("get", ffid, method)
    fetchpython 2025-04-17 21:54:58.345 File "/layers/google.python.pip/pip/lib/python3.11/site-packages/javascript/proxy.py", line 150, in getProp
    fetchpython 2025-04-17 21:54:58.345 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    fetchpython 2025-04-17 21:54:58.345 methodType, val = self._exe.getProp(self._pffid, attr)
    fetchpython 2025-04-17 21:54:58.343 File "/layers/google.python.pip/pip/lib/python3.11/site-packages/javascript/proxy.py", line 230, in __getattr__
    fetchpython 2025-04-17 21:54:58.343 ^^^^^^^^^^^^^^^^^^^^^^^^^
    fetchpython 2025-04-17 21:54:58.343 console = config.global_jsi.console # TODO: Remove this in 1.0
    fetchpython 2025-04-17 21:54:58.343 File "/layers/google.python.pip/pip/lib/python3.11/site-packages/javascript/__init__.py", line 18, in init
    fetchpython 2025-04-17 21:54:58.343 init()
    fetchpython 2025-04-17 21:54:58.341 File "/layers/google.python.pip/pip/lib/python3.11/site-packages/javascript/__init__.py", line 27, in <module>
    fetchpython 2025-04-17 21:54:58.341 from javascript import require
    fetchpython 2025-04-17 21:54:58.341 File "/workspace/fetch.py", line 2, in <module>
    fetchpython 2025-04-17 21:54:58.341 from fetch import KeyFetcher
    fetchpython 2025-04-17 21:54:58.341 File "/workspace/main.py", line 3, in <module>


    main.py

    from flask import Flask, request, jsonify
    from google.cloud import firestore
    from fetch import KeyFetcher
    import os

    app = Flask(__name__)
    db = firestore.Client()

    @app.route('/keys', methods=['POST'])
    def call_fetch():
    data = request.json
    version = data.get("version")

    if not version:
    return jsonify({"error": "Version is required"}), 401

    try:
    result = KeyFetcher(version).fetch_keys()
    except Exception as e:
    return jsonify({"parse error": e}), 401

    return jsonify({"result": result}), 200

    if __name__ == '__main__':
    port = int(os.environ.get('PORT', 8080))
    app.run(host='0.0.0.0', port=port)


    fetch.py

    import base64, os, re, tempfile, threading, esprima
    from javascript import require
    from functools import wraps
    from logger import Logger
    from wasm import Wasm
    import jsbeautifier
    import requests
    import binascii
    import sys, os
    import base64
    import re

    # My code here


    Requirments.txt

    firebase_functions~=0.1.0
    flask
    google-cloud-firestore
    jsbeautifier
    requests
    esprima
    javascript
    colorama

    Continue reading...

Compartilhe esta Página