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.
CodePudding user response:
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>