0

My code is successfully working in nodejs6. I just wanted to deploy it in nodejs8.

index.js contains below code

// [START functions bgMCMA]
/**
 * Background Cloud Function - Triggered by PubSub.
 * @param {!Object} event The Cloud Functions event.
 * @param {!Function} The callback function.
 */
exports.bgScrapeMCMA = (event, callback) => {

    //Logic to avoid infinite loop on timeout
    const eventAge = Date.now() - Date.parse(event.timestamp);
    const eventMaxAge = 420000; //7Minutes as timeout is set as 504 seconds(9Minutes)

    // Ignore events that are too old
    if (eventAge > eventMaxAge) {
      console.log(`INFINITE-LOOP-BREAK : Dropping context ${context} with age ${eventAge} ms.`);
      callback(); // Don't forget to call the callback. Remember its either a callback() or promise
      return;
    }    

    // The Cloud Pub/Sub Message object.
    const pubSubMessage = event.data;
    const message = pubSubMessage.data ? Buffer.from(pubSubMessage.data, 'base64').toString() : 'NONE';

    console.log('MCMA triggered time : ' + DateTime.getCurrentISTdt("yyyy-mmm-dd hh:mm:ss") + ' Message : ' + message);

    MCMA.start().then( (resp) =>{
        console.log('MCMA : Success : '+resp);
        callback(); // Don't forget to call the callback. Remember its either a callback() or promise        
    }).catch( (e) =>{
        console.error('MCMA : Error : '+e);
        callback(); // Don't forget to call the callback. Remember its either a callback() or promise
    });

};
// [END functions bgMCMA]

Deployed to nodejs8 using below command

gcloud functions deploy bgScrapeMCMA --trigger-resource psTriggerbgScrapeMCMA --trigger-event google.pubsub.topic.publish --entry-point=bgScrapeMCMA --timeout=540s --runtime nodejs8

Function got deployed successfully.

I am triggering the code via pubsub. From the functions console, i will click the topic and publish message like "TEST" in the message textarea.

What expected is ?
When in check in the logs it usually displays the message "TEST". So, i can understand function is triggered successfully.

What i am seeing after deploying in nodejs8 ?

  1. After i input the message "TEST" in pubsub. In the log the message i see is "NONE". So my understanding is function got triggered by it didn't get my message.

  2. After function executing for around 45Seconds. It crashes with message textPayload: "Error: function crashed out of request scope Function invocation was interrupted.".

  3. When i rerun the same, this time it crashes because of memory textPayload: "Error: memory limit exceeded. Function invocation was interrupted."

Since the second time it crashes due to memory, currently using is 256MB. So i edit the function and increase it to 512MB. The first error comes back again, "Error: function crashed out of request scope Function invocation was interrupted.".

At this point, i doubt my program which was previously working, just to upgrade to nodejs8. I deleted the previous working function and tried to deploy it with new node version. So i go back and redeploy with nodejs6 and 256MB memory. When i execute the function it works.

What the function basically does is, it scrapes a site and updates firebase. Functions elapsed is around 10s - 45s.

So two things i would basically like to know is, what has changed in NodeJS8 in GCP.
1. Simple message from pubsub is not coming in the log

const pubSubMessage = event.data;
const message = pubSubMessage.data ? Buffer.from(pubSubMessage.data, 'base64').toString() : 'NONE';

console.log('MCMA triggered time : ' + DateTime.getCurrentISTdt("yyyy-mmm-dd hh:mm:ss") + ' Message : ' + message);
  1. Consuming lots of memory. What working nodejs6 with 256MB is crashing with even 512MB.

I know nodejs8 is in beta. Eventually it will come to GA. How to resolve this.

bobby.dreamer
  • 366
  • 4
  • 19

1 Answers1

1

The comment below shows how this issue was resolved.

Daniel Collins
  • 752
  • 3
  • 14
  • 1
    My bad, i thought upgrading nodejs is easy just adding --runtime nodejs8 flag. I didn't [check the sample programs in link](https://cloud.google.com/functions/docs/writing/background#functions_background_parameters-node8). It seems i will have to do some bit of other changes as well. After updating the program as mentioned in above link, it works. – bobby.dreamer Feb 18 '19 at 16:57