Skip to content

Commit

Permalink
refactor: add system config background rotation (#470)
Browse files Browse the repository at this point in the history
relates-to: #466
  • Loading branch information
lsndr committed Oct 12, 2023
1 parent a6dc8ca commit 4268892
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 22 deletions.
47 changes: 27 additions & 20 deletions src/Config/CliBuilder.ts
@@ -1,6 +1,6 @@
import { CliConfig, ConfigReader } from './ConfigReader';
import { ClusterArgs, Helpers, logger, LogLevel, Sentry } from '../Utils';
import { SystemConfigReader } from './SystemConfigReader';
import { SystemConfigManager } from './SystemConfigManager';
import { CliInfo } from './CliInfo';
import { Arguments, Argv, CommandModule } from 'yargs';

Expand Down Expand Up @@ -105,33 +105,40 @@ export class CliBuilder {
const handler = command.handler.bind(command);

command.handler = async (args: Arguments) => {
const systemConfigReader = new SystemConfigReader(args.api as string);
const systemConfig = await systemConfigReader.read();

Sentry.init({
attachStacktrace: true,
dsn: systemConfig.sentryDsn,
release: process.env.VERSION,
beforeSend(event) {
if (event.contexts.args) {
event.contexts.args = {
...event.contexts.args,
t: event.contexts.args.t && '[Filtered]',
token: event.contexts.args.token && '[Filtered]'
};
}

return event;
}
});
const systemConfigManager = new SystemConfigManager(args.api as string);
const systemConfig = await systemConfigManager.read();

return Sentry.runWithAsyncContext(() => {
this.initSentry(systemConfig.sentryDsn);
Sentry.setContext('args', args);

systemConfigManager.enableBackgroundRotation((rotatedSystemConfig) => {
this.initSentry(rotatedSystemConfig.sentryDsn);
});

return handler(args);
});
};

return command;
}

private initSentry(dsn: string) {
Sentry.init({
attachStacktrace: true,
dsn,
release: process.env.VERSION,
beforeSend(event) {
if (event.contexts.args) {
event.contexts.args = {
...event.contexts.args,
t: event.contexts.args.t && '[Filtered]',
token: event.contexts.args.token && '[Filtered]'
};
}

return event;
}
});
}
}
Expand Up @@ -14,10 +14,11 @@ interface SystemConfigFile {
updatedAt: Date;
}

export class SystemConfigReader {
export class SystemConfigManager {
private readonly rotationInterval = 3600000;
private readonly path = join(homedir(), '.brightclirc');
private readonly client: RequestPromiseAPI;
private backgroundRotationEnabled = false;

constructor(baseUrl: string) {
this.client = request.defaults({
Expand All @@ -37,6 +38,45 @@ export class SystemConfigReader {
};
}

public enableBackgroundRotation(onRotation: (config: SystemConfig) => void) {
this.backgroundRotationEnabled = true;

this.runBackgroundRotation(onRotation).catch((e) => {
logger.debug('An error occurred during background rotation', e);
});
}

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

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

const isRotated = await this.rotateIfNecessary();

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

onRotation(configFile.data);

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

await this.sleep(this.rotationInterval);
}
}

private sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms).unref());
}

private needsRotation(configFile: SystemConfigFile) {
if (process.env.NODE_ENV !== 'production') {
return;
Expand All @@ -58,7 +98,7 @@ export class SystemConfigReader {
configFile.updatedAt
);

return;
return false;
}

logger.debug(
Expand All @@ -73,13 +113,17 @@ export class SystemConfigReader {
data: newConfig,
updatedAt: new Date()
});

return true;
} else {
logger.debug('Rotation failed');

await this.updateConfigFile({
...configFile,
updatedAt: new Date()
});

return false;
}
}

Expand Down

0 comments on commit 4268892

Please sign in to comment.