From 0780e8dc792bf31a8e73c787b5c8ec1adb41a3c9 Mon Sep 17 00:00:00 2001 From: Alexander Rodionov Date: Tue, 10 Oct 2023 16:18:22 +0400 Subject: [PATCH 1/4] refactor: add system config background rotation --- src/Config/CliBuilder.ts | 47 +++++++++++-------- ...ConfigReader.ts => SystemConfigManager.ts} | 44 ++++++++++++++++- 2 files changed, 69 insertions(+), 22 deletions(-) rename src/Config/{SystemConfigReader.ts => SystemConfigManager.ts} (74%) diff --git a/src/Config/CliBuilder.ts b/src/Config/CliBuilder.ts index cc2d5463..2caa9b11 100644 --- a/src/Config/CliBuilder.ts +++ b/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'; @@ -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; + } + }); + } } diff --git a/src/Config/SystemConfigReader.ts b/src/Config/SystemConfigManager.ts similarity index 74% rename from src/Config/SystemConfigReader.ts rename to src/Config/SystemConfigManager.ts index e6e2c689..c398e9ee 100644 --- a/src/Config/SystemConfigReader.ts +++ b/src/Config/SystemConfigManager.ts @@ -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 isBackgroundRotationEnabled = false; constructor(baseUrl: string) { this.client = request.defaults({ @@ -37,6 +38,41 @@ export class SystemConfigReader { }; } + public enableBackgroundRotation(onRotation: (config: SystemConfig) => void) { + this.isBackgroundRotationEnabled = true; + + (async () => { + while (this.isBackgroundRotationEnabled) { + 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); + } + })().catch((e) => { + logger.debug('An error occurred during background rotation', e); + }); + } + + public disableBackgroundRotation() { + this.isBackgroundRotationEnabled = false; + } + + private sleep(ms: number) { + return new Promise((resolve) => setTimeout(resolve, ms).unref()); + } + private needsRotation(configFile: SystemConfigFile) { if (process.env.NODE_ENV !== 'production') { return; @@ -58,7 +94,7 @@ export class SystemConfigReader { configFile.updatedAt ); - return; + return false; } logger.debug( @@ -73,6 +109,8 @@ export class SystemConfigReader { data: newConfig, updatedAt: new Date() }); + + return true; } else { logger.debug('Rotation failed'); @@ -80,6 +118,8 @@ export class SystemConfigReader { ...configFile, updatedAt: new Date() }); + + return false; } } From d336178bf7ab3e31f1686349709fe86f8613c575 Mon Sep 17 00:00:00 2001 From: Alexander Rodionov Date: Wed, 11 Oct 2023 15:14:53 +0400 Subject: [PATCH 2/4] refactor: move debug log under if condition --- src/Config/SystemConfigManager.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Config/SystemConfigManager.ts b/src/Config/SystemConfigManager.ts index c398e9ee..429470e2 100644 --- a/src/Config/SystemConfigManager.ts +++ b/src/Config/SystemConfigManager.ts @@ -51,12 +51,12 @@ export class SystemConfigManager { const configFile = await this.getConfigFile(); onRotation(configFile.data); - } - logger.debug( - 'Background rotation is done, sleeping for %s ms', - this.rotationInterval - ); + logger.debug( + 'Background rotation is done, sleeping for %s ms', + this.rotationInterval + ); + } await this.sleep(this.rotationInterval); } From 2cb73e3ff51f28d398b4f493a4ba72b6dbc5098f Mon Sep 17 00:00:00 2001 From: Alexander Rodionov Date: Wed, 11 Oct 2023 15:45:50 +0400 Subject: [PATCH 3/4] refactor: extract run logic from `enableBackgroundRotation` method --- src/Config/SystemConfigManager.ts | 42 +++++++++++++++++-------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/src/Config/SystemConfigManager.ts b/src/Config/SystemConfigManager.ts index 429470e2..f8a1a468 100644 --- a/src/Config/SystemConfigManager.ts +++ b/src/Config/SystemConfigManager.ts @@ -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) { From b2fc13a31cef93d7bbe207b8665d80c0aa1c91bc Mon Sep 17 00:00:00 2001 From: Alexander Rodionov Date: Thu, 12 Oct 2023 11:55:54 +0400 Subject: [PATCH 4/4] refactor: rename `isBackgroundRotationEnabled` to `backgroundRotationEnabled` --- src/Config/SystemConfigManager.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Config/SystemConfigManager.ts b/src/Config/SystemConfigManager.ts index f8a1a468..1581a0d3 100644 --- a/src/Config/SystemConfigManager.ts +++ b/src/Config/SystemConfigManager.ts @@ -18,7 +18,7 @@ export class SystemConfigManager { private readonly rotationInterval = 3600000; private readonly path = join(homedir(), '.brightclirc'); private readonly client: RequestPromiseAPI; - private isBackgroundRotationEnabled = false; + private backgroundRotationEnabled = false; constructor(baseUrl: string) { this.client = request.defaults({ @@ -39,7 +39,7 @@ export class SystemConfigManager { } public enableBackgroundRotation(onRotation: (config: SystemConfig) => void) { - this.isBackgroundRotationEnabled = true; + this.backgroundRotationEnabled = true; this.runBackgroundRotation(onRotation).catch((e) => { logger.debug('An error occurred during background rotation', e); @@ -47,13 +47,13 @@ export class SystemConfigManager { } public disableBackgroundRotation() { - this.isBackgroundRotationEnabled = false; + this.backgroundRotationEnabled = false; } private async runBackgroundRotation( onRotation: (config: SystemConfig) => void ) { - while (this.isBackgroundRotationEnabled) { + while (this.backgroundRotationEnabled) { logger.debug('Performing background rotation of system config file'); const isRotated = await this.rotateIfNecessary();