0

How should I implement a PHP exec like call to a system function with HapiJS? The user submits a processing job that needs to run in the background for some time.

I somehow need to return a job id / session id to the user, run the job asynchronously, allow the user to check back for completion and reroute when completed...

I bet there are existing solutions for that, yet I'd highly welcome a pointer into the right direction.

El Dude
  • 5,328
  • 11
  • 54
  • 101

1 Answers1

1

Check out node's child process documentation: here

To do what you are describing I would spawn a process without a callback and then use a little trick: trying to kill a process that isn't running causes an error see here

const exec = require('child_process').exec;

//Launch the process
const child = exec('ls');
const pid = child.pid;

//later in another scope when you are looking to see if it is running
try {
    process.kill(pid, 0);
}
catch (e) {
    console.log("it's finished");
}
Dana Case
  • 84
  • 1
  • 8