StanchionJS
A simple & fast queue done right. backed by Redis, supports auto-reconnect, TypeScript, Promise and Rxjs.
Features
-
Fast. Just
BLPOP
andRPUSH
, no other fancy stuff, simple and fast. -
Auto-Reconnect.
-
Works with Promise. Unleash the power of
async / await
, say bye-bye to callback. -
Better error handling.
-
Written in TypeScript. No hacky code and provides better experience if you are using
TypeScript
. -
Reactive. If you don't like making promises, there're reactive APIs too, all APIs have two versions.
Installation
$ npm i stanchionjs
Examples
Initialize
const Stanchion = ; const stanchion = redis: host: '127.0.0.1' port: 6379 db: '1' concurrency: 20;
How To Create a Job
const Stanchion = ; const stanchion = ; // Payload will be serialized using `JSON.stringify`const job_1 = thing: 'apple';const job_2 = thing: 'banana'; //// Promise way:// stanchion; //// Rxjs way:// stanchion;
How To Process a Job
When a Job processing is done, you must tell StanchionJS
so it can go fetching next Job for you. StanchionJS
provides several ways to do that:
const Stanchion = ;const Observable = ; // Not required. const stanchion = ; //// Promise way:// stanchion; //// Async / Await way:// stanchion; //// Rxjs way:// stanchion; // Don't forget to subscribe!
Error Handling
Every exception (including those from redis
) can be obtained by attach handler to Stanchion
instance:
const Stanchion = ; const stanchion = ; //// Callback handler:// stanchion; //// Rxjs stream:// stanchion;
How To Exit
const Stanchion = ; const stanchion = ; //// Promise way:// stanchion; //// Rxjs way:// stanchion;
When Exited
const Stanchion = ; const stanchion = ; //// Promise way:// stanchion; //// Rxjs way:// stanchion;
Reference
Interfaces
ConstructOptions
Default value:
// Redis configuration. redis: host: '127.0.0.1' port: 6739 // If you have lots of I/O intensive jobs, increase this may help. concurrency: 10 // Redis key for this queue. // Stanchion also support multiple keys, just use: // rediskey: ['stanchion_queue1', 'stanchion_queue2'] redisKey: 'stanchion_queue' // How many times you want Stanchion to try reconnecting when connection is lost. retryAttempts: 6
Stanchion
Stanchion#constructor
constructoroptions: ConstructOptions
Stanchion#push
push...jobs: any: Promise<void>
Stanchion#push$
push$...jobs: any: Observable<void>
Stanchion#getSize
getSize: Promise<number>
Stanchion#getSize$
getSize$: Observable<number>
Stanchion#onError
onErrorhandler: ErrorHandler: Subscription
Stanchion#onError$
onError$: Subject<any>
Stanchion#process
processprocessor: PromiseProcessor: Promise<void>
Stanchion#process$
process$processor: ObservableProcessor: Observable<void>
Stanchion#shutdown
shutdown: Promise<void>
Stanchion#shutdown$
shutdown$: Observable<void>
Stanchion#isShutdowned
isShutdowned: boolean
Stanchion#onShutdowned
onShutdownedcb: VoidFunction: Subscription
Stanchion#onShutdowned$
onShutdowned$: Observable<void>
TODOs
-
Tests. Although Stanchion is bettle-tested, handling about 26K jobs per second.
-
Multiple queue (Redis key) support.
-
"unprocess" method.
-
Key-sharding support for cluster usage.
Credits
Awesome icon by Freepik, licensed under the Creative Commons BY 3.0.
Inspired by Neamar/featureless-job-queue.