-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathpolling_manager.ts
36 lines (32 loc) · 1.03 KB
/
polling_manager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import Flagsmith from './index.js';
import { Logger } from 'pino';
export class EnvironmentDataPollingManager {
private interval?: NodeJS.Timeout;
private main: Flagsmith;
private refreshIntervalSeconds: number;
private logger: Logger;
constructor(main: Flagsmith, refreshIntervalSeconds: number, logger: Logger) {
this.main = main;
this.refreshIntervalSeconds = refreshIntervalSeconds;
this.logger = logger;
}
start() {
const updateEnvironment = () => {
if (this.interval) clearInterval(this.interval);
this.interval = setInterval(async () => {
try {
await this.main.updateEnvironment();
} catch (error) {
this.logger.error(error, 'failed to poll environment');
}
}, this.refreshIntervalSeconds * 1000);
};
updateEnvironment();
}
stop() {
if (!this.interval) {
return;
}
clearInterval(this.interval);
}
}