forked from launchdarkly/node-server-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolling.js
More file actions
53 lines (43 loc) · 1.26 KB
/
Copy pathpolling.js
File metadata and controls
53 lines (43 loc) · 1.26 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
var noop = function(){};
function PollingProcessor(config, requestor) {
var processor = {},
store = config.feature_store,
stopped = false;
function poll(cb) {
var start_time, delta;
cb = cb || noop;
if (stopped) {
return;
}
start_time = new Date().getTime();
config.logger.debug("Polling LaunchDarkly for feature flag updates");
requestor.request_all_flags(function(err, flags) {
elapsed = new Date().getTime() - start_time;
sleepFor = Math.max(config.poll_interval * 1000 - elapsed, 0);
config.logger.debug("Elapsed: %d ms, sleeping for %d ms", elapsed, sleepFor);
if (err) {
config.logger.error("[LaunchDarkly] Error polling for all feature flags", err);
cb(err);
// Recursively call poll after the appropriate delay
setTimeout(poll, sleepFor);
} else {
store.init(flags, function() {
cb();
// Recursively call poll after the appropriate delay
setTimeout(poll, sleepFor);
});
}
});
};
processor.start = function(cb) {
poll(cb);
}
processor.stop = function() {
stopped = true;
}
processor.close = function() {
this.stop();
}
return processor;
}
module.exports = PollingProcessor;