I have set up listener functions to monitor 2 JSON endpoints in my Firebase Realtime Database.
def handler1(event):
rtdb_path = event.path
m_id = rtdb_path.split('/')[1]
if m_id == "": # At init, listen() returns event at "/" with all data in reference
logprint("Endpoint1: Initialization complete, listening...")
return
logprint(f"{event.event_type.upper()} event received at {rtdb_path}")
# Do something with the event here
def handler2(event):
rtdb_path = event.path
m_id = rtdb_path.split('/')[1]
if m_id == "": # At init, listen() returns event at "/" with all data in reference
logprint("Endpoint2: Initialization complete, listening...")
return
logprint(f"{event.event_type.upper()} event received at {rtdb_path}")
# Do something with the event here
def rtdb_listen1():
ref = db.reference('myendpoint1').listen(handler1)
logprint("Endpoint1: Listener Initializing...")
def rtdb_listen2():
ref = db.reference('myendpoint2').listen(handler2)
logprint("Endpoint2: Listener Initializing...")
The above code runs in a docker container having an entrypoint file that calls rtdb_listener1() and rtdb_listener2(). When starting up the container for the first time, I get this:
2022-01-04 19:05:43 -- Endpoint1: Initializing...
2022-01-04 19:05:59 -- Endpoint1: Initialization complete, listening...
2022-01-04 19:06:00 -- Endpoint2: Initializing...
2022-01-04 19:07:13 -- Endpoint2: Initialization complete, listening...
All good, it's exactly as expected.
However, when observing the logs as well as the server CPU graphs, I notice that every single hour, the listener restarts itself like clockwork.
...
...
2022-01-07 13:06:15 -- Endpoint1: Initialization complete, listening...
2022-01-07 13:07:41 -- Endpoint2: Initialization complete, listening...
2022-01-07 14:06:15 -- Endpoint1: Initialization complete, listening...
2022-01-07 14:07:40 -- Endpoint2: Initialization complete, listening...
2022-01-07 15:06:15 -- Endpoint1: Initialization complete, listening...
2022-01-07 15:07:38 -- Endpoint2: Initialization complete, listening...
2022-01-07 16:06:15 -- Endpoint1: Initialization complete, listening...
2022-01-07 16:07:37 -- Endpoint2: Initialization complete, listening...
2022-01-07 17:06:15 -- Endpoint1: Initialization complete, listening...
2022-01-07 17:07:37 -- Endpoint2: Initialization complete, listening...
I know it's not just the event handler firing, because:
- The CPU goes to 100% when the listener is initializing every hour. During a regular event, it does not do this. See: CPU Graph
- There are no changes in the DB at the endpoint root
Also: as you can notice, during hourly restarts there is no log entry for "Endpoint1: Listener Initializing..." or "Endpoint2: Listener Initializing...", which means that my application has not restarted, but it's the Firebase SDK that's doing the restarts.
The issue is that this behavior is affecting my user experience. Each time it initializes, the listener is effectively out of action for a minute or so. If the user happens to make some changes in the frontend during this period, he will not get the feedback about these changes till the listener completes its initialization and the server then updates the frontend.
I don't believe that this is normal or acceptable behavior. Especially because I don't see any other mentions of it anywhere. Am I doing something wrong here? How do I go about debugging and fixing this?