A package that helps you manage basically queue and job
This is a package that helps you manage basically queue and job
npm i @knfs-tech/bamimi-schedule
#or
yarn add @knfs-tech/bamimi-schedule
The job
function sets up queues and workers according to the job definitions provided. It supports scheduling jobs as cron jobs and provides logging for job completion and failure.
To use the job
function, import it and provide it with the necessary parameters.
const { job, QueueManager } = require("@knfs-tech/bamimi-schedule");
const jobs = [
{
queue: "testQueue",
name: "testJob",
schedules: [
{
name: "first-day-every-month",
time: {
pattern: "0 0 1 * *"
}, // cron schedule
prepare: { data: { email: "[email protected]" } },
},
],
options: {},
handle: async (job) => { /* Job handler function */ }
}
];
const queueManager = QueueManager.getInstance({
storage: {
host: "localhost",
port: 6379
}
});
job(jobs, queueManager)
.then(() => {
console.log("All jobs initialized");
})
.catch((err) => {
console.error("Error initializing jobs:", err);
});
Initializes queues and workers for each job in the provided jobs array. If a job is marked as a cron job (isCronJob is true), it schedules the job accordingly.
Parameters
jobs: Object[]
An array of job definitions.
Each job object should have the following properties:
queue: string
The name of the queue to be used for the job.
name: string
The name of the job to be added to the queue.
isCronJob: boolean
Indicates whether the job is a cron job.
options: Object (optional, default: {})
Additional options for the job, such as scheduling and job-specific settings.
handle: Function
The handler function for processing the job.
queueManager: QueueManager
An instance of QueueManager used for managing queues and workers.
Returns
Promise<void>
Workers listen for the following events:
completed
: Fired when a job completes successfully.
Job <jobID> completed successfully
failed
: Fired when a job fails.
Job <jobID> failed with error <error.message>
Here is an example of a jobs array:
const jobs = [
{
queue: "emailQueue",
name: "sendWelcomeEmail",
options: { priority: 1 },
handle: async (job) => {
// Job handler logic for sending welcome emails
}
},
{
queue: "reportQueue",
name: "generateMonthlyReport",
schedules: [
{
name: "first-day-every-month",
time: {
pattern: "0 0 1 * *"
}, // cron schedule
prepare: { data: { email: "[email protected]" } },
},
], // Runs on the first day of every month
handle: async (job) => {
// Job handler logic for generating reports
}
}
];
Bamimi is open-sourced software licensed under the MIT license.