Skip to content

Commit

Permalink
Added validation to addJob() in jobs/queue.mjs to ensure that data is…
Browse files Browse the repository at this point in the history
… an object.
  • Loading branch information
rizen committed Dec 17, 2024
1 parent 900d23b commit ea9354c
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
5 changes: 5 additions & 0 deletions ving/docs/change-log.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ outline: deep
---
# Change Log

## December 2024

### 2024-12-17
* Added validation to addJob() in jobs/queue.mjs to ensure that data is an object.

## October 2024

### 2024-10-30
Expand Down
2 changes: 1 addition & 1 deletion ving/jobs/handlers/Test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import ving from '#ving/index.mjs';
* @returns {boolean} `true`
*/
export default async function (job) {
ving.log('jobs').debug(`Test ran with data: ${JSON.stringify(job.data)}`);
ving.log('jobs').debug(`Test ran with data: ${JSON.stringify(job.data)} which is a ${typeof job.data}`);
return true;
}
6 changes: 5 additions & 1 deletion ving/jobs/queue.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Queue } from 'bullmq';
import ving from '#ving/index.mjs';
import { useRedis } from '#ving/redis.mjs';
import { jobHandlers } from '#ving/jobs/map.mjs';
import { isObject } from '#ving/utils/identify.mjs';

/**
* Get BullMQ queue object.
Expand Down Expand Up @@ -35,7 +36,7 @@ export const getHandlerNames = () => {
* Enqueues a job in the jobs system.
*
* @param {string} type Must match the filename (without the `.mjs`) of a job handler.
* @param {Object} data An object containing whatever data you wish to pass into the job.
* @param {Object|Undefined} data An object containing whatever data you wish to pass into the job.
* @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.
Expand All @@ -49,6 +50,9 @@ export const getHandlerNames = () => {
export const addJob = async (type, data = {}, options = { queueName: 'jobs' }) => {
if (!(getHandlerNames().includes(type)))
throw ving.ouch(404, `Job handler ${type} is not available.`);
if (!isObject(data)) {
throw ving.ouch(442, `Data must be an object.`);
}
const queue = getQueue(options);
const jobOptions = {
removeOnComplete: {
Expand Down

0 comments on commit ea9354c

Please sign in to comment.