This repository was archived by the owner on Oct 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathworker.js
100 lines (95 loc) · 3.36 KB
/
worker.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
const rethink = require('rethinkdb')
const config = require('config')
const JSONfn = require('json-fn')
let pluginWorkerRegister = 'role:worker,cmd:register'
let defaultConnction = config.get('defaultConnection')
let defaultWorker = config.get('defaultWorker')
// console.log(defaultConnectionOptions, defaultQueueOption, defaultCreateJobOption)
module.exports = function worker (options) {
let rethinkDBInfo, newoptions
let plugin = this
let rdbConn = connectRethinkDB(defaultConnction)
console.log("==========options===>", options)
this.add(pluginWorkerRegister, async function (msg, response) {
try {
let validParamErr
[validParamErr, msg] = validateRequestBody(msg)
if (validParamErr) {
response(validParamErr)
return false
}
console.log("=====================job Options=======")
await registerWroker(defaultConnction.db, defaultWorker.table, rdbConn, msg)
.then(result => {
result.connctionInfo = rethinkDBInfo
response(null, result)
})
.catch(err => {
err.connctionInfo = rethinkDBInfo
response(err)
})
} catch (err) {
response(err)
}
})
let validateRequestBody = function (msg) {
console.log("================>", msg, "<====================")
if (msg.request$ !== undefined) {
var contype = msg.request$.headers['content-type']
// if (!contype || contype.indexOf('application/json') !== 0) {
// let err = {error: {message: gErrMessages['ERR_CONTENT_TYPE'], code: 'ERR_CONTENT_TYPE'}}
// return [err, null]
// }
if (msg.args !== undefined && msg.args.body !== undefined && msg.args.body !== '') {
try {
let paserBody = eval('(' + msg.args.body + ')')
return [null, paserBody]
} catch (err) {
console.log(err)
let err1 = {error: {message: gErrMessages['ERR_INVALID_PRAMATER'], code: 'ERR_INVALID_PRAMATER'}}
return [err1, null]
}
} else {
let err = {error: {message: gErrMessages['ERR_PRAMATER_MISSING'], code: 'ERR_INVALID_PORT'}}
return [err, null]
}
} else {
return [null, msg]
}
}
let registerWroker = async function (db, table, rethinkDBConnection, worker) {
return new Promise((resolve, reject) => {
let workerData = {
jobType: worker.jobType,
payLoad: worker.payload
}
console.log(workerData)
rethink.db(db).table(table).insert(workerData).run(rethinkDBConnection, function (err, result) {
if (err) {
reject(err)
} else {
resolve('inserted successfully')
}
})
})
}
function connectRethinkDB (cxnOptions) {
return new Promise((resolve, reject) => {
rethink.connect({ host: cxnOptions.host, port: cxnOptions.port, db: cxnOptions.db }, function (err, conn) {
if (err) {
reject(err)
} else {
resolve(conn)
}
})
})
}
}
let gErrMessages = {
'ERR_INVALID_PORT': 'port number should be in the range [1, 65535]',
'ERR_SERVICE_UNAVAIALBLE': 'Service not avaialble',
'ERR_REQLDRIVERERROR': 'RethinkDB service unavaialble',
'ERR_PRAMATER_MISSING': 'job data missing, Please provide body as paramater',
'ERR_CONTENT_TYPE': 'Content-Type should be application/json',
'ERR_INVALID_PRAMATER': 'Please provide valid paramater'
}