Skip to content

Commit 1e3ddb0

Browse files
authored
[O2B-1125] Use scheduled process manager for synchronisation with MonALISA (#1340)
* instnatiate synchronzier * typo * use const instead of class property
1 parent 9cf155a commit 1e3ddb0

2 files changed

Lines changed: 59 additions & 20 deletions

File tree

lib/application.js

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@ const { Logger } = require('./utilities');
1414
const database = require('./database');
1515
const { webUiServer } = require('./server');
1616
const { gRPCServer } = require('./server/index.js');
17-
const { GRPCConfig } = require('./config');
17+
const { GRPCConfig, ServicesConfig } = require('./config');
18+
19+
const { monalisa: monalisaConfig } = ServicesConfig;
1820
const { handleLostRunsAndEnvironments } = require('./server/services/housekeeping/handleLostRunsAndEnvironments.js');
19-
const { ServicesConfig } = require('./config/index.js');
2021
const { isInTestMode } = require('./utilities/env-utils.js');
2122
const { ScheduledProcessesManager } = require('./server/services/ScheduledProcessesManager.js');
23+
const { MonALISASynchronizer } = require('./server/monalisa-synchronization/MonALISASynchronizer');
24+
const { createMonALISAClient } = require('./server/monalisa-synchronization/MonALISAClient');
2225

2326
/**
2427
* Bookkeeping Application
@@ -52,26 +55,20 @@ class BookkeepingApplication {
5255
await this.gRPCServer.listen(gRPCOrigin);
5356
}
5457

55-
if (ServicesConfig.enableHousekeeping) {
58+
if (monalisaConfig.enableSynchronization) {
59+
const monALISASynchronizer = await this.createMonALISASynchronizer();
5660
this.scheduledProcessesManager.schedule(
57-
async () => {
58-
try {
59-
const { transitionedEnvironments, endedRuns } = await handleLostRunsAndEnvironments();
60-
const subMessages = [];
61-
if (transitionedEnvironments.length > 0) {
62-
subMessages.push(`environments (${transitionedEnvironments.join(', ')})`);
63-
}
64-
if (endedRuns.length > 0) {
65-
subMessages.push(`runs (${endedRuns.join(', ')})`);
66-
}
67-
68-
if (subMessages.length > 0) {
69-
this.logger.debug(`Updated lost ${subMessages.join(' and ')}`);
70-
}
71-
} catch (error) {
72-
this.logger.error(`Error while handling lost runs & environments: ${error}`);
73-
}
61+
() => monALISASynchronizer.synchronizeDataPassesFromMonALISA(),
62+
{
63+
wait: 10 * 1000,
64+
every: monalisaConfig.synchronizationPeriod,
7465
},
66+
);
67+
}
68+
69+
if (ServicesConfig.enableHousekeeping) {
70+
this.scheduledProcessesManager.schedule(
71+
() => this.housekeeping(),
7572
{
7673
wait: 30 * 1000,
7774
every: 30 * 1000,
@@ -86,6 +83,43 @@ class BookkeepingApplication {
8683
this.logger.info('Started');
8784
}
8885

86+
/**
87+
* Instantiate MonALISA synchronizer with global configuration
88+
* @return {MonALISASynchronizer} instance
89+
*/
90+
async createMonALISASynchronizer() {
91+
return new MonALISASynchronizer(await createMonALISAClient({
92+
dataPassesUrl: monalisaConfig.dataPassesUrl,
93+
dataPassDetailsUrl: monalisaConfig.dataPassDetailsUrl,
94+
yearLowerLimit: monalisaConfig.dataPassesYearLowerLimit,
95+
userCertificatePath: monalisaConfig.userCertificate.path,
96+
certificatePassphrase: monalisaConfig.userCertificate.passphrase,
97+
}));
98+
}
99+
100+
/**
101+
* Houskeeping method, it wraps @see handleLostRunsAndEnvironments and logs its results
102+
* @return {Promise<void>} promise
103+
*/
104+
async housekeeping() {
105+
try {
106+
const { transitionedEnvironments, endedRuns } = await handleLostRunsAndEnvironments();
107+
const subMessages = [];
108+
if (transitionedEnvironments.length > 0) {
109+
subMessages.push(`environments (${transitionedEnvironments.join(', ')})`);
110+
}
111+
if (endedRuns.length > 0) {
112+
subMessages.push(`runs (${endedRuns.join(', ')})`);
113+
}
114+
115+
if (subMessages.length > 0) {
116+
this.logger.debug(`Updated lost ${subMessages.join(' and ')}`);
117+
}
118+
} catch (error) {
119+
this.logger.error(`Error while handling lost runs & environments: ${error}`);
120+
}
121+
}
122+
89123
/**
90124
* Begins the process of terminating the application. Calling this method terminates the process.
91125
*

lib/config/services.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ const {
1717
DATA_PASSES_YEAR_LOWER_LIMIT,
1818
MONALISA_DATA_PASSES_URL,
1919
MONALISA_DATA_PASS_DETAILS_URL,
20+
MONALISA_ENABLE_SYNCHRONIZATION,
21+
MONALISA_SYNCHRONIZATION_PERIODS,
2022
} = process.env ?? {};
2123

2224
exports.services = {
@@ -46,5 +48,8 @@ exports.services = {
4648

4749
dataPassesUrl: MONALISA_DATA_PASSES_URL,
4850
dataPassDetailsUrl: MONALISA_DATA_PASS_DETAILS_URL,
51+
52+
enableSynchronization: MONALISA_ENABLE_SYNCHRONIZATION?.toLowerCase() === 'true',
53+
synchronizationPeriod: Number(MONALISA_SYNCHRONIZATION_PERIODS) || 3600000, // 1h in milisecond
4954
},
5055
};

0 commit comments

Comments
 (0)