H
H
hckn2018-08-19 14:48:48
Node.js
hckn, 2018-08-19 14:48:48

How to stop a running function (task)?

On the server (Node/Express), tasks are running that run at intervals

function testJob (name, timeout) {
  setInterval(() => {
    console.log(name)
  }, timeout)
}

function startUpJobs () {
  Job.find({}).then(jobs => {
    jobs.forEach(job => {
      testJob(job.name, job.timeout)
    })
  })
}

The tasks themselves are stored in Monge, created through the API.
And how to delete a task? For example, I no longer need one of the created tasks. I want to kill him from the GUI. How to implement it? I looked at the bible cronfor Node https://github.com/kelektiv/node-cron there is a way stop()but I don't understand how to identify the right task?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rostislav, 2018-08-20
Makkall @viogull

You can try to separate the task code into a separate module, and then run it using spawn() or fork(), while keeping the process id. And to stop the execution of the task, then call process.kill(pid).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question