Usage
Initialize Queue
After defining your job types, payloads, and handlers, you need to initialize the job queue which basically sets up the connection pool to the Postgres database.
import { initJobQueue } from '@nicnocquee/dataqueue';
import { type JobPayloadMap } from './types/job-payload-map';
let jobQueuePromise: ReturnType<typeof initJobQueue<JobPayloadMap>> | null =
null;
export const getJobQueue = async () => {
if (!jobQueuePromise) {
jobQueuePromise = initJobQueue<JobPayloadMap>({
databaseConfig: {
connectionString: process.env.PG_DATAQUEUE_DATABASE, // Set this in your environment
},
verbose: process.env.NODE_ENV === 'development',
});
}
return jobQueuePromise;
};
You can now use this queue instance throughout your app to add jobs, process jobs, and more.
import { getJobQueue } from '@/lib/queue';
const sendEmail = async () => {
const jobQueue = await getJobQueue();
await jobQueue.addJob({
jobType: 'send_email',
payload: {
to: 'test@example.com',
subject: 'Hello',
body: 'Hello, world!',
},
});
};