Itβs best to separate this task from the main application. However, it would be easy to run it in the background. To run it in the background and save it without a message queue, etc., child_process will be the child_process .
- You can run the
spawn task at the endpoint (or url) that the user invokes. - Then configure a
socket to return live monitoring of the child process - Add another endpoint to stop the job, with a unique identifier returned by
1. (or not, depending on your concurrency needs)
Some coding ideas:
var spawn = require('child_process').spawn var job = null //keeping the job in memory to kill it app.get('/save', function(req, res) { if(job && job.pid) return res.status(500).send('Job is already running').end() job = spawn('node', ['/path/to/save/job.js'], { detached: false, //if not detached and your main process dies, the child will be killed too stdio: [process.stdin, process.stdout, process.stderr] //those can be file streams for logs or wathever }) job.on('close', function(code) { job = null //send socket informations about the job ending }) return res.status(201) //created }) app.get('/stop', function(req, res) { if(!job || !job.pid) return res.status(404).end() job.kill('SIGTERM') //or process.kill(job.pid, 'SIGTERM') job = null return res.status(200).end() }) app.get('/isAlive', function(req, res) { try { job.kill(0) return res.status(200).end() } catch(e) { return res.status(500).send(e).end() } })
To execute the child process, you can use pidusage , we use it in PM2 . Add a route to complete the task and call it every second. Remember to free up memory when completing a job.
You might want to check out this library , which will help you manage multiprocessing through microservices.
source share