Skip to content

Commit

Permalink
Merge pull request #327 from BaseAdresseNationale/antoineludeau/fix-q…
Browse files Browse the repository at this point in the history
…ueue-active-state

Modified job add or update mechanism
  • Loading branch information
antoineludeau authored Nov 27, 2023
2 parents e5fd0bc + 0eb9c19 commit 9721699
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
1 change: 0 additions & 1 deletion lib/api/consumers/api-consumer.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ export default async function apiConsumers({data: {dataType, jobType, data, stat
await addOrUpdateJob(
exportToExploitationDBQueue,
districtID,
{districtID},
exportToExploitationDBJobDelay
)
})
Expand Down
15 changes: 8 additions & 7 deletions lib/api/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9721699

Please sign in to comment.