Use delay instead of interval to recursively report state

Change-type: patch
Signed-off-by: 20k-ultra <3946250+20k-ultra@users.noreply.github.com>
This commit is contained in:
20k-ultra 2022-04-27 00:44:00 -04:00
parent 4557644149
commit 2e81a7328e

View File

@ -1,5 +1,6 @@
import * as url from 'url';
import * as _ from 'lodash';
import { delay } from 'bluebird';
import { CoreOptions } from 'request';
import * as constants from '../lib/constants';
@ -141,15 +142,24 @@ export async function startReporting() {
);
const doReport = async () => {
if (!reportPending) {
throttledReport(reportConfigs);
await throttledReport(reportConfigs);
}
};
// If the state changes, report it
deviceState.on('change', doReport);
// But check once every max report frequency to ensure that changes in system
// info are picked up (CPU temp etc)
setInterval(doReport, constants.maxReportFrequency);
// Try to perform a report right away
return doReport();
async function recursivelyReport(delayBy: number) {
try {
// Try to send current state
await doReport();
} finally {
// Wait until we want to report again
await delay(delayBy);
// Try to report again
await recursivelyReport(delayBy);
}
}
return recursivelyReport(constants.maxReportFrequency);
}