Skip to content
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

Reload Interval Feature #52

Closed
Closed
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
3 changes: 3 additions & 0 deletions examples/service-esm-ts/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@
"swc": true,
"esm": true
},
"compilerOptions": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure this part is needed? I don't remember having to do this when I added type: module in the secrets manager plugin.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without that, I get this error

Screenshot 2024-01-26 at 9 41 23 AM

I think the reason is because the @tsconfig/node18 which we are extending from has this compilerOptions configuration

"compilerOptions": {
    "lib": ["es2022"],
    "module": "commonjs", <--- Maybe this config here is the culprit? 
    "target": "es2022",

    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "moduleResolution": "node"
  }

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The /example/script-esm-ts is actually with the same problem. If I don't change the tsconfig file I get that same error.

"module": "ES2022"
},
"include": ["./src/**/*"]
}
31 changes: 23 additions & 8 deletions packages/config-dug/src/config-dug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ class ConfigDug<T extends ConfigDugSchema> extends EventEmitter {
private rawValues: UntypedConfig = {};
private validatedValues: ConfigDugConfig<T> | undefined;
private valueOrigins: ValueOrigins = {};
private reloadTimeout?: NodeJS.Timeout;
private pluginsInitialized = false;
private loaded = false;

Expand Down Expand Up @@ -156,11 +155,13 @@ class ConfigDug<T extends ConfigDugSchema> extends EventEmitter {
this.pluginsInitialized = true;
}

const { values: pluginValues, nextReloadIn } = await this.loadPlugins();

this.valueOrigins = {};
this.rawValues = {
...(await this.loadConfigFile('config.default')),
...(await this.loadConfigFile(`config.${environmentName}`)),
...(await this.loadPlugins()),
...pluginValues,
...(await this.loadLocalConfigFile(`config.${environmentName}.local`)),
...(await this.loadLocalConfigFile('config.local')),
...this.loadEnvironment(Object.keys(this.schema)),
Expand All @@ -179,9 +180,22 @@ class ConfigDug<T extends ConfigDugSchema> extends EventEmitter {

debug('load validated values', this.validatedValues);

this.setReloadInterval(nextReloadIn);

this.loaded = true;
}

private setReloadInterval(nextReloadIn: number): void {
const { minReloadInterval } = this.options;

setTimeout(
async () => {
await this.reload();
},
nextReloadIn >= minReloadInterval ? nextReloadIn : minReloadInterval
);
}

private async loadConfigFile(filename: string): Promise<UntypedConfig> {
if (this.options.loadConfigFiles) {
debug('load config file', filename);
Expand Down Expand Up @@ -245,8 +259,9 @@ class ConfigDug<T extends ConfigDugSchema> extends EventEmitter {
}
}

private async loadPlugins(): Promise<UntypedConfig> {
private async loadPlugins(): Promise<{ values: UntypedConfig; nextReloadIn: number }> {
let values: UntypedConfig = {};
let nextReloadIn: number = this.options.reloadInterval;

for (const plugin of this.options.plugins) {
const pluginReturnValue: ConfigDugPluginOutput = await plugin.load();
Expand All @@ -255,16 +270,16 @@ class ConfigDug<T extends ConfigDugSchema> extends EventEmitter {

this.valueOrigins = mergeOrigins(this.valueOrigins, pluginReturnValue.valueOrigins);

if (pluginReturnValue.nextReloadIn) {
this.reloadTimeout = setTimeout(async () => {
await this.reload();
}, pluginReturnValue.nextReloadIn);
const pluginNextReloadIn = pluginReturnValue.nextReloadIn;

if (pluginNextReloadIn && pluginNextReloadIn < nextReloadIn) {
nextReloadIn = pluginNextReloadIn;
}
}

debug('plugin values', values);

return values;
return { values, nextReloadIn };
}

private notLoadedError(): Error {
Expand Down
4 changes: 4 additions & 0 deletions packages/config-dug/src/lib/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const optionsSchema = z
printConfig: z.boolean().optional(),
strict: z.boolean().optional(),
warnOnLocalConfigFile: z.boolean().optional(),
reloadInterval: z.number().optional(),
minReloadInterval: z.number().optional(),
})
.strict();

Expand All @@ -26,6 +28,8 @@ const optionsWithDefaultsSchema = z.object({
printConfig: z.boolean().default(false),
strict: z.boolean().default(false),
warnOnLocalConfigFile: z.boolean().default(true),
reloadInterval: z.number().default(60000),
minReloadInterval: z.number().default(10000),
});

const getOptions = (options: ConfigDugOptions): ConfigDugOptionsWithDefaults => {
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-aws-secrets-manager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "1.0.0-alpha.0",
"author": "Neo Financial Engineering <engineering@neofinancial.com>",
"license": "MIT",
"type": "module",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could have sworn that I committed this change

"engines": {
"node": ">=18.0.0"
},
Expand Down
Loading