mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2024-12-19 21:57:54 +00:00
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:
parent
4557644149
commit
2e81a7328e
@ -1,5 +1,6 @@
|
|||||||
import * as url from 'url';
|
import * as url from 'url';
|
||||||
import * as _ from 'lodash';
|
import * as _ from 'lodash';
|
||||||
|
import { delay } from 'bluebird';
|
||||||
import { CoreOptions } from 'request';
|
import { CoreOptions } from 'request';
|
||||||
|
|
||||||
import * as constants from '../lib/constants';
|
import * as constants from '../lib/constants';
|
||||||
@ -141,15 +142,24 @@ export async function startReporting() {
|
|||||||
);
|
);
|
||||||
const doReport = async () => {
|
const doReport = async () => {
|
||||||
if (!reportPending) {
|
if (!reportPending) {
|
||||||
throttledReport(reportConfigs);
|
await throttledReport(reportConfigs);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// If the state changes, report it
|
// If the state changes, report it
|
||||||
deviceState.on('change', doReport);
|
deviceState.on('change', doReport);
|
||||||
// But check once every max report frequency to ensure that changes in system
|
|
||||||
// info are picked up (CPU temp etc)
|
async function recursivelyReport(delayBy: number) {
|
||||||
setInterval(doReport, constants.maxReportFrequency);
|
try {
|
||||||
// Try to perform a report right away
|
// Try to send current state
|
||||||
return doReport();
|
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