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

@chris.troutner/retry-queue-commonjs

v1.0.8

Published

A Promise-based Queue with automatic retry

Downloads

96

Readme

retry-queue-commonjs

This library is based on p-queue and p-retry. It create a Queue for executing Promise-based functions. If the function fails, it will be automatically retried.

Executing promise-based functions and then needed to do an automatic retry if it fails, is an extremely common scenario is the software I write. I created this small library because I got tired of constantly re-writing this functionality.

This repository is forked from npm-lib-boilerplate-esm.

Installation

  • npm install --save-exact @chris.troutner/retry-queue-commonjs

Environment

  • node.js v16+

Example

This provides a simple example of how to use this retry queue.

longRunningProcess() is a Promise-based function. It lasts for 2-5 seconds and it has a 25% chance of failing.

A new longRunningProcess() is added to the queue as soon as the previous one completes. Awaiting on the queue will wait until the queue is empty before adding a new task. If the await is omitted, all processes will be added immediately and the Queue is allowed to work according concurrency setting.

// Include the library.
const RetryQueue = require('@chris.troutner/retry-queue-commonjs')

// Retry-Queue settings:
const options = {
  concurrency: 1,
  attempts: 5,
  retryPeriod: 1000
}
const retryQueue = new RetryQueue(options)

// Example function using the retry-queue.
async function retryQueueExample () {
  try {
    // Generate 10 processes.
    const processCount = 10
    for (let i = 0; i < processCount; i++) {
      // Dummy data to pass to the function.
      const now = new Date()
      const processObj = {
        index: i,
        startedOn: now.toLocaleString()
      }

      // Add the process to the queue.
      // Omit the await to load up the queue quickly and allow its internal
      // concurrency logic to drive execution.
      await retryQueue.addToQueue(longRunningProcess, processObj)
    }
  } catch (err) {
    console.error('Error in retryQueueExample(): ', err)
  }
}
retryQueueExample()

// A long-running, Promise-based function
// It will run between 2-5 seconds.
// It has a 25% chance of throwing an error.
async function longRunningProcess (inObj = {}) {
  // Wait for 2 seconds.
  await retryQueue.sleep(2000)

  const rndNum1 = Math.random()
  const rndNum2 = 3 * Math.random()

  // 25% chance of failing
  if (rndNum1 < 0.25) throw new Error('longRunningProcess() failed')

  // Wait an additional 0-3 seconds
  await retryQueue.sleep(rndNum2)

  // Add a timestamp for when the process completed.
  const now = new Date()
  inObj.stoppedOn = now.toLocaleString()

  console.log(`longRunningProcess() ${inObj.index+1} completed with input object ${JSON.stringify(inObj, null, 2)}`)

  return true
}

The above code will produce an output like this:

Attempt 1 of 5 failed. Waiting before trying again.
Waiting 1000 milliseconds before trying again.

Attempt 2 of 4 failed. Waiting before trying again.
Waiting 1000 milliseconds before trying again.

longRunningProcess() 1 completed with input object {
  "index": 0,
  "startedOn": "9/15/2022, 1:31:35 PM",
  "stoppedOn": "9/15/2022, 1:31:47 PM"
}
longRunningProcess() 2 completed with input object {
  "index": 1,
  "startedOn": "9/15/2022, 1:31:47 PM",
  "stoppedOn": "9/15/2022, 1:31:49 PM"
}
longRunningProcess() 3 completed with input object {
  "index": 2,
  "startedOn": "9/15/2022, 1:31:49 PM",
  "stoppedOn": "9/15/2022, 1:31:51 PM"
}
longRunningProcess() 4 completed with input object {
  "index": 3,
  "startedOn": "9/15/2022, 1:31:51 PM",
  "stoppedOn": "9/15/2022, 1:31:53 PM"
}
longRunningProcess() 5 completed with input object {
  "index": 4,
  "startedOn": "9/15/2022, 1:31:53 PM",
  "stoppedOn": "9/15/2022, 1:31:55 PM"
}
longRunningProcess() 6 completed with input object {
  "index": 5,
  "startedOn": "9/15/2022, 1:31:55 PM",
  "stoppedOn": "9/15/2022, 1:31:57 PM"
}
longRunningProcess() 7 completed with input object {
  "index": 6,
  "startedOn": "9/15/2022, 1:31:57 PM",
  "stoppedOn": "9/15/2022, 1:31:59 PM"
}
Attempt 1 of 5 failed. Waiting before trying again.
Waiting 1000 milliseconds before trying again.

Attempt 2 of 4 failed. Waiting before trying again.
Waiting 1000 milliseconds before trying again.

longRunningProcess() 8 completed with input object {
  "index": 7,
  "startedOn": "9/15/2022, 1:31:59 PM",
  "stoppedOn": "9/15/2022, 1:32:10 PM"
}
longRunningProcess() 9 completed with input object {
  "index": 8,
  "startedOn": "9/15/2022, 1:32:10 PM",
  "stoppedOn": "9/15/2022, 1:32:12 PM"
}
longRunningProcess() 10 completed with input object {
  "index": 9,
  "startedOn": "9/15/2022, 1:32:12 PM",
  "stoppedOn": "9/15/2022, 1:32:14 PM"
}

Licence

MIT