I am reading data from my Firebase realtime database from Java code running inside a GCP Cloud Function. I am using the Admin SDK for this purpose. I am following this Google documentation page very closely.
The only approach available for Java is to attach an asynchronous callback method extending ValueEventListener
class overriding onDataChange()
method.
The documentation reads:
Asynchronous listeners: Data stored in a Firebase Realtime Database is retrieved by attaching an asynchronous listener to a database reference. The listener is triggered once for the initial state of the data and again anytime the data changes. An event listener may receive several different types of events. This mode of data retrieval is supported in Java, Node.js and Python Admin SDKs.
The code generally does what I want, however my callback method is called very late - three, five, seven minutes after the cloud function terminates, as shown by the following log snippet.
DEBUG 2023-01-22T16:01:36.992937150Z [resource.labels.functionName: xxx] [labels.executionId: hijz4xa4t89k] Function execution started
<...>
INFO 2023-01-22T 16:01:45.251247Z [resource.labels.functionName: tht-mailer-function] [labels.executionId: hijz4xa4t89k] ***4. Listener assigned.
DEBUG 2023-01-22T16:01:45.350950544Z [resource.labels.functionName: xxx] [labels.executionId: hijz4xa4t89k] Function execution took 8358 ms, finished with status code: 200
INFO 2023-01-22T 16:07:03.848739Z [resource.labels.functionName: xxx] [labels.executionId: hijz4xa4t89k] ***Inside onDataChange!!!, dataSnapshot = DataSnapshot { key =
<...>
In this specific run it took about 6 minutes (between "***4. Listener assigned" and "***Inside onDataChange!!!" log lines)
Since the documentation states that the callback will be invoked once for the initial state of the data, I expect it to be called immediately, during execution of the Cloud Functions main thread or perhaps a few seconds after its termination. Would you know how to interpret this very long delay and how to avoid it?