skip to Main Content

I have a problem during the execution of my python script from crontab, which consists of an insert operation in the firestore database.

db.collection(u'ab').document(str(row["Name"])).collection(str(row["id"])).document(str(row2["id"])).set(self.packStructure(row2))

When I execute normally with python3 script.py command it works, but when I execute it from crontab it return the following error:

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/axatel/angel_bridge/esportazione_firebase/main.py", line 23, in <module>
    dato.getDati(dato, db, cursor, cursor2, fdb, select, anagrafica)
  File "/home/axatel/angel_bridge/esportazione_firebase/dati.py", line 19, in getDati 
db.collection(u'ab').document(str(row["Name"])).collection(str(row["id"])).document(str(row2["id"])).set(self.packStructure(row2))
  File "/home/axatel/.local/lib/python3.7/site-packages/google/cloud/firestore_v1/document.py", line 234, in set
    write_results = batch.commit()
  File "/home/axatel/.local/lib/python3.7/site-packages/google/cloud/firestore_v1/batch.py", line 147, in commit
    metadata=self._client._rpc_metadata,
  File "/home/axatel/.local/lib/python3.7/site-packages/google/cloud/firestore_v1/gapic/firestore_client.py", line 1121, in commit
    request, retry=retry, timeout=timeout, metadata=metadata
  File "/home/axatel/.local/lib/python3.7/site-packages/google/api_core/gapic_v1/method.py", line 145, in __call__
    return wrapped_func(*args, **kwargs)
  File "/home/axatel/.local/lib/python3.7/site-packages/google/api_core/retry.py", line 286, in retry_wrapped_func
    on_error=on_error,
  File "/home/axatel/.local/lib/python3.7/site-packages/google/api_core/retry.py", line 184, in retry_target
    return target()
  File "/home/axatel/.local/lib/python3.7/site-packages/google/api_core/timeout.py", line 214, in func_with_timeout
    return func(*args, **kwargs)
  File "/home/axatel/.local/lib/python3.7/site-packages/google/api_core/grpc_helpers.py", line 59, in error_remapped_callable
    six.raise_from(exceptions.from_grpc_error(exc), exc)
  File "<string>", line 3, in raise_from
google.api_core.exceptions.ServiceUnavailable: 503 DNS resolution failed for service: firestore.googleapis.com:443

I really don’t understand what’s the problem, because the connection at the database works every time the script is started in both ways.

Is there a fix for this kind of issue?

2

Answers


  1. Chosen as BEST ANSWER

    I found the solution.
    The problem occured because the timeout sleep() value was lower than expected, so the database connection function starts too early during boot phase of machine. Increasing this value to 45 or 60 seconds fixed the problem.

    #time.sleep(10) # old version
    time.sleep(60) # working version
    
    fdb = firebaseConnection()
    
    def firebaseConnection():
        # firebase connection
        cred = credentials.Certificate('/database/axatel.json')
        firebase_admin.initialize_app(cred)
        fdb = firestore.client()
        if fdb:
            return fdb
        else:
            print("Error")
            sys.exit()
    

  2. I found something that might be helpful. There is nice troubleshooting guide and there is a part there, which seems to be related:

    If your command works by invoking a runtime like python some-command.py perform a few checks to determine that the runtime
    version and environment is correct. Each language runtime has quirks
    that can cause unexpected behavior under crontab.

    For python you might find that your web app is using a virtual
    environment you need to invoke in your crontab.

    I haven’t seen such error running Firestore API, but this seems to match to your issue.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search