Home > Mobile >  How to complete a process in Node JS after executing all the operations
How to complete a process in Node JS after executing all the operations

Time:03-28

I am very new to NodeJS and trying to develop an application which acts as a scheduler that tries to fetch data from ELK and sends the processed data to another ELK. I am able to achieve the expected behaviour but after completing all the processes, scheduler job does not exists and wait for another scheduler job to come up.

Note: This scheduler runs every 3 minutes.

job.js

const self = module.exports = {
  async schedule() {
    if (process.env.SCHEDULER == "MinuteFrequency") {
      var timenow = moment().seconds(0).milliseconds(0).valueOf();
      var endtime = timenow - 60000;
      var starttime = endtime - 60000 * 3;
      //sendData is an async method
      reports.sendData(starttime, endtime, "SCHEDULER");
    } 
  }
}

I tried various solutions such Promise.allSettled(....., Promise.resolve(true), etc, but not able to fix this.

As per my requirement, I want the scheduler to complete and process and exit so that I can save some resources as I am planning to deploy the application using Kubernetes cronjobs.

CodePudding user response:

When all your work is done, you can call process.exit() to cause your application to exit.

In this particular code, you may need to know when reports.sendData() is actually done before exiting. We would have to know what that code is and/or see the code to know how to know when it is done. Just because it's an async function doesn't mean it's written properly to return a promise that resolves when it's done. If you want further help, show us the code for sendData() and any code that it calls too.

  • Related