|
|
@@ -7,20 +7,23 @@ import { Timer } from './timer.class';
|
|
|
export class ServerConnector {
|
|
|
private subscriptions: Array<{ id: number; interval: number; server: Server }> = [];
|
|
|
|
|
|
- constructor(private db: Database) {
|
|
|
+ constructor(db: Database) {
|
|
|
(async () => {
|
|
|
try {
|
|
|
const serverList = await db.getAllServerConfigs();
|
|
|
|
|
|
- serverList.forEach(server => {
|
|
|
+ serverList.forEach(async server => {
|
|
|
+ const serverDB = new Database();
|
|
|
+ await serverDB.open();
|
|
|
+
|
|
|
const interval = Number(server.config['syncInterval'] ?? '300');
|
|
|
Logger.info('[INFO] Starting Server Sync Connector for', server.title, 'with interval', interval, 'seconds ...');
|
|
|
- const id = Timer.instance.subscribe(interval, () => this.timerTick(server));
|
|
|
+ const id = Timer.instance.subscribe(interval, () => this.timerTick(server, serverDB));
|
|
|
this.subscriptions.push({ id, interval, server });
|
|
|
|
|
|
process.nextTick(async () => {
|
|
|
Logger.info('[INFO] Initial Sync for', server.title, '...');
|
|
|
- await this.timerTick(server);
|
|
|
+ await this.timerTick(server, serverDB);
|
|
|
Logger.info('[SUCCESS] Initial Sync for', server.title, 'succeeded.');
|
|
|
});
|
|
|
});
|
|
|
@@ -32,7 +35,7 @@ export class ServerConnector {
|
|
|
})();
|
|
|
}
|
|
|
|
|
|
- private async timerTick(server: Server) {
|
|
|
+ private async timerTick(server: Server, db: Database) {
|
|
|
Logger.debug('[DEBUG] TICK', new Date(), JSON.stringify(server));
|
|
|
|
|
|
let trxHdl: number | undefined = undefined;
|
|
|
@@ -46,7 +49,7 @@ export class ServerConnector {
|
|
|
const data: ReducedData[] = response.data.data.map((entry: any) => ({ ...entry, time: new Date(entry.time) }));
|
|
|
|
|
|
// Process data in DB
|
|
|
- await this.db.insertServerData(server.id, data);
|
|
|
+ await db.insertServerData(server.id, data);
|
|
|
|
|
|
// Commit Transaction
|
|
|
await axios.patch(`http://${server.fqdn}:8890/${trxHdl}`, null, { responseType: 'json' });
|
|
|
@@ -60,6 +63,8 @@ export class ServerConnector {
|
|
|
Logger.error(`[WARN] Rollback succeeded.`);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ // if (!!db) await db.close();
|
|
|
}
|
|
|
}
|
|
|
}
|