feat: add measure execution function (#2801)

This commit is contained in:
ChengLei Shao 2023-10-11 19:15:59 +08:00 committed by GitHub
parent 8bd6ef897c
commit 2dc964d4f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 9 deletions

View File

@ -5,7 +5,7 @@ import { Cache, createCache, ICacheConfig } from '@nocobase/cache';
import Database, { CollectionOptions, IDatabaseOptions } from '@nocobase/database';
import { AppLoggerOptions, createAppLogger, Logger } from '@nocobase/logger';
import { ResourceOptions, Resourcer } from '@nocobase/resourcer';
import { applyMixins, AsyncEmitter, Toposort, ToposortOptions } from '@nocobase/utils';
import { applyMixins, AsyncEmitter, Toposort, ToposortOptions, measureExecutionTime } from '@nocobase/utils';
import chalk from 'chalk';
import { Command, CommandOptions, ParseOptions } from 'commander';
import { IncomingMessage, Server, ServerResponse } from 'http';
@ -632,18 +632,29 @@ export class Application<StateT = DefaultState, ContextT = DefaultContext> exten
async upgrade(options: any = {}) {
await this.emitAsync('beforeUpgrade', this, options);
const force = false;
await measureExecutionTime(async () => {
await this.db.migrator.up();
}, 'Migrator');
await measureExecutionTime(async () => {
await this.db.sync({
force,
alter: {
drop: force,
},
});
}, 'Sync');
await this.version.update();
await this.emitAsync('afterUpgrade', this, options);
this.log.debug(chalk.green(`✨ NocoBase has been upgraded to v${this.getVersion()}`));
if (this._started) {
await measureExecutionTime(async () => {
await this.restart();
}, 'Restart');
}
}

View File

@ -20,5 +20,6 @@ export * from './requireModule';
export * from './toposort';
export * from './uid';
export * from './url';
export * from './measure-execution-time';
export { dayjs, lodash };

View File

@ -0,0 +1,7 @@
export async function measureExecutionTime(operation, operationName) {
const startTime = Date.now();
await operation();
const endTime = Date.now();
const duration = (endTime - startTime).toFixed(0);
console.log(`${operationName} completed in ${duration} milliseconds`);
}