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

Modified job add or update mechanism #327

Merged
merged 1 commit into from
Nov 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: 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
Loading