From 773b7aef527af7d713a2b7665b3e9da4f7ee75b3 Mon Sep 17 00:00:00 2001 From: Junyi Date: Tue, 9 Apr 2024 17:29:11 +0800 Subject: [PATCH] refactor(plugin-workflow): adjust comments (#3990) --- .../plugin-workflow/src/server/Plugin.ts | 72 +++++++++++-------- 1 file changed, 43 insertions(+), 29 deletions(-) diff --git a/packages/plugins/@nocobase/plugin-workflow/src/server/Plugin.ts b/packages/plugins/@nocobase/plugin-workflow/src/server/Plugin.ts index 66c662d1f..e88e90dfb 100644 --- a/packages/plugins/@nocobase/plugin-workflow/src/server/Plugin.ts +++ b/packages/plugins/@nocobase/plugin-workflow/src/server/Plugin.ts @@ -46,33 +46,6 @@ export default class PluginWorkflowServer extends Plugin { private loggerCache: LRUCache; private meter = null; - getLogger(workflowId: ID): Logger { - const now = new Date(); - const date = `${now.getFullYear()}-${`0${now.getMonth() + 1}`.slice(-2)}-${`0${now.getDate()}`.slice(-2)}`; - const key = `${date}-${workflowId}}`; - if (this.loggerCache.has(key)) { - return this.loggerCache.get(key); - } - - const logger = this.createLogger({ - dirname: path.join('workflows', date), - filename: `${workflowId}.log`, - transports: [...(process.env.NODE_ENV !== 'production' ? ['console'] : ['file'])], - } as LoggerOptions); - - this.loggerCache.set(key, logger); - - return logger; - } - - isWorkflowSync(workflow: WorkflowModel) { - const trigger = this.triggers.get(workflow.type); - if (!trigger) { - throw new Error(`invalid trigger type ${workflow.type} of workflow ${workflow.id}`); - } - return trigger.sync ?? workflow.sync; - } - private onBeforeSave = async (instance: WorkflowModel, options) => { const Model = instance.constructor; @@ -119,7 +92,42 @@ export default class PluginWorkflowServer extends Plugin { } }; - registerTrigger(type: string, trigger: T | { new (p: Plugin): T }) { + /** + * @experimental + */ + getLogger(workflowId: ID): Logger { + const now = new Date(); + const date = `${now.getFullYear()}-${`0${now.getMonth() + 1}`.slice(-2)}-${`0${now.getDate()}`.slice(-2)}`; + const key = `${date}-${workflowId}}`; + if (this.loggerCache.has(key)) { + return this.loggerCache.get(key); + } + + const logger = this.createLogger({ + dirname: path.join('workflows', date), + filename: `${workflowId}.log`, + transports: [...(process.env.NODE_ENV !== 'production' ? ['console'] : ['file'])], + } as LoggerOptions); + + this.loggerCache.set(key, logger); + + return logger; + } + + /** + * @experimental + * @param {WorkflowModel} workflow + * @returns {boolean} + */ + isWorkflowSync(workflow: WorkflowModel): boolean { + const trigger = this.triggers.get(workflow.type); + if (!trigger) { + throw new Error(`invalid trigger type ${workflow.type} of workflow ${workflow.id}`); + } + return trigger.sync ?? workflow.sync; + } + + public registerTrigger(type: string, trigger: T | { new (p: Plugin): T }) { if (typeof trigger === 'function') { this.triggers.register(type, new trigger(this)); } else if (trigger) { @@ -129,7 +137,10 @@ export default class PluginWorkflowServer extends Plugin { } } - registerInstruction(type: string, instruction: InstructionInterface | { new (p: Plugin): InstructionInterface }) { + public registerInstruction( + type: string, + instruction: InstructionInterface | { new (p: Plugin): InstructionInterface }, + ) { if (typeof instruction === 'function') { this.instructions.register(type, new instruction(this)); } else if (instruction) { @@ -162,6 +173,9 @@ export default class PluginWorkflowServer extends Plugin { } } + /** + * @internal + */ async load() { const { db, options } = this;