Skip to content

Commit

Permalink
try fix cron issues
Browse files Browse the repository at this point in the history
  • Loading branch information
folland87 committed Feb 7, 2025
1 parent 7ffd12d commit bea0a69
Showing 1 changed file with 26 additions and 19 deletions.
45 changes: 26 additions & 19 deletions src/jobs/sirene/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ async function getLastExecutionDate() {
.collection("_jobs")
.find(filters)
.sort({ "result.lastExecution": -1 })
.limit(1)
.toArray();

return jobs?.[0]?.result?.lastExecution?.toISOString()?.slice(0, 19);
Expand Down Expand Up @@ -45,8 +46,12 @@ export async function monitorSiren(job) {
const now = new Date();
const from = await getLastExecutionDate();
const until = now.toISOString().slice(0, 19);
let res = null

if (!from) job.fail("No previous execution")
if (!from) {
job.fail("No previous execution");
return res;
}

const siretStockFromPaysage = await getSiretStockFromPaysage();
const sirenUpdatesMap = await fetchLegalUnitUpdates(from, until);
Expand All @@ -62,31 +67,33 @@ export async function monitorSiren(job) {
updates.push(...changes);
};
if (updates.length === 0) {
return {
res = {
status: "success",
message: "Nothing to update",
lastExecution: now,
from,
until,
};
} else {
const bulkOperations = makeBulk(updates);

const result = await db.collection("sirene_updates").bulkWrite(bulkOperations).catch((error) => {
console.error('Bulk write error:', error.message);
job.fail(error)
return null;
})
res = {
status: "success",
lastExecution: now,
from,
until,
updates: {
modified: result?.modifiedCount,
inserted: result?.upsertedCount
}
};
}

const bulkOperations = makeBulk(updates);

const result = await db.collection("sirene_updates").bulkWrite(bulkOperations).catch((error) => {
console.error('Bulk write error:', error);
job.fail(error.message)
})
return {
status: "success",
lastExecution: now,
from,
until,
updates: {
modified: result?.modifiedCount,
inserted: result?.upsertedCount
}
};
return res;
}

export async function monitorSiret(job) {
Expand Down

0 comments on commit bea0a69

Please sign in to comment.