From 0eb9c19e462dd67fb68f16ad1ac03e0471abcfeb Mon Sep 17 00:00:00 2001 From: antoineludeau <52679050+antoineludeau@users.noreply.github.com> Date: Tue, 21 Nov 2023 13:30:12 +0100 Subject: [PATCH] Modified job add or update mechanism --- lib/api/consumers/api-consumer.js | 1 - lib/api/helper.js | 15 ++++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/api/consumers/api-consumer.js b/lib/api/consumers/api-consumer.js index 66419c5c..f2b71886 100644 --- a/lib/api/consumers/api-consumer.js +++ b/lib/api/consumers/api-consumer.js @@ -39,7 +39,6 @@ export default async function apiConsumers({data: {dataType, jobType, data, stat await addOrUpdateJob( exportToExploitationDBQueue, districtID, - {districtID}, exportToExploitationDBJobDelay ) }) diff --git a/lib/api/helper.js b/lib/api/helper.js index 2a82e166..3c56d1de 100644 --- a/lib/api/helper.js +++ b/lib/api/helper.js @@ -115,16 +115,17 @@ export const checkIfDistrictsExist = async districtIDs => { } } -export const addOrUpdateJob = async (queue, jobId, data, delay) => { +export const addOrUpdateJob = async (queue, districtID, delay) => { try { - const existingJob = await queue.getJob(jobId) - - if (existingJob) { - await existingJob.remove() + const waitingJobs = await queue.getJobs(['waiting, delayed']) + const waitingJobsWithSameData = waitingJobs.filter(job => job.data.districtID === districtID) + if (waitingJobsWithSameData.length > 0) { + // Job is already waiting, do nothing + return } - await queue.add(data, { - jobId, + // Job is either active, or 'not waiting and not delayed', add to queue + await queue.add({districtID}, { delay, removeOnComplete: true, removeOnFail: true