-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
174 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
const { ServiceBroker } = require("moleculer"); | ||
const QueueMixin = require("../../index"); | ||
|
||
let broker = new ServiceBroker({ | ||
logger: console, | ||
transporter: "TCP", | ||
tracking: { // Enable moleculer graceful shutdown | ||
enabled: true, | ||
shutdownTimeout: 30 * 1000 | ||
} | ||
}); | ||
|
||
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}`); | ||
}, 3000); // Simulate slow task | ||
}); | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
broker.start().then(() => { | ||
broker.repl(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
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: "publisher", | ||
version: 1, | ||
|
||
mixins: [ | ||
queueMixin, | ||
], | ||
|
||
settings: { | ||
amqp: { | ||
connection: "amqp://localhost", // You can also override setting from service setting | ||
}, | ||
}, | ||
|
||
async started() { | ||
await broker.waitForServices({ name: "consumer", version: 1 }); | ||
|
||
let name = "repeat_name"; | ||
setInterval(async () => { | ||
const response = await broker.call("v1.consumer.hello.async", { | ||
// `params` is the real param will be passed to original action | ||
params: { | ||
name, | ||
}, | ||
// `options` is the real options will be passed to original action | ||
options: { | ||
timeout: 12000, | ||
}, | ||
}); | ||
this.logger.info(`[PUBLISHER] PID: ${process.pid} Called job with name=${name} response=${JSON.stringify(response)}`); | ||
}, 500); | ||
} | ||
}); | ||
|
||
broker.start().then(() => { | ||
broker.repl(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters