Jelajahi Sumber

WIP: testing / bugfixing dis/conjunctive http content checks

Christian Kahlau 2 tahun lalu
induk
melakukan
8f59d4db26

+ 6 - 4
server/src/ctrl/database.class.ts

@@ -350,8 +350,9 @@ export class Database extends SQLiteController {
     try {
       if (oldConf) {
         // UPDATE
+        Logger.debug('[DEBUG] Updating HealthCheckConfig', conf.title, `(${oldConf.id})`);
         if (oldConf.title !== conf.title) {
-          await this.stmt('UPDATE HealthCheckConfig SET Title = ?', [conf.title]);
+          await this.stmt('UPDATE HealthCheckConfig SET Title = ? WHERE ID = ?', [conf.title, oldConf.id]);
         }
 
         let updValues: any[][] = [];
@@ -379,7 +380,7 @@ export class Database extends SQLiteController {
         const delIDs: number[] = [];
         res.rows.forEach((row, i) => {
           if (i < conf.checks.length) {
-            updValues.push([conf.checks[i], row['ID']]);
+            updValues.push([JSON.stringify(conf.checks[i]), row['ID']]);
           } else {
             delIDs.push(row['ID']);
           }
@@ -395,7 +396,7 @@ export class Database extends SQLiteController {
             await this.run('UPDATE HealthCheckParams SET Value = ? WHERE ID = ?;', data);
           }
         }
-        const insValues = conf.checks.filter((c, i) => i > res.rows.length - 1).map(c => [conf.id, 'regexp', 'check', c]);
+        const insValues = conf.checks.filter((c, i) => i > res.rows.length - 1).map(c => [conf.id, 'regexp', 'check', JSON.stringify(c)]);
         if (insValues.length) {
           for (const data of insValues) {
             await this.run('INSERT INTO HealthCheckParams(ConfigID, Type, Key, Value) VALUES(?, ?, ?, ?);', data);
@@ -403,6 +404,7 @@ export class Database extends SQLiteController {
         }
       } else {
         // INSERT
+        Logger.debug('[DEBUG] Inserting new HealthCheckConfig', conf.title);
         const res = await this.run('INSERT INTO HealthCheckConfig(ServerID, Type, Title) VALUES(?, ?, ?);', [serverID, 'http', conf.title]);
         conf.id = res.lastID;
         if (conf.active ?? defaults.serviceChecks.active) {
@@ -425,7 +427,7 @@ export class Database extends SQLiteController {
             ...[res.lastID, 'number', 'timeout', conf.timeout ?? defaults.serviceChecks.httpTimeout],
             ...[res.lastID, 'boolean', 'notify', conf.notify ?? defaults.serviceChecks.notify],
             ...[res.lastID, 'number', 'notifyThreshold', conf.notifyThreshold ?? defaults.serviceChecks.notifyThreshold],
-            ...conf.checks.reduce((ret, check) => [...ret, res.lastID, 'regexp', 'check', check], [] as any[])
+            ...conf.checks.reduce((ret, check) => [...ret, res.lastID, 'regexp', 'check', JSON.stringify(check)], [] as any[])
           ]
         );
       }

+ 18 - 13
server/src/ctrl/http-check-controller.class.ts

@@ -24,10 +24,10 @@ export class HttpCheckController {
         const configs = await this.db.getHttpCheckConfigs();
 
         for (const conf of configs) {
-          if (!conf) return;
-          if (!conf.active) return;
+          if (!conf) continue;
+          if (!conf.active) continue;
 
-          await this.scheduleCheck(conf);
+          this.scheduleCheck(conf);
 
           Logger.info('[INFO] Initial HTTP Service Check for', conf.title, '...');
           await this.timerTick(conf);
@@ -40,26 +40,26 @@ export class HttpCheckController {
     })();
   }
 
-  async updateCheck(status: ServiceChangedStatus, conf: HttpCheckConfig) {
+  updateCheck(status: ServiceChangedStatus, conf: HttpCheckConfig) {
     const subscriber = this.subscriptions.find(sub => sub.conf.id === conf.id);
 
     switch (status) {
       case ServiceChangedStatus.Created:
       case ServiceChangedStatus.Activated:
-        await this.scheduleCheck(conf);
+        this.scheduleCheck(conf);
         break;
       case ServiceChangedStatus.Deactivated:
-        await this.unscheduleCheck(subscriber);
+        this.unscheduleCheck(subscriber);
         break;
       case ServiceChangedStatus.Rescheduled:
-        await this.rescheduleCheck(conf, subscriber);
+        this.rescheduleCheck(conf, subscriber);
         break;
       default:
         break;
     }
   }
 
-  private async scheduleCheck(conf: HttpCheckConfig, log = true) {
+  private scheduleCheck(conf: HttpCheckConfig, log = true) {
     let interval = Number(conf.interval);
     if (Number.isNaN(interval)) interval = defaults.serviceChecks.interval;
 
@@ -70,13 +70,13 @@ export class HttpCheckController {
     return sub;
   }
 
-  private async rescheduleCheck(conf: HttpCheckConfig, sub?: Subscriber) {
+  private rescheduleCheck(conf: HttpCheckConfig, sub?: Subscriber) {
     Logger.info('[INFO] Rescheduling HTTP Service Check for', conf.title);
-    await this.unscheduleCheck(sub, false);
-    await this.scheduleCheck(conf, false);
+    this.unscheduleCheck(sub, false);
+    this.scheduleCheck(conf, false);
   }
 
-  private async unscheduleCheck(sub?: Subscriber, log = true) {
+  private unscheduleCheck(sub?: Subscriber, log = true) {
     if (!sub) return;
 
     if (log) Logger.info('[INFO] Removing HTTP Service Check for', sub.conf.title);
@@ -180,6 +180,7 @@ export class HttpCheckController {
 
   private recurseDisjunctChecks(checks: CheckDisjunction, responseText: string): ContentCheckError[] {
     const errorBuffer: ContentCheckError[] = [];
+    Logger.debug(`[DEBUG] Processing ${checks.length} disjunctive checks ...`);
     for (const check of checks) {
       const errors: ContentCheckError[] = [];
       if (typeof check === 'string') {
@@ -202,11 +203,13 @@ export class HttpCheckController {
         return [];
       }
     }
+    Logger.debug(`[DEBUG] All disjunctive checks failed, collected ${errorBuffer.length} errors`);
     return errorBuffer;
   }
 
   private recurseConjunctChecks(check: CheckConjunction, responseText: string): ContentCheckError[] {
     const errorBuffer: ContentCheckError[] = [];
+    Logger.debug(`[DEBUG] Processing ${check.and.length} conjunctive checks ...`);
     for (const con of check.and) {
       try {
         if (typeof con === 'string') {
@@ -222,15 +225,17 @@ export class HttpCheckController {
         } else throw error;
       }
     }
+    Logger.debug(`[DEBUG] Ran through conjunctive checks, collected ${errorBuffer.length} errors`);
     return errorBuffer;
   }
 
-  private async doCheck(check: string, responseText: string) {
+  private doCheck(check: string, responseText: string) {
     const reg = new RegExp(check, 'i');
     if (!reg.test(responseText)) {
       Logger.debug(`[DEBUG] Regular expression /${check}/i not found in response`);
       throw { type: 'contentCheck', status: HttpCheckStatus.CheckFailed, message: `Regular expression /${check}/i not found in response` };
     }
+    Logger.debug(`[DEBUG] RegExp check /${check}/i successful ✔︎`);
   }
 
   async close() {