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