diff --git a/packages/app/server/src/config/logger.ts b/packages/app/server/src/config/logger.ts index c4897d643..5d656c1de 100644 --- a/packages/app/server/src/config/logger.ts +++ b/packages/app/server/src/config/logger.ts @@ -2,4 +2,5 @@ import { AppLoggerOptions } from '@nocobase/logger'; export default { transports: process.env.LOGGER_TRANSPORT || ['console', 'dailyRotateFile'], + level: process.env.LOGGER_LEVEL || (process.env.APP_ENV === 'development' ? 'debug' : 'info') } as AppLoggerOptions; diff --git a/packages/plugins/workflow/src/server/Plugin.ts b/packages/plugins/workflow/src/server/Plugin.ts index 11c4ec46a..bd48ba707 100644 --- a/packages/plugins/workflow/src/server/Plugin.ts +++ b/packages/plugins/workflow/src/server/Plugin.ts @@ -149,6 +149,8 @@ export default class WorkflowPlugin extends Plugin { this.events.push([workflow, context, options]); + this.app.logger.debug(`[Workflow] new event triggered, now events: ${this.events.length}`); + if (this.events.length > 1) { return; } @@ -164,6 +166,7 @@ export default class WorkflowPlugin extends Plugin { } const [workflow, context, options] = event; + let valid = true; if (options.context?.executionId) { // NOTE: no transaction here for read-uncommitted execution const existed = await workflow.countExecutions({ @@ -173,48 +176,52 @@ export default class WorkflowPlugin extends Plugin { }); if (existed) { - console.warn(`workflow ${workflow.id} has already been triggered in same execution (${options.context.executionId}), and newly triggering will be skipped.`); - return; + this.app.logger.warn(`[Workflow] workflow ${workflow.id} has already been triggered in same execution (${options.context.executionId}), and newly triggering will be skipped.`); + valid = false; } } - const execution = await this.db.sequelize.transaction(async transaction => { - const execution = await workflow.createExecution({ - context, - key: workflow.key, - status: EXECUTION_STATUS.CREATED, - useTransaction: workflow.useTransaction, - }, { transaction }); + if (valid) { + const execution = await this.db.sequelize.transaction(async transaction => { + const execution = await workflow.createExecution({ + context, + key: workflow.key, + status: EXECUTION_STATUS.CREATED, + useTransaction: workflow.useTransaction, + }, { transaction }); - const executed = await workflow.countExecutions({ transaction }); + const executed = await workflow.countExecutions({ transaction }); - // NOTE: not to trigger afterUpdate hook here - await workflow.update({ executed }, { transaction, hooks: false }); + // NOTE: not to trigger afterUpdate hook here + await workflow.update({ executed }, { transaction, hooks: false }); - const allExecuted = await (execution.constructor).count({ - where: { - key: workflow.key - }, - transaction - }); - await (workflow.constructor).update({ - allExecuted - }, { - where: { - key: workflow.key - }, - individualHooks: true, - transaction + const allExecuted = await (execution.constructor).count({ + where: { + key: workflow.key + }, + transaction + }); + await (workflow.constructor).update({ + allExecuted + }, { + where: { + key: workflow.key + }, + individualHooks: true, + transaction + }); + + execution.workflow = workflow; + + return execution; }); - execution.workflow = workflow; + this.app.logger.debug(`[Workflow] execution of workflow ${workflow.id} created as ${execution.id}`); - return execution; - }); - - // NOTE: cache first execution for most cases - if (!this.executing && !this.pending.length) { - this.pending.push([execution]); + // NOTE: cache first execution for most cases + if (!this.executing && !this.pending.length) { + this.pending.push([execution]); + } } if (this.events.length) { @@ -267,9 +274,13 @@ export default class WorkflowPlugin extends Plugin { const processor = this.createProcessor(execution); - console.log('workflow processing:', new Date(), execution.workflowId, execution.id); + this.app.logger.info(`[Workflow] execution ${execution.id} ${job ? 'resuming' : 'starting'} ...`); - await (job ? processor.resume(job) : processor.start()); + try { + await (job ? processor.resume(job) : processor.start()); + } catch (err) { + this.app.logger.error(`[Workflow] ${err.message}`, err); + } this.executing = null;