Microwork
Core Microwork class that provides a way to create new microservice
Constructor Summary
Public Constructor | ||
public |
constructor(opts: object): void Microwork class construct |
Member Summary
Public Members | ||
public |
RabbitMQ exchange name |
|
public |
RabbitMQ host address |
|
public |
Service unique ID |
|
public |
Reconnect timeout timer stored for later usage |
|
public |
Active route handlers and queues |
Method Summary
Public Methods | ||
public |
registerPlugin(plugin: Object): void Register new Microwork plugin |
|
public |
Send given data to the specified topic |
|
public |
Stops the service, closes all workers/subscriptions and terminates the connection to RabbitMQ |
|
public |
subscribe(topic: string, handler: Function, queueConfig: Object, consumeConfig: Object, config: Object): string Create subscription to given topic that will pass all incoming messages to given handler |
|
public |
tryReconnect(e: *) |
|
public |
unsubscribe(topic: string, consumerTag: string): Promise Removes existing subscription or worker. |
Public Constructors
Public Members
Public Methods
public registerPlugin(plugin: Object): void source
Register new Microwork plugin
Params:
Name | Type | Attribute | Description |
plugin | Object | Microwork plugin object |
Return:
void |
Example:
import myMicroworkPlugin from 'my-microwork-plugin';
microworkInstance.registerPlugin(myMicroworkPlugin);
public send(topic: string, data: Any, opts: Object): Promise source
Send given data to the specified topic
Example:
await microworkInstance.send('test.topic', 'test');
await microworkInstance.send('test.topic', {json: 'works too'});
public stop(): Promise source
Stops the service, closes all workers/subscriptions and terminates the connection to RabbitMQ
Example:
await microworkInstance.stop();
public subscribe(topic: string, handler: Function, queueConfig: Object, consumeConfig: Object, config: Object): string source
Create subscription to given topic that will pass all incoming messages to given handler
Params:
Name | Type | Attribute | Description |
topic | string | Topic to subscribe to |
|
handler | Function | Handler function that will get all incoming messages |
|
queueConfig | Object | Queue config to pass to RabbitMQ |
|
consumeConfig | Object | Consume config to pass to RabbitMQ |
|
config | Object | Config for subscriber (e.g. wether to auto-ack messages) |
Example:
await microworkInstance.subscribe('test.topic', (msg, reply) => {
if (msg === 'ping') {
reply('test.reply', 'pong');
}
});
await microworkInstance.subscribe('test.topic', (msg, reply) => {
if (msg === 'ping') {
reply('test.reply', 'pong');
}
}, {durable: true, autoDelete: true, exclusive: true});
await microworkInstance.subscribe('test.topic', (msg, reply, ack, nack) => {
if (msg === 'ping') {
ack();
reply('test.reply', 'pong');
} else {
nack();
}
}, {}, {}, {ack: false});
public tryReconnect(e: *) source
Params:
Name | Type | Attribute | Description |
e | * |
public unsubscribe(topic: string, consumerTag: string): Promise source
Removes existing subscription or worker. If consumerTag is given only corresponding subscription will be removed. Otherwise, all consumers for given topic will be terminated.
Example:
await microworkInstance.unsubscribe('test.topic', 'tag');
await microworkInstance.unsubscribe('test.topic');