Skip to content

Commit 0780e8d

Browse files
committed
refactor: add system config background rotation
1 parent 5cf2453 commit 0780e8d

File tree

2 files changed

+69
-22
lines changed

2 files changed

+69
-22
lines changed

src/Config/CliBuilder.ts

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { CliConfig, ConfigReader } from './ConfigReader';
22
import { ClusterArgs, Helpers, logger, LogLevel, Sentry } from '../Utils';
3-
import { SystemConfigReader } from './SystemConfigReader';
3+
import { SystemConfigManager } from './SystemConfigManager';
44
import { CliInfo } from './CliInfo';
55
import { Arguments, Argv, CommandModule } from 'yargs';
66

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

107107
command.handler = async (args: Arguments) => {
108-
const systemConfigReader = new SystemConfigReader(args.api as string);
109-
const systemConfig = await systemConfigReader.read();
110-
111-
Sentry.init({
112-
attachStacktrace: true,
113-
dsn: systemConfig.sentryDsn,
114-
release: process.env.VERSION,
115-
beforeSend(event) {
116-
if (event.contexts.args) {
117-
event.contexts.args = {
118-
...event.contexts.args,
119-
t: event.contexts.args.t && '[Filtered]',
120-
token: event.contexts.args.token && '[Filtered]'
121-
};
122-
}
123-
124-
return event;
125-
}
126-
});
108+
const systemConfigManager = new SystemConfigManager(args.api as string);
109+
const systemConfig = await systemConfigManager.read();
127110

128111
return Sentry.runWithAsyncContext(() => {
112+
this.initSentry(systemConfig.sentryDsn);
129113
Sentry.setContext('args', args);
130114

115+
systemConfigManager.enableBackgroundRotation((rotatedSystemConfig) => {
116+
this.initSentry(rotatedSystemConfig.sentryDsn);
117+
});
118+
131119
return handler(args);
132120
});
133121
};
134122

135123
return command;
136124
}
125+
126+
private initSentry(dsn: string) {
127+
Sentry.init({
128+
attachStacktrace: true,
129+
dsn,
130+
release: process.env.VERSION,
131+
beforeSend(event) {
132+
if (event.contexts.args) {
133+
event.contexts.args = {
134+
...event.contexts.args,
135+
t: event.contexts.args.t && '[Filtered]',
136+
token: event.contexts.args.token && '[Filtered]'
137+
};
138+
}
139+
140+
return event;
141+
}
142+
});
143+
}
137144
}

src/Config/SystemConfigReader.ts renamed to src/Config/SystemConfigManager.ts

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ interface SystemConfigFile {
1414
updatedAt: Date;
1515
}
1616

17-
export class SystemConfigReader {
17+
export class SystemConfigManager {
1818
private readonly rotationInterval = 3600000;
1919
private readonly path = join(homedir(), '.brightclirc');
2020
private readonly client: RequestPromiseAPI;
21+
private isBackgroundRotationEnabled = false;
2122

2223
constructor(baseUrl: string) {
2324
this.client = request.defaults({
@@ -37,6 +38,41 @@ export class SystemConfigReader {
3738
};
3839
}
3940

41+
public enableBackgroundRotation(onRotation: (config: SystemConfig) => void) {
42+
this.isBackgroundRotationEnabled = true;
43+
44+
(async () => {
45+
while (this.isBackgroundRotationEnabled) {
46+
logger.debug('Performing background rotation of system config file');
47+
48+
const isRotated = await this.rotateIfNecessary();
49+
50+
if (isRotated) {
51+
const configFile = await this.getConfigFile();
52+
53+
onRotation(configFile.data);
54+
}
55+
56+
logger.debug(
57+
'Background rotation is done, sleeping for %s ms',
58+
this.rotationInterval
59+
);
60+
61+
await this.sleep(this.rotationInterval);
62+
}
63+
})().catch((e) => {
64+
logger.debug('An error occurred during background rotation', e);
65+
});
66+
}
67+
68+
public disableBackgroundRotation() {
69+
this.isBackgroundRotationEnabled = false;
70+
}
71+
72+
private sleep(ms: number) {
73+
return new Promise((resolve) => setTimeout(resolve, ms).unref());
74+
}
75+
4076
private needsRotation(configFile: SystemConfigFile) {
4177
if (process.env.NODE_ENV !== 'production') {
4278
return;
@@ -58,7 +94,7 @@ export class SystemConfigReader {
5894
configFile.updatedAt
5995
);
6096

61-
return;
97+
return false;
6298
}
6399

64100
logger.debug(
@@ -73,13 +109,17 @@ export class SystemConfigReader {
73109
data: newConfig,
74110
updatedAt: new Date()
75111
});
112+
113+
return true;
76114
} else {
77115
logger.debug('Rotation failed');
78116

79117
await this.updateConfigFile({
80118
...configFile,
81119
updatedAt: new Date()
82120
});
121+
122+
return false;
83123
}
84124
}
85125

0 commit comments

Comments
 (0)