简体   繁体   中英

Catching Firebase 504 gateway timeout

I'm building a simple IOT device (with a Raspberry Pi Zero) which pulls data from Firebase Realtime Database every 1 second and checks for updates.

However, after a certain time (not sure exactly how much but somewhere between 1 hour and 3 hours) the program exits with a 504 Server Error: Gateway Time-out message. I couldn't understand exactly why this is happening, I tried to recreate this error by disconnecting the Pi from the internet and I did not get this message. Instead, the program simply paused in a ref.get() line and automatically resumed running once the connection was back.

This device is meant to be always on, so ideally if I get some kind of error, I would like to restart the program / reinitiate the connection / reboot the Pi. Is there a way to achieve something like this?

It seems like the message is actually generated by the firebase_admin package.

Here is the error message:

Traceback (most recent call last):
  File "/home/pi/.local/lib/python3.7/site-packages/firebase_admin/db.py", line 944, in request
    return super(_Client, self).request(method, url, **kwargs)
  File "/home/pi/.local/lib/python3.7/site-packages/firebase_admin/_http_client.py", line 105, in request
    resp.raise_for_status()
  File "/usr/lib/python3/dist-packages/requests/models.py", line 940, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 504 Server Error: Gateway Time-out for url: https://someFirebaseProject.firebaseio.com/someRef/subSomeRef/payload.json

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/pi/Desktop/project/main.py", line 94, in <module>
    lastUpdate = ref.get()['lastUpdate']
  File "/home/pi/.local/lib/python3.7/site-packages/firebase_admin/db.py", line 223, in get
    return self._client.body('get', self._add_suffix(), params=params)
  File "/home/pi/.local/lib/python3.7/site-packages/firebase_admin/_http_client.py", line 117, in body
    resp = self.request(method, url, **kwargs)
  File "/home/pi/.local/lib/python3.7/site-packages/firebase_admin/db.py", line 946, in request
    raise _Client.handle_rtdb_error(error)
firebase_admin.exceptions.UnknownError: Internal server error.
>>> 

To reboot the whole Raspberry Pi, you can just run a shell command:

import os
os.system("sudo reboot")

I've had this problem too and usually feel safer with that, but there's obvious downsides. I'd try resetting the wifi connection or network interface in a similar way

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM