Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Process timeout | Amazon Lambda to Firebase

i've written code in node.js and my data is on Firebase. The problem i'm facing is that my code never exits. I've done it like this one Link

The problem is that firebase referance/listener never become null and therefore my function never exits. I tried using firebase.database().goOffline() but it didn't work.

On my local machine i forcefully stopped the process using process.exit(0), but when i deployed my code on AWS lambda, it doesn't return any response/call back and exits (giving error message "Process exited before completing request")

I also added wait of 5-10 seconds after invoking callback in lambda and then forcefully exited the process, but it didn't help either.

How to fix this issue? Please help.

like image 488
Fayza Nawaz Avatar asked Jul 25 '16 05:07

Fayza Nawaz


1 Answers

Your going through crisis that any new lambda user has gone.

As suggested, you can use context.done for stopping. However, this is not recommended as this is only possible due to historic runtime versions of nodejs.


why this timeout happens?

Your lambda may get to the last line of your code and still keep running. Well, it is actually waiting for something - for the event loop to be empty.

what this means?

In nodejs, when you make an async operation and register a callback function to be executed once the operation is done, the registration sort of happens in the event loop.

In one line, it's the event loop that knows which callback function to execute when an async operation ends. But that's to another thread :)


back to Lambda

Given the above information, it follows that lambda should not halt before empty event loop is reached - as this means some follow-up procedure will not execute after some async operation returns.

What if you still need to halt the execution manually? regardless of the event loop status? At the beginning of the function, execute:

context.callbackWaitsForEmptyEventLoop = false

And then use the third parameter you get in the handler signature. Which is the callback.

the callback parameter

It is a function which you call when you want to end the execution.

If you call it with no parameters, or with the first parameter as null and text as second parameter - it is considered as a successful invocation.

To fail the lambda execution, you can call the callback function with some non-null value as the first parameter.

like image 183
johni Avatar answered Oct 02 '22 06:10

johni