Saturday, April 29, 2017

Google Cloud Functions always timeout

If you've hopped onto the true "serverless" cloud, you've likely played with Amazon Lambda and Google Cloud functions.

I've had a particularly fun time with HTTP-triggered Cloud Functions from Google. My first foray however was shortly blocked by the strange behavior that every cloud function I wrote, would test fine in dev on Node.js but would simply timeout and hang when deployed to Google.

Struggled a bit with this, before finding his little gem, not so well-hidden in the docs, but overlooked by me:
Note: You should always call a termination method such as send(), json(), or end() when your function has completed. Otherwise your function may continue to run and be forcibly terminated by the system.
So, you must always send some response for the function to complete.

    start: 0,
    end: 200
  .on('error', function(err) {})
  .on('end', function(data) {
    while ( pos + 2 <= 200 ) {
      pos += 2;
    console.log('The file is fully downloaded');
  .on('data', function(data) {

In this case, a simple res.send() is done once the filestream ends.