Skip to content

Commit

Permalink
✨ api: unschedule old cron job when disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
Steph0 committed Oct 4, 2024
1 parent dfcb3c9 commit da95a1d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions api/src/shared/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ const configuration = (function () {

config.cpf.sendEmailJob = {
recipient: '[email protected]',
cron: '0 3 * * *',
};

config.jwtConfig.livretScolaire = { secret: 'secretosmose', tokenLifespan: '1h' };
Expand Down
19 changes: 19 additions & 0 deletions api/tests/unit/worker_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,5 +157,24 @@ describe('#registerJobs', function () {
'legyNameForScheduleComputeOrganizationLearnersCertificabilityJobController',
);
});

context('when a cron job is disabled', function () {
it('unschedule the job', async function () {
//given
sinon.stub(config.cpf.sendEmailJob, 'cron').value('0 21 * * *');
sinon.stub(config.pgBoss, 'exportSenderJobEnabled').value(false);

await registerJobs({
jobGroup: JobGroup.DEFAULT,
dependencies: {
startPgBoss: startPgBossStub,
createJobQueues: createJobQueuesStub,
},
});

// then
expect(jobQueueStub.unscheduleCronJob).to.have.been.calledWithExactly('CpfExportSenderJob');
});
});
});
});
6 changes: 6 additions & 0 deletions api/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ export async function registerJobs({ jobGroup, dependencies = { startPgBoss, cre
}
} else {
logger.warn(`Job "${job.jobName}" is disabled.`);

// For cronJob we need to unschedule older cron
if (job.jobCron) {
await jobQueues.unscheduleCronJob(job.jobName);
logger.info(`Job CRON "${job.jobName}" is unscheduled.`);
}
}
}

Expand Down

0 comments on commit da95a1d

Please sign in to comment.