mathcoll / very-simple-queue

Very Simple Queue is a job queue with a simple API and support for sqlite3, redis and mysql

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Very Simple Queue

CI

Very Simple Queue is a job queue with a simple API and support for:

  • redis
  • mysql
  • sqlite3

Installation

npm install very-simple-queue

or

yarn add very-simple-queue

Usage

Instantiating the VerySimpleQueue facade

const VerySimpleQueue = require('very-simple-queue');

const verySimpleQueue = new VerySimpleQueue('sqlite3', {
  filePath: '/tmp/testdb.sqlite3',
});

Usage example

await verySimpleQueue.createJobsDbStructure(); // Only the first time
await verySimpleQueue.pushJob({ obladi: "oblada" }, 'myQueue');
await verySimpleQueue.handleJob((payload) => console.log(payload), 'myQueue');

Workers

Using the work function

await verySimpleQueue.work((payload) => console.log(payload), { queue: 'myQueue' });
Default values for worker settings
{
  queue: 'default',
  restTimeInSeconds: 5,
  logResults: false,
  limit: null, // Number of jobs to handle before stopping
  logErrors: false,
  stopOnFailure: false,
  loggerFunction: console.log,
}

Custom workers

You can create custom workers using the provided functions to handle jobs. You only need a loop. Check out the API reference for more information.

API Reference

VerySimpleQueue

Kind: global class


new VerySimpleQueue(driverName, driverConfig)

VerySimpleQueue client constructor

Param Type Description
driverName string 'sqlite3' or 'redis'
driverConfig Sqlite3DriverConfig | Object Driver specific configuration. For redis see https://github.com/NodeRedis/node-redis#options-object-properties . For mysql see https://github.com/mysqljs/mysql#connection-options .

Example (Sqlite3 driver)

new VerySimpleQueue('sqlite3', { filePath: '/tmp/db.sqlite3' });

Example (Redis driver)

new VerySimpleQueue('redis', {}); // Options: https://github.com/NodeRedis/node-redis#options-object-properties

Example (MySQL driver)

new VerySimpleQueue('mysql', {
     host: 'localhost',
     user: 'root',
     password: 'root',
     database: 'queue',
   }); // Options: https://github.com/mysqljs/mysql#connection-options

verySimpleQueue.createJobsDbStructure() ⇒ Promise.<void>

Creates the jobs table for SQL drivers and does nothing for redis

Kind: instance method of VerySimpleQueue


verySimpleQueue.pushJob(payload, [queue]) ⇒ Promise.<string>

Push a new job to a queue

Kind: instance method of VerySimpleQueue Returns: Promise.<string> - - A promise of the created job's uuid

Param Type Default Description
payload Object This the object that the handler is going to get when you try to handle the job
[queue] string "default" Queue name

Example

const jobUuid = verySimpleQueue.pushJob({ sendEmailTo: 'foo@foo.com' }, 'emails-to-send');

verySimpleQueue.handleJob(jobHandler, [queue], [throwErrorOnFailure]) ⇒ Promise.<*>

Handle one job on the given queue The job get's deleted if it doesn't fail and is marked a failed if it does

Kind: instance method of VerySimpleQueue Returns: Promise.<*> - - A promise of what the jobHandler returns

Param Type Default Description
jobHandler JobHandler Function that will receive the payload and handle the job
[queue] string "default" The queue from which to take the job
[throwErrorOnFailure] boolean false If a job fails, mark it failed and then throw an error

Example

verySimpleQueue.handleJob((payload) => sendEmail(payload.email), 'emails-to-send');

verySimpleQueue.handleJobByUuid(jobHandler, jobUuid, [throwErrorOnFailure]) ⇒ Promise.<*>

Handle a job by uuid Same as handleJob but here you know which job you want to handle

Kind: instance method of VerySimpleQueue Returns: Promise.<*> - - A promise of what the jobHandler returns

Param Type Default Description
jobHandler JobHandler Function that will receive the payload and handle the job
jobUuid string The job uuid that you've got when you pushed the job
[throwErrorOnFailure] boolean false If a job fails, mark it failed and then throw an error

Example

verySimpleQueue.handleJobByUuid(
 (payload) => sendEmail(payload.email),
 'd5dfb2d6-b845-4e04-b669-7913bfcb2600'
);

verySimpleQueue.handleFailedJob(jobHandler, [queue], [throwErrorOnFailure]) ⇒ Promise.<*>

Handle a job that failed on a given queue

Kind: instance method of VerySimpleQueue Returns: Promise.<*> - - A promise of what the jobHandler returns

Param Type Default Description
jobHandler JobHandler Function that will receive the payload and handle the job
[queue] string "default" The queue from which to take the failed job
[throwErrorOnFailure] boolean false If a job fails, mark it failed and then throw an error

Example

verySimpleQueue.handleFailedJob((payload) => tryAgain(payload.email), 'emails-to-send');

verySimpleQueue.closeConnection() ⇒ Promise.<void>

Closes the connection to the database

Kind: instance method of VerySimpleQueue


verySimpleQueue.work(jobHandler, settings) ⇒ Promise.<void>

Worker function to continuously handle jobs on a queue

Kind: instance method of VerySimpleQueue

Param Type
jobHandler JobHandler
settings WorkerSettings

Example

verySimpleQueue.work(
 (payload) => sendEmail(payload.email),
 { queue: 'email-to-send' }
);

types


types.JobHandler(payload)

Kind: static method of types

Param Type
payload Object

types.Sqlite3DriverConfig : Object

Sqlite3DriverConfig

Kind: static typedef of types Properties

Name Type
filePath string

types.WorkerSettings : Object

WorkerSettings

Kind: static typedef of types Properties

Name Type Default Description
[queue] string "default" The queue to work on
[restTimeInSeconds] Number 5 Time to wait after attempting to handle a job whether successful or not
[limit] Number | null Max number of jobs to be handled
[logResults] boolean false console.log the return value of the handler function
[logErrors] boolean false console.log errors for failed jobs
[stopOnFailure] boolean false Stop the worker if a job fails
[logger] function console.log Function used to log. Defaults to console.log

License

ISC

About

Very Simple Queue is a job queue with a simple API and support for sqlite3, redis and mysql

License:ISC License


Languages

Language:JavaScript 96.9%Language:Shell 3.1%