Skip to content

Commit

Permalink
feat(api): use jobQueue method instead of pbboss directly
Browse files Browse the repository at this point in the history
  • Loading branch information
xav-car committed Sep 24, 2024
1 parent 2e22e9e commit bb194ba
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class ScheduleComputeOrganizationLearnersCertificabilityJobController extends Jo
});
}

get legacyName() {
return 'ComputeOrganizationLearnersCertificabilityJob';
}

async handle({
data = {},
dependencies = { organizationLearnerRepository, computeCertificabilityJobRepository, config, logger },
Expand Down
2 changes: 1 addition & 1 deletion api/src/shared/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ const configuration = (function () {
pixCertifScoBlockedAccessDateCollege: process.env.PIX_CERTIF_SCO_BLOCKED_ACCESS_DATE_COLLEGE,
scheduleComputeOrganizationLearnersCertificability: {
cron: process.env.SCHEDULE_COMPUTE_LEARNERS_CERTIFICABILITY_JOB_CRON || '0 21 * * *',
chunkSize: process.env.SCHEDULE_COMPUTE_LEARNERS_CERTIFICABILITY_CHUNK_SIZE || 50000,
chunkSize: process.env.SCHEDULE_COMPUTE_LEARNERS_CERTIFICABILITY_CHUNK_SIZE || 1000,
},
scoAccountRecoveryKeyLifetimeMinutes: process.env.SCO_ACCOUNT_RECOVERY_KEY_LIFETIME_MINUTES,
},
Expand Down
80 changes: 72 additions & 8 deletions api/tests/unit/worker_test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { UserAnonymizedEventLoggingJob } from '../../src/identity-access-management/domain/models/UserAnonymizedEventLoggingJob.js';
import { ScheduleComputeOrganizationLearnersCertificabilityJobController } from '../../src/prescription/learner-management/application/jobs/schedule-compute-organization-learners-certificability-job-controller.js';
import { ValidateOrganizationLearnersImportFileJobController } from '../../src/prescription/learner-management/application/jobs/validate-organization-learners-import-file-job-controller.js';
import { ValidateOrganizationImportFileJob } from '../../src/prescription/learner-management/domain/models/ValidateOrganizationImportFileJob.js';
import { UserAnonymizedEventLoggingJobController } from '../../src/shared/application/jobs/audit-log/user-anonymized-event-logging-job-controller.js';
Expand All @@ -8,16 +9,19 @@ import { registerJobs } from '../../worker.js';
import { catchErr, expect, sinon } from '../test-helper.js';

describe('#registerJobs', function () {
let startPgBossStub, createJobQueuesStub, scheduleCpfJobsStub, jobQueueStub;
let startPgBossStub, createJobQueuesStub, jobQueueStub;

beforeEach(function () {
const pgBossStub = { schedule: sinon.stub() };
jobQueueStub = { register: sinon.stub() };
const pgBossStub = Symbol('pgBoss');
jobQueueStub = { register: sinon.stub(), scheduleCronJob: sinon.stub(), unscheduleCronJob: sinon.stub() };
startPgBossStub = sinon.stub();
startPgBossStub.resolves(pgBossStub);
createJobQueuesStub = sinon.stub();
createJobQueuesStub.withArgs(pgBossStub).returns(jobQueueStub);
scheduleCpfJobsStub = sinon.stub();
});

afterEach(function () {
sinon.restore();
});

it('should register UserAnonymizedEventLoggingJob', async function () {
Expand All @@ -27,7 +31,6 @@ describe('#registerJobs', function () {
dependencies: {
startPgBoss: startPgBossStub,
createJobQueues: createJobQueuesStub,
scheduleCpfJobs: scheduleCpfJobsStub,
},
});

Expand All @@ -38,6 +41,26 @@ describe('#registerJobs', function () {
);
});

it('should register legacyName from UserAnonymizedEventLoggingJob', async function () {
// when
sinon
.stub(UserAnonymizedEventLoggingJobController.prototype, 'legacyName')
.get(() => 'legyNameForUserAnonymizedEventLoggingJobController');
await registerJobs({
jobGroup: JobGroup.DEFAULT,
dependencies: {
startPgBoss: startPgBossStub,
createJobQueues: createJobQueuesStub,
},
});

// then
expect(jobQueueStub.register).to.have.been.calledWithExactly(
'legyNameForUserAnonymizedEventLoggingJobController',
UserAnonymizedEventLoggingJobController,
);
});

it('should register ValidateOrganizationImportFileJob when job is enabled', async function () {
//given
sinon.stub(config.pgBoss, 'validationFileJobEnabled').value(true);
Expand All @@ -48,7 +71,6 @@ describe('#registerJobs', function () {
dependencies: {
startPgBoss: startPgBossStub,
createJobQueues: createJobQueuesStub,
scheduleCpfJobs: scheduleCpfJobsStub,
},
});

Expand All @@ -69,7 +91,6 @@ describe('#registerJobs', function () {
dependencies: {
startPgBoss: startPgBossStub,
createJobQueues: createJobQueuesStub,
scheduleCpfJobs: scheduleCpfJobsStub,
},
});

Expand All @@ -86,12 +107,55 @@ describe('#registerJobs', function () {
dependencies: {
startPgBoss: startPgBossStub,
createJobQueues: createJobQueuesStub,
scheduleCpfJobs: scheduleCpfJobsStub,
},
});

// then
expect(error).to.be.instanceOf(Error);
expect(error.message).to.equal(`Job group invalid, allowed Job groups are [${Object.values(JobGroup)}]`);
});

describe('cron Job', function () {
it('schedule ScheduleComputeOrganizationLearnersCertificabilityJob', async function () {
//given
sinon.stub(config.features.scheduleComputeOrganizationLearnersCertificability, 'cron').value('0 21 * * *');

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

// then
expect(jobQueueStub.scheduleCronJob).to.have.been.calledWithExactly({
name: 'ScheduleComputeOrganizationLearnersCertificabilityJob',
cron: '0 21 * * *',
options: { tz: 'Europe/Paris' },
});
});

it('unschedule legacyName from ScheduleComputeOrganizationLearnersCertificabilityJob', async function () {
//given
sinon
.stub(ScheduleComputeOrganizationLearnersCertificabilityJobController.prototype, 'legacyName')
.get(() => 'legyNameForScheduleComputeOrganizationLearnersCertificabilityJobController');

sinon.stub(config.features.scheduleComputeOrganizationLearnersCertificability, 'cron').value('0 21 * * *');

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

// then
expect(jobQueueStub.unscheduleCronJob).to.have.been.calledWithExactly(
'legyNameForScheduleComputeOrganizationLearnersCertificabilityJobController',
);
});
});
});
10 changes: 8 additions & 2 deletions api/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,20 @@ export async function registerJobs({ jobGroup, dependencies = { startPgBoss, cre
logger.info(`Job "${job.jobName}" registered from module "${moduleName}."`);
jobQueues.register(job.jobName, ModuleClass);

if (job.legacyName) {
if (!job.jobCron && job.legacyName) {
logger.warn(`Temporary Job" ${job.legacyName}" registered from module "${moduleName}."`);
jobQueues.register(job.legacyName, ModuleClass);
}

if (job.jobCron) {
await pgBoss.schedule(job.jobName, job.jobCron, {}, { tz: 'Europe/Paris' });
await jobQueues.scheduleCronJob({ name: job.jobName, cron: job.jobCron, options: { tz: 'Europe/Paris' } });
logger.info(`Cron for job "${job.jobName}" scheduled "${job.jobCron}"`);

// For cronJob we need to unschedule older cron
if (job.legacyName) {
await jobQueues.unscheduleCronJob(job.legacyName);
}

cronJobCount++;
} else {
jobRegisteredCount++;
Expand Down

0 comments on commit bb194ba

Please sign in to comment.