Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@therms/future-queue

Package Overview
Dependencies
Maintainers
4
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@therms/future-queue

A MongoDB queue service for Node.js with scheduling and retrying

  • 1.0.0
  • latest
  • npm
  • Socket score

Version published
Maintainers
4
Created
Source

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 with 0.1kb data array processed in ~17 seconds.
  • Machine: 2 CPU, 4GB RAM Digital Ocean
    • 10k messages with 5kb 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
  }
)

Keywords

FAQs

Package last updated on 01 Jan 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc