forked from Gobie/luncher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.js
48 lines (39 loc) · 1.29 KB
/
service.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'use strict'
let config = require('./config.js')
let throng = require('throng')
let start = (workerId) => {
let tv4 = require('tv4')
let winston = require('winston')
let serviceSchema = require('./lib/schema/service')
let bus = require('./lib/bus')(config, winston)
let app = require('./service/middleware/app')(config, winston)
let channelWrapper = bus.server('service.menu', (msg, data) => {
let validate = (err, res) => {
if (err) {
winston.error('SERVICE: error', data, err.stack || err)
return {error: String(err)}
}
let result = tv4.validateResult(res, serviceSchema.response, true, true)
if (!result.valid) {
winston.error('SERVICE: schema validation failed', data, result)
return {error: result}
}
return res
}
let next = (err, req, res) => {
channelWrapper.sendToQueue(
msg.properties.replyTo,
{
data: validate(err, res ? res.data : null),
timestamp: Date.now()
},
{correlationId: msg.properties.correlationId}
)
}
app.middleware.run({data: data}, {data: [], send: next}, next)
})
channelWrapper.waitForConnect().then(() => winston.info('SERVICE: worker', workerId, 'is listening'))
}
throng({
workers: config.WORKERS
}, start)