|
@@ -9,7 +9,6 @@ import { Logger } from '../../../common/util/logger.class';
|
|
|
import { ServiceChangedStatus } from '../lib/service-changed-status.enum';
|
|
import { ServiceChangedStatus } from '../lib/service-changed-status.enum';
|
|
|
import { Timer } from '../timer.class';
|
|
import { Timer } from '../timer.class';
|
|
|
import { FCMController } from './fcm-controller.class';
|
|
import { FCMController } from './fcm-controller.class';
|
|
|
-import { HealthCheckDataProvider } from './health-check-data-provider.interface';
|
|
|
|
|
import { MariaDBDatabase } from './mariadb-database.class';
|
|
import { MariaDBDatabase } from './mariadb-database.class';
|
|
|
|
|
|
|
|
type Subscriber = { id: number; interval: number; conf: HttpCheckConfig };
|
|
type Subscriber = { id: number; interval: number; conf: HttpCheckConfig };
|
|
@@ -17,14 +16,13 @@ type ContentCheckError = { type: 'contentCheck'; status: HttpCheckStatus; messag
|
|
|
|
|
|
|
|
export class HttpCheckController {
|
|
export class HttpCheckController {
|
|
|
private subscriptions: Array<Subscriber> = [];
|
|
private subscriptions: Array<Subscriber> = [];
|
|
|
- private db!: MariaDBDatabase;
|
|
|
|
|
|
|
|
|
|
- constructor(pool: Pool) {
|
|
|
|
|
- this.db = new MariaDBDatabase(pool);
|
|
|
|
|
|
|
+ constructor(private pool: Pool) {
|
|
|
|
|
+ const db = new MariaDBDatabase(pool);
|
|
|
(async () => {
|
|
(async () => {
|
|
|
try {
|
|
try {
|
|
|
- await this.db.open();
|
|
|
|
|
- const configs = await this.db.getHttpCheckConfigs();
|
|
|
|
|
|
|
+ await db.open();
|
|
|
|
|
+ const configs = await db.getHttpCheckConfigs();
|
|
|
|
|
|
|
|
for (const conf of configs) {
|
|
for (const conf of configs) {
|
|
|
if (!conf) continue;
|
|
if (!conf) continue;
|
|
@@ -33,12 +31,16 @@ export class HttpCheckController {
|
|
|
this.scheduleCheck(conf);
|
|
this.scheduleCheck(conf);
|
|
|
|
|
|
|
|
Logger.info('[INFO] Initial HTTP Service Check for', conf.title, '...');
|
|
Logger.info('[INFO] Initial HTTP Service Check for', conf.title, '...');
|
|
|
- await this.runCheck(conf, this.db);
|
|
|
|
|
|
|
+ await this.runCheck(conf);
|
|
|
}
|
|
}
|
|
|
} catch (err) {
|
|
} catch (err) {
|
|
|
Logger.error('[FATAL] Initializing HttpCheckController failed:', err);
|
|
Logger.error('[FATAL] Initializing HttpCheckController failed:', err);
|
|
|
Logger.error('[EXITING]');
|
|
Logger.error('[EXITING]');
|
|
|
process.exit(1);
|
|
process.exit(1);
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ try {
|
|
|
|
|
+ await db.close();
|
|
|
|
|
+ } catch (e) {}
|
|
|
}
|
|
}
|
|
|
})();
|
|
})();
|
|
|
}
|
|
}
|
|
@@ -67,7 +69,7 @@ export class HttpCheckController {
|
|
|
if (Number.isNaN(interval)) interval = defaults.serviceChecks.interval;
|
|
if (Number.isNaN(interval)) interval = defaults.serviceChecks.interval;
|
|
|
|
|
|
|
|
if (log) Logger.info(`[INFO] Starting HTTP Service Check Controller for "${conf.title}" with interval ${interval} seconds ...`);
|
|
if (log) Logger.info(`[INFO] Starting HTTP Service Check Controller for "${conf.title}" with interval ${interval} seconds ...`);
|
|
|
- const id = Timer.instance.subscribe(interval, async () => await this.runCheck(conf, this.db));
|
|
|
|
|
|
|
+ const id = Timer.instance.subscribe(interval, async () => await this.runCheck(conf));
|
|
|
const sub = { id, interval, conf };
|
|
const sub = { id, interval, conf };
|
|
|
this.subscriptions.push(sub);
|
|
this.subscriptions.push(sub);
|
|
|
return sub;
|
|
return sub;
|
|
@@ -87,7 +89,7 @@ export class HttpCheckController {
|
|
|
this.subscriptions = this.subscriptions.filter(s => s.id !== sub.id);
|
|
this.subscriptions = this.subscriptions.filter(s => s.id !== sub.id);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- public async runCheck(conf: HttpCheckConfig, db: HealthCheckDataProvider) {
|
|
|
|
|
|
|
+ public async runCheck(conf: HttpCheckConfig) {
|
|
|
Logger.debug('[DEBUG] TICK', new Date(), JSON.stringify(conf));
|
|
Logger.debug('[DEBUG] TICK', new Date(), JSON.stringify(conf));
|
|
|
|
|
|
|
|
const now = new Date();
|
|
const now = new Date();
|
|
@@ -96,7 +98,11 @@ export class HttpCheckController {
|
|
|
responseType: 'text'
|
|
responseType: 'text'
|
|
|
};
|
|
};
|
|
|
let success = true;
|
|
let success = true;
|
|
|
|
|
+
|
|
|
|
|
+ const db = new MariaDBDatabase(this.pool);
|
|
|
try {
|
|
try {
|
|
|
|
|
+ await db.open();
|
|
|
|
|
+
|
|
|
const id = conf.id;
|
|
const id = conf.id;
|
|
|
conf = (await db.getHttpCheckConfigByID(conf.serverId ?? 0, id)) as HttpCheckConfig;
|
|
conf = (await db.getHttpCheckConfigByID(conf.serverId ?? 0, id)) as HttpCheckConfig;
|
|
|
|
|
|
|
@@ -160,7 +166,12 @@ export class HttpCheckController {
|
|
|
log = true;
|
|
log = true;
|
|
|
}
|
|
}
|
|
|
if (log) Logger.error('[ERROR] HTTP Service Check failed:', err);
|
|
if (log) Logger.error('[ERROR] HTTP Service Check failed:', err);
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ try {
|
|
|
|
|
+ await db.close();
|
|
|
|
|
+ } catch (e) {}
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
if (!success && conf.notify && !process.env.DEV_MODE) {
|
|
if (!success && conf.notify && !process.env.DEV_MODE) {
|
|
|
try {
|
|
try {
|
|
|
const lastErrors = await db.getLastErrors(conf.id, conf.notifyThreshold + 1);
|
|
const lastErrors = await db.getLastErrors(conf.id, conf.notifyThreshold + 1);
|
|
@@ -240,9 +251,4 @@ export class HttpCheckController {
|
|
|
}
|
|
}
|
|
|
Logger.debug(`[DEBUG] RegExp check /${check}/i successful ✔︎`);
|
|
Logger.debug(`[DEBUG] RegExp check /${check}/i successful ✔︎`);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- async close() {
|
|
|
|
|
- if (!this.db) return;
|
|
|
|
|
- await this.db.close();
|
|
|
|
|
- }
|
|
|
|
|
}
|
|
}
|