1
- import {
2
- PROBE_INGEST_URL ,
3
- PROBE_MONITOR_FETCH_LIMIT ,
4
- PROBE_MONITORING_WORKERS ,
5
- } from "../../Config" ;
1
+ import { PROBE_INGEST_URL , PROBE_MONITOR_FETCH_LIMIT } from "../../Config" ;
6
2
import MonitorUtil from "../../Utils/Monitors/Monitor" ;
7
3
import ProbeAPIRequest from "../../Utils/ProbeAPIRequest" ;
8
4
import BaseModel from "Common/Models/DatabaseModels/DatabaseBaseModel/DatabaseBaseModel" ;
9
5
import HTTPErrorResponse from "Common/Types/API/HTTPErrorResponse" ;
10
6
import HTTPMethod from "Common/Types/API/HTTPMethod" ;
11
7
import HTTPResponse from "Common/Types/API/HTTPResponse" ;
12
8
import URL from "Common/Types/API/URL" ;
9
+ import OneUptimeDate from "Common/Types/Date" ;
13
10
import APIException from "Common/Types/Exception/ApiException" ;
14
11
import { JSONArray } from "Common/Types/JSON" ;
15
12
import ProbeMonitorResponse from "Common/Types/Probe/ProbeMonitorResponse" ;
13
+ import Sleep from "Common/Types/Sleep" ;
16
14
import API from "Common/Utils/API" ;
17
15
import logger from "Common/Server/Utils/Logger" ;
18
16
import Monitor from "Common/Models/DatabaseModels/Monitor" ;
19
- import { EVERY_MINUTE } from "Common/Utils/CronTime" ;
20
- import BasicCron from "Common/Server/Utils/BasicCron" ;
21
-
22
- BasicCron ( {
23
- jobName : "Probe:MonitorFetchList" ,
24
- options : {
25
- schedule : EVERY_MINUTE ,
26
- runOnStartup : true ,
27
- } ,
28
- runFunction : async ( ) => {
29
- try {
30
- let workers : number = 0 ;
31
-
32
- while ( workers < PROBE_MONITORING_WORKERS ) {
33
- workers ++ ;
34
-
35
- const currentWorker : number = workers ;
36
-
37
- logger . debug ( `Starting worker ${ currentWorker } ` ) ;
38
-
39
- new FetchListAndProbe ( "Worker " + currentWorker )
40
- . run ( )
41
- . catch ( ( err : any ) => {
42
- logger . error ( `Worker ${ currentWorker } failed: ` ) ;
43
- logger . error ( err ) ;
44
- } ) ;
45
- }
46
- } catch ( err ) {
47
- logger . error ( "Starting workers failed" ) ;
48
- logger . error ( err ) ;
49
- }
50
- } ,
51
- } ) ;
52
17
53
18
export default class FetchListAndProbe {
54
19
private workerName : string = "" ;
@@ -60,15 +25,41 @@ export default class FetchListAndProbe {
60
25
public async run ( ) : Promise < void > {
61
26
logger . debug ( `Running worker ${ this . workerName } ` ) ;
62
27
63
- try {
64
- logger . debug ( `Probing monitors ${ this . workerName } ` ) ;
28
+ // eslint-disable-next-line no-constant-condition
29
+ while ( true ) {
30
+ try {
31
+ // Sleep randomly between 1 and 5 seconds.
32
+ // We do this to avoid all workers hitting the server at the same time and fetching the same monitors.
33
+ const sleepTime : number = Math . floor ( Math . random ( ) * 5000 ) + 1000 ;
34
+ logger . debug (
35
+ `Worker ${ this . workerName } is sleeping for ${ sleepTime } seconds` ,
36
+ ) ;
37
+ await Sleep . sleep ( Math . round ( sleepTime ) % 5000 ) ;
65
38
66
- await this . fetchListAndProbe ( ) ;
39
+ const runTime : Date = OneUptimeDate . getCurrentDate ( ) ;
67
40
68
- logger . debug ( `Probing monitors ${ this . workerName } complete` ) ;
69
- } catch ( err ) {
70
- logger . error ( `Error in worker ${ this . workerName } ` ) ;
71
- logger . error ( err ) ;
41
+ logger . debug ( `Probing monitors ${ this . workerName } ` ) ;
42
+
43
+ await this . fetchListAndProbe ( ) ;
44
+
45
+ logger . debug ( `Probing monitors ${ this . workerName } complete` ) ;
46
+
47
+ // if rumTime + 5 seconds is in the future, then this fetchLst either errored out or had no monitors in the list. Either way, wait for 5 seconds and proceed.
48
+
49
+ const twoSecondsAdded : Date = OneUptimeDate . addRemoveSeconds (
50
+ runTime ,
51
+ 2 ,
52
+ ) ;
53
+
54
+ if ( OneUptimeDate . isInTheFuture ( twoSecondsAdded ) ) {
55
+ logger . debug ( `Worker ${ this . workerName } is waiting for 2 seconds` ) ;
56
+ await Sleep . sleep ( 2000 ) ;
57
+ }
58
+ } catch ( err ) {
59
+ logger . error ( `Error in worker ${ this . workerName } ` ) ;
60
+ logger . error ( err ) ;
61
+ await Sleep . sleep ( 2000 ) ;
62
+ }
72
63
}
73
64
}
74
65
0 commit comments