Skip to content

Commit c9363e5

Browse files
[TECH] Ne plus planifier de job unitaire pour chaque calcul de certificabilité.
#11287
2 parents bfdb231 + 1c548ff commit c9363e5

File tree

4 files changed

+413
-177
lines changed

4 files changed

+413
-177
lines changed

api/src/prescription/learner-management/application/jobs/schedule-compute-organization-learners-certificability-job-controller.js

+24-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { DomainTransaction } from '../../../../shared/domain/DomainTransaction.j
88
import * as organizationLearnerRepository from '../../../../shared/infrastructure/repositories/organization-learner-repository.js';
99
import { logger } from '../../../../shared/infrastructure/utils/logger.js';
1010
import { computeCertificabilityJobRepository } from '../../../learner-management/infrastructure/repositories/jobs/compute-certificability-job-repository.js';
11+
import { usecases } from '../../domain/usecases/index.js';
1112

1213
class ScheduleComputeOrganizationLearnersCertificabilityJobController extends JobScheduleController {
1314
constructor() {
@@ -26,8 +27,11 @@ class ScheduleComputeOrganizationLearnersCertificabilityJobController extends Jo
2627
}) {
2728
const skipLoggedLastDayCheck = data?.skipLoggedLastDayCheck;
2829
const onlyNotComputed = data?.onlyNotComputed;
29-
const chunkSize = dependencies.config.features.scheduleComputeOrganizationLearnersCertificability.chunkSize;
30-
const cronConfig = dependencies.config.features.scheduleComputeOrganizationLearnersCertificability.cron;
30+
const {
31+
chunkSize,
32+
cron: cronConfig,
33+
synchronously,
34+
} = dependencies.config.features.scheduleComputeOrganizationLearnersCertificability;
3135

3236
const isolationLevel = 'repeatable read';
3337

@@ -71,6 +75,17 @@ class ScheduleComputeOrganizationLearnersCertificabilityJobController extends Jo
7175
`ScheduleComputeOrganizationLearnersCertificabilityJobHandler - Ids count : ${organizationLearnerIds.length}`,
7276
);
7377

78+
if (synchronously) {
79+
for (const organizationLearnerId of organizationLearnerIds) {
80+
await usecases.computeOrganizationLearnerCertificability({ organizationLearnerId });
81+
}
82+
83+
dependencies.logger.info(
84+
`ScheduleComputeOrganizationLearnersCertificabilityJobHandler - Certificability computed count : ${organizationLearnerIds.length}`,
85+
);
86+
continue;
87+
}
88+
7489
const jobsToInsert = organizationLearnerIds.map(
7590
(organizationLearnerId) => new ComputeCertificabilityJob({ organizationLearnerId }),
7691
);
@@ -83,6 +98,13 @@ class ScheduleComputeOrganizationLearnersCertificabilityJobController extends Jo
8398
);
8499
}
85100

101+
if (synchronously) {
102+
dependencies.logger.info(
103+
`ScheduleComputeOrganizationLearnersCertificabilityJobHandler - Total certificability computed count : ${totalJobsInserted}`,
104+
);
105+
106+
return;
107+
}
86108
dependencies.logger.info(
87109
`ScheduleComputeOrganizationLearnersCertificabilityJobHandler - Total jobs inserted count : ${totalJobsInserted}`,
88110
);

api/src/shared/config.js

+1
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ const configuration = (function () {
249249
scheduleComputeOrganizationLearnersCertificability: {
250250
cron: process.env.SCHEDULE_COMPUTE_LEARNERS_CERTIFICABILITY_JOB_CRON || '0 21 * * *',
251251
chunkSize: process.env.SCHEDULE_COMPUTE_LEARNERS_CERTIFICABILITY_CHUNK_SIZE || 1000,
252+
synchronously: process.env.SCHEDULE_COMPUTE_LEARNERS_CERTIFICABILITY_SYNCHRONOUSLY || false,
252253
},
253254
scoAccountRecoveryKeyLifetimeMinutes: process.env.SCO_ACCOUNT_RECOVERY_KEY_LIFETIME_MINUTES,
254255
},

api/tests/prescription/learner-management/integration/application/jobs/schedule-compute-organization-learners-certificability-job-controller_test.js

+53-20
Original file line numberDiff line numberDiff line change
@@ -32,31 +32,64 @@ describe('Integration | Infrastructure | Jobs | scheduleComputeOrganizationLearn
3232
};
3333
});
3434

35-
it('should schedule multiple ComputeCertificabilityJob', async function () {
36-
// given
37-
const scheduleComputeOrganizationLearnersCertificabilityJobHandler =
38-
new ScheduleComputeOrganizationLearnersCertificabilityJobController();
39-
40-
// when
41-
await scheduleComputeOrganizationLearnersCertificabilityJobHandler.handle({
42-
data: { skipLoggedLastDayCheck: true, onlyNotComputed: false },
43-
dependencies: {
44-
logger,
45-
organizationLearnerRepository,
46-
computeCertificabilityJobRepository,
47-
config: {
48-
features: {
49-
scheduleComputeOrganizationLearnersCertificability: {
50-
chunkSize: 2,
51-
cron: '0 21 * * *',
35+
context('when computation is asynchronous', function () {
36+
it('should schedule multiple ComputeCertificabilityJob', async function () {
37+
// given
38+
const scheduleComputeOrganizationLearnersCertificabilityJobHandler =
39+
new ScheduleComputeOrganizationLearnersCertificabilityJobController();
40+
41+
// when
42+
await scheduleComputeOrganizationLearnersCertificabilityJobHandler.handle({
43+
data: { skipLoggedLastDayCheck: true, onlyNotComputed: false },
44+
dependencies: {
45+
logger,
46+
organizationLearnerRepository,
47+
computeCertificabilityJobRepository,
48+
config: {
49+
features: {
50+
scheduleComputeOrganizationLearnersCertificability: {
51+
chunkSize: 2,
52+
cron: '0 21 * * *',
53+
synchronously: false,
54+
},
5255
},
5356
},
5457
},
55-
},
58+
});
59+
60+
// then
61+
await expect('ComputeCertificabilityJob').to.have.been.performed.withJobsCount(2);
5662
});
57-
// then
63+
});
5864

59-
await expect('ComputeCertificabilityJob').to.have.been.performed.withJobsCount(2);
65+
context('when computation is synchronous', function () {
66+
it('should not schedule any ComputeCertificabilityJob', async function () {
67+
// given
68+
const scheduleComputeOrganizationLearnersCertificabilityJobHandler =
69+
new ScheduleComputeOrganizationLearnersCertificabilityJobController();
70+
71+
// when
72+
await scheduleComputeOrganizationLearnersCertificabilityJobHandler.handle({
73+
data: { skipLoggedLastDayCheck: true, onlyNotComputed: false },
74+
dependencies: {
75+
logger,
76+
organizationLearnerRepository,
77+
computeCertificabilityJobRepository,
78+
config: {
79+
features: {
80+
scheduleComputeOrganizationLearnersCertificability: {
81+
chunkSize: 2,
82+
cron: '0 21 * * *',
83+
synchronously: true,
84+
},
85+
},
86+
},
87+
},
88+
});
89+
90+
// then
91+
await expect('ComputeCertificabilityJob').to.have.been.performed.withJobsCount(0);
92+
});
6093
});
6194
});
6295
});

0 commit comments

Comments
 (0)