@therms/future-queue
v1.0.0
Published
A MongoDB queue service for Node.js with scheduling and retrying
Downloads
47
Readme
Future Queue Service
Schedule messages to be processed in the future.
This is not meant to be a full-featured queue service, but rather a simple service that can be used to schedule messages to be processed in the future.
Use Case
This service is useful when you want to schedule a message to be processed in the future. For example, you may want to send a reminder email to a user in 3 days. You can use this service to schedule the message to be sent in 3 days.
When the message is ready to be processed an implementation use-case would be to send the message to a queue service such as RabbitMQ or Kafka where your application queue service handles the message.
Performance
This implementation was tested to gain an idea of performance.
- Machine:
1 CPU, 2GB RAM Digital Ocean
10k
messages with0.1kb
data array processed in ~17 seconds.
- Machine:
2 CPU, 4GB RAM Digital Ocean
10k
messages with5kb
data array processed in ~27 seconds.
Example Usage
Add Messages to the Queue
const queue = new FutureQueue(
{
queueName: 'test-queue',
},
{
mongo: {
uri: global.__MONGO_URI__,
},
}
)
await queue.connectionReady
const fiveMinFromNow = new Date().setMinutes(fiveMinFromNow.getMinutes() + 5)
await queue.add(
{
data: { test: 'test' },
key: 'test-key',
},
{
readyTime: fiveMinFromNow,
},
)
await queue.add(
[
{
data: { test: 'test' },
key: 'test-key-2',
},
{
data: { test: 'test' },
key: 'test-key-3',
},
],
{
readyTime: fiveMinFromNow,
},
)
A message can be set with an optional expireTime
which will remove the message
from the queue if it is not processed before the expireTime
.
await queue.add(
{
data: { test: 'test' },
key: 'test-key',
},
{
expireTime: tenMinFromNow,
readyTime: fiveMinFromNow,
},
)
Process Messages
You can process messages that are ready by providing a handler to the constructor. This allows you to have instances that are designated for processing messages (ie: a background server or thread) and instances that are designated for adding and interacting with messages in the queue.
const queue = new FutureQueue(
{
queueName: 'test-queue',
},
{
mongo: {
uri: global.__MONGO_URI__,
},
},
{
onMessageReady: async (message: ReadyQueueMessage) => {/*...*/}
}
)
Messages will be processed in the order they are ready. If a message is not
processed within the message expireTime
it will be removed from the queue and
will not be processed.
Get, Cancel or Update Messages
You can get, cancel or update messages by their key. You must provide the key when adding a message to the queue.
await queue.add(
{
data: { test: 'test' },
key: 'test-key',
},
{
readyTime: fiveMinFromNow,
},
)
const message = await queue.getByKey('test-key')
// Cancel the message (effectively removing it from the queue)
await queue.cancelByKey('test-key')
// Update the message data or expire/ready time
await queue.updateByKey(
'test-key',
{
expireTime: new Date(),
readyTime: new Date(),
data: { test: 'new data' }
},
{
throwIfNotFound: false
}
)