mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2024-12-21 22:47:49 +00:00
Merge pull request #1936 from balena-os/sleep-between-reports
Use delay instead of interval to recursively report state
This commit is contained in:
commit
01934503e5
@ -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);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user