Skip to content

Commit

Permalink
refactor: extract run logic from enableBackgroundRotation method
Browse files Browse the repository at this point in the history
  • Loading branch information
lsndr committed Oct 11, 2023
1 parent 1ebd003 commit 2cb73e3
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions src/Config/SystemConfigManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,32 +41,36 @@ export class SystemConfigManager {
public enableBackgroundRotation(onRotation: (config: SystemConfig) => void) {
this.isBackgroundRotationEnabled = true;

(async () => {
while (this.isBackgroundRotationEnabled) {
logger.debug('Performing background rotation of system config file');
this.runBackgroundRotation(onRotation).catch((e) => {
logger.debug('An error occurred during background rotation', e);
});
}

public disableBackgroundRotation() {
this.isBackgroundRotationEnabled = false;
}

const isRotated = await this.rotateIfNecessary();
private async runBackgroundRotation(
onRotation: (config: SystemConfig) => void
) {
while (this.isBackgroundRotationEnabled) {
logger.debug('Performing background rotation of system config file');

if (isRotated) {
const configFile = await this.getConfigFile();
const isRotated = await this.rotateIfNecessary();

onRotation(configFile.data);
if (isRotated) {
const configFile = await this.getConfigFile();

logger.debug(
'Background rotation is done, sleeping for %s ms',
this.rotationInterval
);
}
onRotation(configFile.data);

await this.sleep(this.rotationInterval);
logger.debug(
'Background rotation is done, sleeping for %s ms',
this.rotationInterval
);
}
})().catch((e) => {
logger.debug('An error occurred during background rotation', e);
});
}

public disableBackgroundRotation() {
this.isBackgroundRotationEnabled = false;
await this.sleep(this.rotationInterval);
}
}

private sleep(ms: number) {
Expand Down

0 comments on commit 2cb73e3

Please sign in to comment.