worker
High performance Node.js/PostgreSQL job queue (also suitable for getting jobs generated by PostgreSQL triggers/functions out into a different work queue)
Top Related Projects
Quick Overview
Graphile Worker is a job queue system for Node.js, designed to be high-performance, reliable, and feature-rich. It uses PostgreSQL for job storage and scheduling, making it a robust solution for background task processing in Node.js applications.
Pros
- Built on PostgreSQL, providing reliability and ACID compliance
- Supports job prioritization, retries, and scheduled jobs
- Offers real-time job execution without polling
- Scalable across multiple servers for high-volume processing
Cons
- Requires PostgreSQL as a dependency
- May have a steeper learning curve compared to simpler job queue systems
- Limited to Node.js environments
- Potential performance bottlenecks with very high concurrency due to PostgreSQL limitations
Code Examples
- Creating a worker:
import { run, Task } from "graphile-worker";
const taskList: Task[] = {
hello: async (payload, helpers) => {
console.log(`Hello, ${payload.name}!`);
},
};
await run({
connectionString: "postgres://user:pass@host:5432/dbname",
concurrency: 5,
taskList,
});
- Adding a job:
import { Pool } from "pg";
import { addJob } from "graphile-worker";
const pool = new Pool({
connectionString: "postgres://user:pass@host:5432/dbname",
});
await addJob(
pool,
"hello",
{ name: "World" },
{ priority: 10, runAt: new Date(Date.now() + 60000) }
);
- Using job helpers:
const sendEmail: Task = async (payload, helpers) => {
const { email, subject, body } = payload;
await sendEmailFunction(email, subject, body);
if (helpers.withPgClient) {
await helpers.withPgClient(async (pgClient) => {
await pgClient.query("UPDATE users SET last_emailed_at = NOW() WHERE email = $1", [email]);
});
}
};
Getting Started
-
Install the package:
npm install graphile-worker
-
Set up your PostgreSQL database and create the required tables:
CREATE SCHEMA graphile_worker; CREATE EXTENSION IF NOT EXISTS pgcrypto WITH SCHEMA public;
-
Create a worker file (e.g.,
worker.ts
):import { run, Task } from "graphile-worker"; const taskList: Task[] = { exampleTask: async (payload, helpers) => { console.log("Task executed with payload:", payload); }, }; async function main() { await run({ connectionString: "postgres://user:pass@host:5432/dbname", concurrency: 5, taskList, }); } main().catch(console.error);
-
Run the worker:
ts-node worker.ts
Competitor Comparisons
Premium Queue package for handling distributed jobs and messages in NodeJS.
Pros of Bull
- More mature and widely adopted project with a larger community
- Supports Redis as a backend, offering high performance and scalability
- Provides a user-friendly UI for monitoring and managing jobs
Cons of Bull
- Requires Redis as a dependency, which may not be suitable for all projects
- Less integrated with PostgreSQL compared to Graphile Worker
- May have higher operational complexity due to additional infrastructure needs
Code Comparison
Bull:
const Queue = require('bull');
const myQueue = new Queue('my-queue');
myQueue.add({ data: 'job payload' });
myQueue.process(async (job) => {
console.log(job.data);
});
Graphile Worker:
const { run, quickAddJob } = require("graphile-worker");
await quickAddJob("my-task", { data: "job payload" });
async function worker() {
await run({
taskList: {
"my-task": async (payload) => {
console.log(payload);
},
},
});
}
Both Bull and Graphile Worker offer robust job queue functionality, but they cater to different use cases and infrastructure preferences. Bull excels in Redis-based environments with its mature ecosystem and monitoring tools, while Graphile Worker integrates seamlessly with PostgreSQL-based applications, offering a simpler setup for projects already using this database.
Lightweight job scheduling for Node.js
Pros of Agenda
- More mature project with a larger community and ecosystem
- Supports multiple storage backends (MongoDB, Redis, etc.)
- Offers a web-based UI for job management and monitoring
Cons of Agenda
- Limited to JavaScript/Node.js environments
- Requires a separate database for job storage
- May have higher overhead for simple use cases
Code Comparison
Agenda:
const Agenda = require('agenda');
const agenda = new Agenda({db: {address: mongoConnectionString}});
agenda.define('send email', async job => {
await sendEmail(job.attrs.data.to, job.attrs.data.subject);
});
await agenda.start();
await agenda.schedule('in 5 minutes', 'send email', {to: 'user@example.com', subject: 'Hello'});
Worker:
const { run, makeWorkerUtils } = require("graphile-worker");
async function main() {
const runner = await run({
connectionString: process.env.DATABASE_URL,
concurrency: 5,
taskList: {
send_email: async (payload, helpers) => {
await sendEmail(payload.to, payload.subject);
},
},
});
}
main().catch(console.error);
Both libraries offer job scheduling and processing capabilities, but Worker is more tightly integrated with PostgreSQL and offers better type safety and performance for PostgreSQL-based applications.
A simple, fast, robust job/task queue for Node.js, backed by Redis.
Pros of bee-queue
- Simple and lightweight, focusing on Redis-based job queuing
- Supports job progress reporting and events
- Provides a clean API for job creation and processing
Cons of bee-queue
- Limited to Redis as the only backend storage option
- Lacks advanced features like job dependencies or scheduling
- May have scalability limitations for very high-volume workloads
Code Comparison
bee-queue:
const Queue = require('bee-queue');
const queue = new Queue('example');
queue.process(async (job) => {
return `Processed job ${job.id}`;
});
graphile/worker:
const { run, Task } = require("graphile-worker");
const taskList = {
exampleTask: async (payload, helpers) => {
return `Processed job ${helpers.job.id}`;
},
};
run({ taskList }).catch(console.error);
Key Differences
- bee-queue is more focused on simple Redis-based job queuing, while graphile/worker offers a more comprehensive task processing system
- graphile/worker uses PostgreSQL as its backend, providing ACID compliance and advanced querying capabilities
- bee-queue has a simpler API, while graphile/worker offers more advanced features like job scheduling and dependencies
- graphile/worker integrates well with GraphQL and Postgres-based applications, whereas bee-queue is more lightweight and Redis-centric
Simple job queues for Python
Pros of RQ
- Simple and lightweight Python-based job queue system
- Easy integration with existing Python projects
- Supports job prioritization and worker process management
Cons of RQ
- Limited to Python ecosystem
- Lacks advanced features like job dependencies and scheduling
Code Comparison
RQ:
from rq import Queue
from redis import Redis
q = Queue(connection=Redis())
result = q.enqueue(my_function, arg1, arg2)
Graphile Worker:
const { run, quickAddJob } = require("graphile-worker");
await quickAddJob("my-task", { arg1, arg2 });
Key Differences
- RQ is Python-specific, while Graphile Worker is JavaScript/Node.js-based
- Graphile Worker uses PostgreSQL for job storage, whereas RQ relies on Redis
- Graphile Worker offers more advanced features like job dependencies and custom retry logic
Use Cases
- RQ: Ideal for Python projects requiring simple job queuing
- Graphile Worker: Better suited for complex Node.js applications with PostgreSQL integration
Both projects have active communities and regular updates, but Graphile Worker provides more robust features for enterprise-level job processing needs.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
graphile-worker
Job queue for PostgreSQL running on Node.js - allows you to run jobs (e.g. sending emails, performing calculations, generating PDFs, etc) "in the background" so that your HTTP response/application code is not held up. Can be used with any PostgreSQL-backed application. Pairs beautifully with PostGraphile or PostgREST.
Website at worker.graphile.org
Crowd-funded open-source software
To help us develop this software sustainably, we ask all individuals and businesses that use it to help support its ongoing maintenance and development via sponsorship.
Click here to find out more about sponsors and sponsorship.
And please give some love to our featured sponsors ð¤©:
The Guild * |
Dovetail * |
Stellate * |
Steelhead * |
LatchBio * |
Trigger.dev |
* Sponsors the entire Graphile suite
Top Related Projects
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot