Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

256
Views
Run a script when API endpoint is hit, and check if that script is running when hit again - Node.js

I'm working on a node.js API that needs to start/run a time-intensive javaScript file when a certain endpoint is hit. I've got this working, but what I'm now looking for is a way to stop the script from running again if it's already running and the endpoint gets hit again.

I tried to use the child_process library, hoping that if the script is being executed as a separate process from the main node.js app, it would be easier to check if the script is already running. I thought of using the process exit event for the child process that I created, but this proved to be unhelpful because it would only run once the process was done, but didn't help in checking if it was currently running.

I also had considered cron jobs, but there doesn't seem to be a good way to just start a cron job on command without making a time-based schedule.

In another project, I used a database and simply created a new row in a table to indicate whether the script was available to run, currently running, or finished. I could definitely do it this way again, but I'm trying to keep this project as light as possible.

Lastly, I did consider just adding some global state variable that would indicate if the process is running or not, but wasn't sure if that was the best method.

So, if anyone either knows a way to check if a child process script is currently running or has a better way of achieving what I'm trying to achieve here, any direction would be helpful.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use a variable, and it doesn't even have to be global!

class MySingletonProcess {
  constructor() {
    this.isBusy = false;
    this.spawnProcess = this.spawnProcess.bind(this);
  }

  doProcess() {
    console.log("Doing process...");
    // fake async stuff...
    return new Promise((resolve) => {
      setTimeout(() => {
        this.isBusy = false;
        resolve();
      }, 2500);
    });
  }

  spawnProcess() {
    if (this.isBusy) {
      console.log("I'm busy!");
      return;
    } else {
      this.isBusy = true;
      this.doProcess();
    }
  }
}

const processSingleton = new MySingletonProcess();
const button = document.getElementById("button");
button.addEventListener("click", processSingleton.spawnProcess);
<button id="button">Click Me To Spawn Process</button>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!