-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.js
65 lines (55 loc) · 1.31 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
const Hemera = require('nats-hemera')
const nats = require('nats').connect();
const mode = process.argv[2] || 'READ';
if(mode === 'WRITE'){
if(process.argv[3] === undefined){
console.log('[ERROR] Usage node worker.js WRITE new_value');
process.exit(1);
}
var value = process.argv[3];
}
const hemera = new Hemera(nats)
hemera.ready(() => {
console.log('[READY] Worker on mode:', mode)
if(mode === 'READ'){
read();
}else{
change();
// async_change();
}
})
function read(){
hemera.act({
topic: 'chabok',
cmd: 'read:normal'
},
function(err, resp){
console.log('[RESPONSE] value is', resp.value);
process.exit(0);
})
}
function change(){
hemera.act({
topic: 'chabok',
cmd: 'change:normal',
secret: value
},
function (err, resp) {
if(err) console.log('[ERR]', err);
console.log('[RESPONSE] value changed to', resp.value);
process.exit(0);
}
)
}
async function async_change() {
try{
let response = await hemera.act({
topic: 'chabok',
cmd: 'change:await',
secret: value
})
console.log('[RESPONSE] await', response.data);
}catch(e){
console.log('[ERROR]', e);
}
}