From 36196cbcae287b9f1587704001a5ac6e8bb52c6a Mon Sep 17 00:00:00 2001 From: JT Smith Date: Wed, 24 Apr 2024 14:47:32 -0500 Subject: [PATCH] Added a priority option to jobs. --- docs/change-log.md | 1 + ving/jobs/queue.mjs | 12 +++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/docs/change-log.md b/docs/change-log.md index 760c8b51..97183362 100644 --- a/docs/change-log.md +++ b/docs/change-log.md @@ -14,6 +14,7 @@ outline: deep * NOTE: Because of the above you may want to check out the new acceptedFileExtensions attribute in ving schemas and migrate your S3File integrations to use it. * Implemented: add a display of an s3file thumbnail to the page generator #105 * Fixed: pulumi doesn't create the nodmods.zip file as it should #110 +* Added a priority option to jobs. ## 2024-04-23 * Created SelectInput component to replace FormSelect. However, you should use FormInput with type select instead of using this directly in most cases. diff --git a/ving/jobs/queue.mjs b/ving/jobs/queue.mjs index 8ffd9336..630cf241 100644 --- a/ving/jobs/queue.mjs +++ b/ving/jobs/queue.mjs @@ -10,10 +10,11 @@ import { useRedis } from '#ving/redis.mjs'; * @param {Object} options An object with optional properties. * @param {string} options.queueName The name of the queue to add this job to. Defaults to `jobs`. * @param {number} options.delay The number of milliseconds to wait before executing this job. Defaults to running as soon as possible. + * @param {number} options.priority A number ranging from `1` to `2097152` where `1` is the highest possible priority. Defaults to `2097152`. */ export const addJob = async (type, data = {}, options = { queueName: 'jobs ' }) => { const queue = new Queue(options?.queueName || 'jobs'); - const job = await queue.add(type, data, { + const jobOptions = { connection: useRedis(), removeOnComplete: { age: 3600, // keep up to 1 hour @@ -22,8 +23,13 @@ export const addJob = async (type, data = {}, options = { queueName: 'jobs ' }) removeOnFail: { age: 24 * 3600, // keep up to 24 hours }, - delay: options?.delay, - }); + priority: 2097152 + } + if (options?.delay) + jobOptions.delay = options?.delay; + if (options?.priority) + jobOptions.priority = options?.priority; + const job = await queue.add(type, data, jobOptions); ving.log('jobs').info(`Job ${job.id} ${job.name} enqueued.`); await queue.close(); }