feat(cli): add graceful process abort
Using an AbortSignal and Promise.race we abandon running migrations that take longer to complete after the process is aborted than the given abortRespite period
This commit is contained in:
parent
ce15648251
commit
a4da353d5a
17 changed files with 378 additions and 31 deletions
|
|
@ -165,6 +165,20 @@ const getError = (error?: ErrorLike, indent = ' ') => {
|
|||
return parts.join('\n');
|
||||
};
|
||||
|
||||
const getAbortMessage = (reason?: Error) => {
|
||||
if (!reason) {
|
||||
return '';
|
||||
}
|
||||
|
||||
const parts = [` ${red.bold(reason.message)}`];
|
||||
|
||||
if (isErrorLike(reason.cause)) {
|
||||
parts.push(getError(reason.cause, ' '));
|
||||
}
|
||||
|
||||
return parts.join('\n');
|
||||
};
|
||||
|
||||
const getSummary = (
|
||||
command: ReporterInitParameters['command'],
|
||||
migrations: Array<MigrationMetadata | MigrationMetadataFinished> = [],
|
||||
|
|
@ -281,6 +295,7 @@ class DefaultFancyReporter implements Required<EmigrateReporter> {
|
|||
#error: Error | undefined;
|
||||
#parameters!: ReporterInitParameters;
|
||||
#interval: NodeJS.Timeout | undefined;
|
||||
#abortReason: Error | undefined;
|
||||
|
||||
onInit(parameters: ReporterInitParameters): void | PromiseLike<void> {
|
||||
this.#parameters = parameters;
|
||||
|
|
@ -288,6 +303,10 @@ class DefaultFancyReporter implements Required<EmigrateReporter> {
|
|||
this.#start();
|
||||
}
|
||||
|
||||
onAbort(reason: Error): void | PromiseLike<void> {
|
||||
this.#abortReason = reason;
|
||||
}
|
||||
|
||||
onCollectedMigrations(migrations: MigrationMetadata[]): void | PromiseLike<void> {
|
||||
this.#migrations = migrations;
|
||||
}
|
||||
|
|
@ -358,6 +377,7 @@ class DefaultFancyReporter implements Required<EmigrateReporter> {
|
|||
getTitle(this.#parameters),
|
||||
getHeaderMessage(this.#parameters.command, this.#migrations, this.#lockedMigrations),
|
||||
this.#migrations?.map((migration) => getMigrationText(migration, this.#activeMigration)).join('\n') ?? '',
|
||||
getAbortMessage(this.#abortReason),
|
||||
getSummary(this.#parameters.command, this.#migrations),
|
||||
getError(this.#error),
|
||||
];
|
||||
|
|
@ -403,6 +423,12 @@ class DefaultReporter implements Required<EmigrateReporter> {
|
|||
console.log('');
|
||||
}
|
||||
|
||||
onAbort(reason: Error): void | PromiseLike<void> {
|
||||
console.log('');
|
||||
console.error(getAbortMessage(reason));
|
||||
console.log('');
|
||||
}
|
||||
|
||||
onCollectedMigrations(migrations: MigrationMetadata[]): void | PromiseLike<void> {
|
||||
this.#migrations = migrations;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue