Created
October 31, 2017 23:31
-
-
Save crizstian/ef08055ef16f82a5d5bd332a15a2186f to your computer and use it in GitHub Desktop.
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 cluster = require('cluster') | |
const testW = require('./test-worker') | |
let readlogs, saveLogs | |
// local logic worker recieved | |
const test = () => { | |
readlogs.send({msg: 'hey calmenla !!!'}) | |
readlogs.on('message', ({msg, id}) => { | |
console.log('Master ' + process.pid + ' received message from worker ' + id + '.', msg) | |
saveLogs.send({msg}) | |
}) | |
saveLogs.on('message', ({msg, id}) => { | |
console.log('Master ' + process.pid + ' received message from worker ' + id + '.', msg) | |
}) | |
} | |
const handleExternalreadLogs = (id) => { | |
console.log('Worker ' + id + ' has started.') | |
process.on('message', (msg) => { | |
const respond = testW.recieve(msg) | |
process.send({msg: respond, id}) | |
}) | |
} | |
const handleExternalsaveLogs = (id) => { | |
console.log('Worker ' + id + ' has started.') | |
process.on('message', (msg) => { | |
const respond = testW.save(msg) | |
process.send({msg: respond, id}) | |
}) | |
} | |
if (cluster.isMaster) { | |
readlogs = cluster.fork({WorkerName: 'readlogs'}) | |
saveLogs = cluster.fork({WorkerName: 'savelogs'}) | |
// movelogs = cluster.fork({WorkerName: 'movelogs'}) | |
test() | |
cluster.on('exit', function (worker, code, signal) { | |
if (worker === 'readlogs') { | |
readlogs = cluster.fork({WorkerName: 'readlogs'}) | |
} else if (worker === 'savelogs') { | |
readlogs = cluster.fork({WorkerName: 'savelogs'}) | |
} | |
// else if (worker === 'movelogs') { | |
// readlogs = cluster.fork({WorkerName: 'movelogs'}) | |
// } | |
}) | |
} else if (process.env.WorkerName === 'readlogs') { | |
handleExternalreadLogs(process.pid) | |
} else if (process.env.WorkerName === 'savelogs') { | |
handleExternalsaveLogs(process.pid) | |
} else if (process.env.WorkerName === 'movelogs') { | |
// Code of Worker2 | |
} |
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
'use strict' | |
const respond = (msg) => `calmala recibido...` | |
const recieve = (msg) => respond(msg) | |
const save = (msg) => { | |
console.log(`message recieved, ${msg}`) | |
return `saving logs....` | |
} | |
module.exports = Object.create({ | |
recieve, | |
save | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment