Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

83 limit job data size #84

Merged
merged 2 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export type Options = {
};
redis?: RedisOptions | Redis;
redisEvents?: RedisOptions | Redis;
maxSizeOfJobData?: number /** in bytes */;
};
36 changes: 36 additions & 0 deletions src/helpers/roughSizeOfObject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Options } from '../definitions';

function roughSizeOfObject(object: unknown): number {
const objectList = [];
const stack = [object];
let bytes = 0;

while (stack.length) {
const value = stack.pop();

if (typeof value === 'boolean') {
bytes += 4;
} else if (typeof value === 'string') {
bytes += value.length * 2;
} else if (typeof value === 'number') {
bytes += 8;
} else if (typeof value === 'object' && objectList.indexOf(value as never) === -1) {
objectList.push(value as never);

for (const i in value) {
stack.push(value[i]);
}
}
}
return bytes;
}

export const defaultMaxSizeOfObjectData = 10000;

export function checkJobDataSize(opts: Options, data: unknown): void {
const allowableSize = opts?.maxSizeOfJobData || defaultMaxSizeOfObjectData;
const size = roughSizeOfObject(data);
if (size > allowableSize) {
throw new Error(`Job data ${size} exceeds allowable size ${allowableSize}`);
}
}
2 changes: 2 additions & 0 deletions src/mutation/jobAdd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getJobTC } from '../types/job/Job';
import { findQueue } from '../helpers';
import { Options } from '../definitions';
import { createJobDataITC } from '../types/job/JobInput';
import { checkJobDataSize } from '../helpers/roughSizeOfObject';

export function createJobAddFC(
sc: SchemaComposer<any>,
Expand Down Expand Up @@ -44,6 +45,7 @@ export function createJobAddFC(
}),
},
resolve: async (_, { prefix, queueName, jobName, data, options }) => {
checkJobDataSize(opts, data);
const queue = await findQueue(prefix, queueName, opts);
const job = await queue.add(jobName, data, options);
return {
Expand Down
9 changes: 9 additions & 0 deletions src/mutation/jobAddBulk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { findQueue } from '../helpers';
import { SchemaComposer, ObjectTypeComposerFieldConfigAsObjectDefinition } from 'graphql-compose';
import { getJobTC } from '../types/job/Job';
import { Options } from '../definitions';
import { checkJobDataSize } from '../helpers/roughSizeOfObject';

export function createJobAddBulkFC(
sc: SchemaComposer<any>,
Expand Down Expand Up @@ -46,6 +47,14 @@ export function createJobAddBulkFC(
}).List,
},
resolve: async (_, { prefix, queueName, jobs }) => {
if (Array.isArray(jobs)) {
for (const job of jobs) {
checkJobDataSize(opts, job);
}
} else {
throw new Error('jobAddBulk: jobs argument must be an array');
}

const queue = await findQueue(prefix, queueName, opts);
const jobsRes = await queue.addBulk(jobs);
return {
Expand Down
2 changes: 2 additions & 0 deletions src/mutation/jobAddCron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { findQueue } from '../helpers';
import { SchemaComposer, ObjectTypeComposerFieldConfigAsObjectDefinition } from 'graphql-compose';
import { getJobTC } from '../types/job/Job';
import { Options } from '../definitions';
import { checkJobDataSize } from '../helpers/roughSizeOfObject';

export function createJobAddCronFC(
sc: SchemaComposer<any>,
Expand Down Expand Up @@ -54,6 +55,7 @@ export function createJobAddCronFC(
}),
},
resolve: async (_, { prefix, queueName, jobName, data, options }) => {
checkJobDataSize(opts, data);
const queue = await findQueue(prefix, queueName, opts);
const job = await queue.add(jobName, data, options);
return {
Expand Down
2 changes: 2 additions & 0 deletions src/mutation/jobAddEvery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { findQueue } from '../helpers';
import { SchemaComposer, ObjectTypeComposerFieldConfigAsObjectDefinition } from 'graphql-compose';
import { getJobTC } from '../types/job/Job';
import { Options } from '../definitions';
import { checkJobDataSize } from '../helpers/roughSizeOfObject';

export function createJobAddEveryFC(
sc: SchemaComposer<any>,
Expand Down Expand Up @@ -53,6 +54,7 @@ export function createJobAddEveryFC(
}),
},
resolve: async (_, { prefix, queueName, jobName, data, options }) => {
checkJobDataSize(opts, data);
const queue = await findQueue(prefix, queueName, opts);
const job = await queue.add(jobName, data, options);
return {
Expand Down
6 changes: 3 additions & 3 deletions src/types/queue/Queue.durationAvg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function createDurationAvgFC(
opts: Options
): ObjectTypeComposerFieldConfigDefinition<any, any> {
return {
type: 'Int!',
type: 'String!',
args: {
limit: {
type: 'Int',
Expand All @@ -20,15 +20,15 @@ export function createDurationAvgFC(
let amount = 0;
let counter = 0;
if (jobs.length === 0) {
return 0;
return '0';
} else {
for (const job of jobs) {
if (job?.finishedOn && job?.processedOn) {
amount += job.finishedOn - job.processedOn;
counter++;
}
}
return (amount / (counter || 1)).toFixed(0);
return String((amount / (counter || 1)).toFixed(0));
}
},
};
Expand Down
Loading