Skip to content

A simple type-safe mysql-based job queue for Node.js.

License

Notifications You must be signed in to change notification settings

rabrain/mysql-queue

Repository files navigation

MySQL Lite Queue

GitHub Actions Workflow Status NPM Version

A simple typesafe mysql-based job queue for Node.js.

Installation

$ npm install mysql-queue

Usage

import { connect, Runner, LiteQueue } from "mysql-queue";
import { z } from "zod";

const db = await connect("mysql://root:root@localhost:3306/queue");

const requestSchema = z.object({
    message: z.string(),
});
const ZRequest = z.infer<typeof requestSchema>;

// Init the queue
const queue = new LiteQueue<ZRequest>("requests", db, {
    defaultJobArgs: {
        numRetries: 2,
    },
    keepFailedJobs: false,
});

// Enqueue a job
await queue.enqueue({
    message: "Hello world",
});

// Start the runner
const worker = new Runner<ZRequest>(
  queue,
  {
    run: async (job) => {
      logger.info(`[${job.id}] ${job.data.message}`);
    },
    onComplete: async (job) => {
      console.log(`[${job.id}] Completed successfully`);
    },
    onError: async (job) => {
      logger.error(
        `[${job.id}] job failed: ${job.error}\n${job.error.stack}`,
      );
    },
  },
  {
    concurrency: 1,
    pollIntervalMs: 1000,
    timeoutSecs: 60,
    validator: requestSchema,
  },
);

Development

$ pnpm install

# And before submitting a PR

$ pnpm typecheck
$ pnpm test

About

A simple type-safe mysql-based job queue for Node.js.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •