From e947d21060212f4c98341f583830e33caceca007 Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Wed, 4 Dec 2024 15:57:02 +0000 Subject: [PATCH] Add random sleep interval to FetchList worker to reduce server load --- Probe/Jobs/Monitor/FetchList.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Probe/Jobs/Monitor/FetchList.ts b/Probe/Jobs/Monitor/FetchList.ts index 57aba37689..3ffebbb53c 100644 --- a/Probe/Jobs/Monitor/FetchList.ts +++ b/Probe/Jobs/Monitor/FetchList.ts @@ -28,6 +28,14 @@ export default class FetchListAndProbe { // eslint-disable-next-line no-constant-condition while (true) { try { + // Sleep randomly between 1 and 5 seconds. + // We do this to avoid all workers hitting the server at the same time and fetching the same monitors. + const sleepTime: number = Math.floor(Math.random() * 5000) + 1000; + logger.debug( + `Worker ${this.workerName} is sleeping for ${sleepTime} seconds`, + ); + await Sleep.sleep(Math.round(sleepTime) % 5000); + const runTime: Date = OneUptimeDate.getCurrentDate(); logger.debug(`Probing monitors ${this.workerName}`);