npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@therms/future-queue

v1.0.0

Published

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

Downloads

67

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 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
  }
)