refactor(plugin-workflow): adjust comments (#3990)

This commit is contained in:
Junyi 2024-04-09 17:29:11 +08:00 committed by GitHub
parent 8eee6756d4
commit 773b7aef52
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -46,33 +46,6 @@ export default class PluginWorkflowServer extends Plugin {
private loggerCache: LRUCache<string, Logger>;
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 = <typeof WorkflowModel>instance.constructor;
@ -119,7 +92,42 @@ export default class PluginWorkflowServer extends Plugin {
}
};
registerTrigger<T extends Trigger>(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<T extends Trigger>(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;