Skip to content

Instantly share code, notes, and snippets.

@williamespindola
Created August 26, 2018 23:19
Show Gist options
  • Save williamespindola/dbe4ebd36604b29a82babc1d00ae75f7 to your computer and use it in GitHub Desktop.
Save williamespindola/dbe4ebd36604b29a82babc1d00ae75f7 to your computer and use it in GitHub Desktop.
const cp = require('child_process');
const n = cp.fork(`${__dirname}/operation.js`);
operationList = [
{id: 1, status: false},
{id: 2, status: false},
{id: 3, status: false},
{id: 4, status: false}
];
var foo = 1;
var latest = null;
// evento recebido no termino do puppeteer
n.on('message', (operation) => {
console.log(`Done ${operation.id}`);
++foo;
if (operationList[foo] != undefined) {
n.send(operationList[foo]);
}
if (latest !== null) {
n.disconnect();
console.log('Finish');
}
if (operationList[foo] == undefined) {
latest = foo;
}
});
n.send(operationList[0]);
n.send(operationList[1]);
// operationList.forEach(operation => n.send(operation));
// operation.js file
process.on('message', (operation) => {
console.log('Star: ', operation.id)
operation.status = true;
let time = parseInt(operation.id + '000');
if ((operation.id / 3) === 0) {
time = 100;
}
setTimeout(() => {
process.send(operation);
},
time
)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment