-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconsumer.js
63 lines (56 loc) · 1.42 KB
/
consumer.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
const { ServiceBroker } = require("moleculer");
const QueueMixin = require("../../index");
let broker = new ServiceBroker({
logger: console,
transporter: "TCP",
});
const queueMixin = QueueMixin({
connection: "amqp://localhost",
asyncActions: true, // Enable auto generate .async version for actions
});
broker.createService({
name: "consumer",
version: 1,
mixins: [
queueMixin,
],
settings: {
amqp: {
connection: "amqp://localhost", // You can also override setting from service setting
},
},
actions: {
hello: {
queue: { // Enable queue for this action
// Options for AMQP queue
amqp: {
queueAssert: {
durable: true,
},
consume: {
noAck: false,
},
prefetch: 0,
},
dedupHash: (ctx) => {
return ctx.params.name;
},
},
params: {
name: "string|convert:true|empty:false",
},
async handler(ctx) {
this.logger.info(`[CONSUMER] PID: ${process.pid} Received job with name=${ctx.params.name}`);
return new Promise((resolve) => {
setTimeout(() => {
this.logger.info(`[CONSUMER] PID: ${process.pid} Processed job with name=${ctx.params.name}`);
return resolve(`hello ${ctx.params.name}`);
}, 10000);
});
},
},
},
});
broker.start().then(() => {
broker.repl();
});