Skip to content

Commit

Permalink
feat: adds more lifecycle debug logs
Browse files Browse the repository at this point in the history
  • Loading branch information
Figedi committed Aug 19, 2023
1 parent 0f1e006 commit 1a85e3a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@figedi/svc-config": "0.1.0",
"@google-cloud/kms": "^3.7.0",
"@kubernetes/client-node": "1.0.0-rc2",
"@types/debug": "^4.1.8",
"@types/chai": "^4.3.5",
"@types/mocha": "^10.0.1",
"@types/node": "^20.4.4",
Expand Down Expand Up @@ -66,6 +67,7 @@
"ws": "^8.8.1"
},
"dependencies": {
"debug": "^4.3.4",
"@figedi/metering": "^1.3.1",
"@figedi/typecop": "^1.3.1",
"@types/lodash": "^4.14.195",
Expand Down
18 changes: 17 additions & 1 deletion src/app/ApplicationBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ import { MissingCommandArgsError } from "./errors";
import { getRootDir, safeReadFile } from "./utils/util";
import { BaseRemoteSource } from "./remoteConfig/remoteSource/BaseRemoteSource";
import { DynamicConfigSource } from "./remoteConfig/remoteSource/DynamicConfigSource";
import _debug from "debug";

const debug = _debug("@figedi/svc");
export type AppPreflightFn<C> = (container: RegisterFnArgs<C>) => Promise<any> | any;

export type ErrorHandlerFn<C> = (args: RegisterFnArgs<C>, e?: Error) => ErrorHandle | Promise<ErrorHandle>;
Expand Down Expand Up @@ -562,18 +564,28 @@ export class ApplicationBuilder<Config> {
private async runCommand<TResult>(
commandName: string,
): Promise<{ result: TResult; shutdownHandle?: () => Promise<void> }> {
debug("Running command");
await Promise.all(this.preflightFns.map(fn => fn(this.buildResolveArgs(this.container))));
debug("Ran all global-preflight fns");
const command = this.container.get<Command<any, TResult>>(commandName);
debug("Resolved ioc-container");
const argv = this.parseCommandArgs(command);

await Promise.all(this.servicesWithLifecycleHandlers.map(svc => svc.preflight && svc.preflight()));
debug("Ran all service-preflight fns");
const commandResult = await command.execute({ logger: this.rootLogger, app: this.app, argv });
debug("Executed command");
let shutdownHandle;

if (this.appBuilderConfig.deferredShutdownHandle) {
shutdownHandle = () => this.handleShutdown({ reason: "SVC_ENDED", code: 0 });
shutdownHandle = async () => {
const result = await this.handleShutdown({ reason: "SVC_ENDED", code: 0 });
debug("Ran deferred shutdown successfully");
return result;
};
} else {
await this.handleShutdown({ reason: "SVC_ENDED", code: 0 });
debug("Ran shutdown successfully");
}
return { result: commandResult, shutdownHandle };
}
Expand Down Expand Up @@ -607,6 +619,7 @@ export class ApplicationBuilder<Config> {
}),
Promise.all(this.servicesWithLifecycleHandlers.map(svc => svc.shutdown && svc.shutdown())),
]);
debug("Ran all service shutdown handlers");
if (this.appBuilderConfig.exitAfterRun) {
this.rootLogger.info({ reason }, `Successfully shut down all services. Reason ${reason}. Goodbye 👋`);
process.exit(exitCode);
Expand Down Expand Up @@ -652,6 +665,7 @@ export class ApplicationBuilder<Config> {
const shutdownHandlerResults = await Promise.allSettled(
this.shutdownHandlers.map(errorFn => errorFn(this.buildResolveArgs(this.container), args.reason)),
);
debug("Ran all global shutdown handlers");

if (shutdownHandlerResults.some(r => r.status === "rejected")) {
this.rootLogger.info("Some shutdown handlers were rejected, will exit immediately");
Expand All @@ -678,10 +692,12 @@ export class ApplicationBuilder<Config> {
command?: string;
config?: PartialConfig<Config>;
}): Promise<{ result: TResult; shutdownHandle?: () => Promise<void> }> {
debug("Init run()");
if (opts?.config) {
this.config = merge({}, this.config, opts.config);
}
const argv: any = minimist(process.argv.slice(2), buildOptions({ command: { type: "string", alias: "c" } }));
debug("Processed argv");
const commandName = (opts?.command || argv.command || this.defaultCommandName) as string | undefined;
if (!commandName || typeof commandName !== "string") {
const availableCommands = this.commandReferences.join("\n");
Expand Down

0 comments on commit 1a85e3a

Please sign in to comment.