Created
February 7, 2018 04:51
-
-
Save crookm/f3d16e709a8a7996a9a85f4fe12f7541 to your computer and use it in GitHub Desktop.
Forked worker to continuously process information in node.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const self = { id: 0 }; | |
let work_loop = () => { | |
// some work here! :D | |
}; | |
function sendPayload(payload) { | |
process.send(payload); | |
} | |
function sendError(err) { | |
sendPayload({ | |
type: 'error', | |
obj: { | |
errcode: -1, | |
errmsg: err | |
} | |
}); | |
} | |
function acknowledgePayload(payload) { | |
sendPayload({ type: 'meta.acknowledged', obj: { id: payload.id } }); | |
} | |
function receivePayload(payload) { | |
switch (payload.type) { | |
case 'id.set': | |
self.id = payload.obj.id; | |
var work_loop_interval = setInterval(work_loop, 1000); | |
break; | |
} | |
acknowledgePayload(payload); | |
} | |
process.on('message', receivePayload); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment