Skip to content

refactor: add system config background rotation #470

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 27 additions & 20 deletions src/Config/CliBuilder.ts
Original file line number Diff line number Diff line change
@@ -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;
}
});
}
}
Original file line number Diff line number Diff line change
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