Skip to content

Commit 0b2a669

Browse files
authored
fix: activity check
1 parent 2df6aaf commit 0b2a669

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

src/mutation/queueDelete.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { MutationError, ErrorCodeEnum } from './../helpers/MutationError';
12
import { SchemaComposer, ObjectTypeComposerFieldConfigAsObjectDefinition } from 'graphql-compose';
3+
import { findQueue } from '../helpers';
24
import { deleteQueue } from '../helpers';
35
import { Options } from '../definitions';
46

@@ -25,8 +27,34 @@ export function createQueueDeleteFC(
2527
type: 'Boolean',
2628
defaultValue: true,
2729
},
30+
checkActivity: {
31+
type: 'Boolean',
32+
defaultValue: true,
33+
},
2834
},
29-
resolve: async (_, { prefix, queueName, checkExistence }) => {
35+
resolve: async (_, { prefix, queueName, checkExistence, checkActivity }) => {
36+
if (checkActivity) {
37+
const queue = await findQueue(prefix, queueName, opts);
38+
const actives = await queue.getActiveCount();
39+
const workers = (await queue.getWorkers()).length;
40+
41+
const messages: string[] = [];
42+
43+
if (actives > 0) {
44+
messages.push(`Queue have ${actives} active jobs.`);
45+
}
46+
47+
if (workers > 0) {
48+
messages.push(`Queue have ${workers} workers.`);
49+
}
50+
51+
if (messages.length > 0) {
52+
throw new MutationError(
53+
['Queue is active!', ...messages].join(' '),
54+
ErrorCodeEnum.OTHER_ERROR
55+
);
56+
}
57+
}
3058
const total = await deleteQueue(prefix, queueName, opts, checkExistence);
3159
return { total };
3260
},

src/types/queue/Queue.durationAvg.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,17 @@ export function createDurationAvgFC(
1616
},
1717
resolve: async (queue: Queue, { limit }) => {
1818
const jobs = await queue.getCompleted(0, limit);
19-
20-
const amount = jobs.reduce((acc, job) => {
21-
if (job?.finishedOn && job?.processedOn) {
22-
return acc + job.finishedOn - job?.processedOn;
19+
let amount = 0;
20+
if (jobs.length === 0) {
21+
return 0;
22+
} else {
23+
for (const job of jobs) {
24+
if (job.finishedOn && job.processedOn) {
25+
amount += job.finishedOn - job?.processedOn;
26+
}
2327
}
24-
25-
return acc;
26-
}, 0);
27-
28-
return (amount / jobs.length).toFixed(0);
28+
return (amount / jobs.length).toFixed(0);
29+
}
2930
},
3031
};
3132
}

0 commit comments

Comments
 (0)