Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

258
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda