Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

Commit e110803

Browse files
authored
[ORCT-163] Enhance synchronisation logic (#178)
1 parent 2871789 commit e110803

5 files changed

Lines changed: 17 additions & 6 deletions

File tree

app/application.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ class RunConditionTableApplication {
6969
await this.databaseManager.migrate();
7070
await this.httpServer.listen();
7171
await this.databaseService.healthcheckInsertData();
72+
if (config.syncTaskAtStart) {
73+
this.syncManager.setSyncAllTask();
74+
}
7275
} catch (error) {
7376
this.logger.error(`Error while starting RCT app: ${error}`);
7477
await this.stop();

app/config/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module.exports = Object.freeze({
2323
winston: ResProvider.winston(),
2424
database: require('./database.js'),
2525
syncTaskAtStart: ResProvider.envOrDef('RCT_SYNC_TASK_AT_START', false, Boolean),
26+
syncPeriod: ResProvider.envOrDef('RCT_SYNC_PERIOD', 12 * 60 * 60 * 1000, Number),
2627
rctData: require('./rct-data'),
2728
public: require('./public.js'),
2829

app/config/rct-data/healthcheckQueries.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const metaObj = {
3939
},
4040
};
4141

42-
const queryForName = (name) => `SELECT name, val, extract(epoch from "updated_at") as "udatedAt" FROM meta WHERE name = '${name}'`;
42+
const queryForName = (name) => `SELECT name, val, extract(epoch from "updated_at") as "updatedAt" FROM meta WHERE name = '${name}'`;
4343
const updateForName = (name, val) => `INSERT INTO meta (name, val, "updated_at") values ('${name}', '${val}', now())
4444
ON conflict (name) do update set val = EXCLUDED.val, "updated_at" = now();`;
4545

app/lib/alimonitor-services/SyncManager.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,23 @@ class SyncManager {
4444
await this.services.monalisaService.setSyncTask();
4545
await this.services.monalisaServiceMC.setSyncTask();
4646
await this.setInDBSyncOk();
47+
this.logger.info('Synchronization completed');
4748
} catch {
4849
await this.setInDBSyncFailure();
4950
}
5051
}
5152

5253
async setSyncAllTask() {
53-
const syncPeriod = 12 * 60 * 60 * 1000; // Twice per day [ms]
54-
const last_sync = this.getLastSyncMetadata();
55-
const firstSyncDelay = last_sync.val === 'ok' ?
56-
syncPeriod - (Date.now() - (last_sync.updatedAt || 0) * 1000)
57-
: 1000;// Gives server 1 sec before sync task starts;
54+
const { syncPeriod } = config;
55+
const last_sync = await this.getLastSyncMetadata();
56+
const firstSyncDelay = last_sync?.val === 'ok' ?
57+
syncPeriod - (Date.now() - (last_sync?.updatedAt || 0) * 1000)
58+
: 3000;// Gives server 1 sec before sync task starts;
59+
const hours = Math.floor(firstSyncDelay / 60 / 60 / 1000);
60+
const minutes = Math.floor((firstSyncDelay - hours * 60 * 60 * 1000) / 60 / 1000);
61+
const seconds = Math.floor((firstSyncDelay - (hours * 60 + minutes) * 60 * 1000) / 1000);
62+
63+
this.logger.info(`Next synchronization in ${hours} h ${minutes} min ${seconds} sec`);
5864

5965
setTimeout(() => {
6066
this.syncAll();

docker/dev.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ RCT_JWT_EXPIRATION=${RCT_JWT_EXPIRATION:-2h}
1717

1818
## external services connections
1919
RCT_SYNC_TASK_AT_START=${RCT_SYNC_TASK_AT_START:-false}
20+
RCT_SYNC_PERIOD=${RCT_SYNC_PERIOD:-}
2021

2122
RCT_CERT_PATH=${RCT_CERT_PATH:-}
2223
ALIMONITOR_PASSPHRASE=${ALIMONITOR_PASSPHRASE:-}

0 commit comments

Comments
 (0)