Skip to content

Instantly share code, notes, and snippets.

@kianenigma
Last active May 8, 2017 10:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kianenigma/ded9d4598ac139a8c002c08cbd463bbb to your computer and use it in GitHub Desktop.
Save kianenigma/ded9d4598ac139a8c002c08cbd463bbb to your computer and use it in GitHub Desktop.
const XYZ = require('xyz-core')
const fs = require('fs')
// create a new microservice
let worker = new XYZ({
selfConf: {
name: 'worker.ms',
// rename this if you want to deploy on a VPS
host: '127.0.0.1',
transport: [{type: 'HTTP', port: 6000}],
// all nodes will connect to seed node which is Front service
seed: ['127.0.0.1:4000'],
// always good to see some logs!
logLevel: 'verbose'
}
})
// a cpu task
worker.register('/task/cpu', (payload, response) => {
let num = 1
for (let i = 1; i < 100; i++) {
num = num * i
}
response.jsonify(num)
})
// an IO task
worker.register('/task/io', (payload, response) => {
const MAX = 1000
for (let i = 0; i <= MAX; i++) {
fs.writeFile('./trash.txt', String(i), function (_i, err) {
if (err) throw err
else {
if (_i === MAX) {
response.jsonify('written')
}
}
}.bind(null, i))
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment