node-multitask
Advanced tools
Comparing version 0.3.0 to 0.3.1
@@ -19,27 +19,29 @@ "use strict"; | ||
const receive = { type: 1 /* receive */, time: Date.now(), payload }; | ||
let timer; | ||
const start = { type: 2 /* start */, time: Date.now(), payload }; | ||
process.send(receive); | ||
process.send(start); | ||
const work = Promise.race([ | ||
new Promise((resolve, reject) => { | ||
try { | ||
const result = require(payload.work)(payload.data); | ||
if (result instanceof Promise || typeof result.then === 'function') { | ||
result.then(r => resolve(r)); | ||
} | ||
else { | ||
resolve(result); | ||
} | ||
} | ||
catch (e) { | ||
reject(e); | ||
} | ||
}), | ||
new Promise((_, reject) => setTimeout(() => reject(new Error(`Task ${message.payload.uuid} is timeout \n Start Time: ${start.time}\n Worker file: ${payload.work}\n Worker data: ${payload.data}`)), timeout)), | ||
]); | ||
try { | ||
const start = { type: 2 /* start */, time: Date.now(), payload }; | ||
process.send(start); | ||
let result; | ||
timer = setTimeout(() => { | ||
payload.error = objectifyError(new Error(`Task ${message.payload.uuid} is timeout \n Start Time: ${start.time}\n Worker file: ${payload.work}\n Worker data: ${payload.data}`)); | ||
const finish = { type: 3 /* finish */, time: Date.now(), payload, willExit: true }; | ||
process.send(finish); | ||
process.exit(1); | ||
}, timeout); | ||
result = require(payload.work)(payload.data); | ||
if (result instanceof Promise || typeof result.then === 'function') { | ||
result = yield result; | ||
} | ||
clearTimeout(timer); | ||
payload.result = result; | ||
const finish = { type: 3 /* finish */, time: Date.now(), payload }; | ||
process.send(finish); | ||
payload.result = yield work; | ||
} | ||
catch (error) { | ||
clearTimeout(timer); | ||
console.error(error); | ||
payload.error = objectifyError(error); | ||
} | ||
finally { | ||
const finish = { type: 3 /* finish */, time: Date.now(), payload }; | ||
@@ -46,0 +48,0 @@ process.send(finish); |
{ | ||
"name": "node-multitask", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "A simple multi-core task runner", | ||
@@ -19,3 +19,3 @@ "main": "dist/index.js", | ||
"devDependencies": { | ||
"@types/node": "^7.0.5", | ||
"@types/node": "^7.0.8", | ||
"@types/uuid": "^2.0.29", | ||
@@ -22,0 +22,0 @@ "typescript": "^2.2.1" |
const { MultiTask } = require('../dist/index'); | ||
const m = new MultiTask({ thread: 2, maxTimeout: 1000 * 3 }); | ||
const m = new MultiTask({ thread: 2, maxTimeout: 1000 * 3 *10 }); | ||
m.runTask({ __dirname, work: './task', data: 'hello', timeout: 1000 * 2 }).then(console.log).catch((e) => console.error(e)); | ||
@@ -5,0 +5,0 @@ m.runTask({ __dirname, work: './task', data: 'world' }).then(console.log).catch((e) => console.error(e)); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12863
12
318