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

@mevitae/redis-work-queue

v0.1.5

Published

A work queue, on top of a redis database, with implementations in Python, Rust, Go, Node.js (TypeScript) and Dotnet (C#).

Downloads

1,553

Readme

A work queue, on top of a redis database, with implementations in Python, Rust, Go, Node.js (TypeScript) and Dotnet (C#).

This is the Python implementations. For an overview of how the work queue works, it's limitations, and the general concepts and implementations in other languages, please read the redis-work-queue readme.

Setup

import Redis from 'ioredis';
const {KeyPrefix, WorkQueue} = require('@mevitae/redis-work-queue');

const db: Redis = new Redis(redisHost);

const work_queue = new WorkQueue(new KeyPrefix("example_work_queue"));

Adding work

Creating Items

const {Item} = require('@mevitae/redis-work-queue');

// Create an item from bytes
const item1Buffer: Buffer = Buffer.from('[1,2,3]', 'utf-8');
const bytesItem: Item = new Item(item1Buffer);

// Create an item from a string
const stringItem: Item = new Item('[1,2,3]');

// Create an item by JSON serializing an object
const jsonItem: Item = Item.fromJsonData([1, 2, 3]);

// Retrieve an item's data, as bytes:
console.assert(value === 42, 'Value should be 42');
console.assert(
  bytesItem.data.equals(Buffer.from('[1,2,3]', 'utf-8')),
  "'bytesItem.data' should be the same as Buffer.from('[1,2,3]', 'utf-8')."
);
console.assert(
  bytesItem.data.equals(stringItem.data),
  "'bytesItem.data' should be the same as 'stringItem.data'"
);
console.assert(
  bytesItem.data.equals(jsonItem.data),
  "'bytesItem.data' should be the same as 'jsonItem.data'"
);

Add an item to a work queue

WorkQueue.addItem(db, item)

Completing work

Please read the documentation on leasing and completing items.

const Redis = require('ioredis');
const {KeyPrefix, WorkQueue} = require('@mevitae/redis-work-queue');

const db: Redis = new Redis(redisHost);
const workQueue: WorkQueue = new WorkQueue(new KeyPrefix('Queue-name'));

while (true) {
  // Wait for a job with no timeout and a lease time of 5 seconds.
  // Use WorkQueue.lease(db, 5, 10) to timeout and return `None` after 10 seconds.
  const job: Item | None = workQueue.lease(db, 5);

  await doTheJob(job);
  workQueue.complete(db, job);
}

Handling errors

Please read the documentation on handling errors.

const {Item, WorkQueue,KeyPrefix} = require('@mevitae/redis-work-queue');

const workQueue: WorkQueue = new WorkQueue(new KeyPrefix("Queue-name"));

while (true) {
  // Wait for a job with no timeout and a lease time of 5 seconds.
  const job: Item = workQueue.lease(db, 5);
  try {
    doSomeWOrk(job);
  } catch (err) {
    if (shouldRetry(err)) {
      // Drop a job that should be retried - it will be returned to the work queue after
      //the (5 second) lease expires.
      continue;
    } else {
      // Errors that shouldn't cause a retry should mark the job as complete so it isn't
      // tried again.
      logError(err);
      workQueue.complete(db, &job);
    }
  }
  // Mark successful jobs as complete
  workQueue.complete(db, job);
}

Creating Docs

To generate the documentation, run:

npx typedoc --entryPoints ./src/WorkQueue.ts ./src/Item.ts ./src/KeyPrefix.ts