UNPKG

@flowlab/all

Version:

A cool library focusing on handling various flows

84 lines (75 loc) 3.31 kB
// --- src/scheduler/scheduler.ts --- import { IWorkflowContext, INodeContext } from '../types'; /** * MARK: 任务调度器接口 * @interface IScheduler * @description 任务调度器接口,用于将工作流或任务的执行推迟或分发 */ export interface IScheduler { /** * MARK: 调度单个工作流 * @param definitionId - 工作流定义 ID * @param input - 工作流输入 * @param options - 调度选项 (例如,延迟执行、优先级、租户信息) * @returns 返回一个任务 ID 或标识符 */ scheduleWorkflow(definitionId: string, input: Record<string, any>, options?: IScheduleOptions): Promise<string>; /** * MARK: 调度单个任务节点 * NOTE: (分布式调度器使用) * @param nodeId - 节点 ID * @param context - 节点执行所需的上下文信息 * @param options - 调度选项 * @returns 返回一个任务 ID 或标识符 */ scheduleNode(nodeId: string, context: Partial<INodeContext>, options?: IScheduleOptions): Promise<string>; /** * MARK: 取消一个已调度的任务 * 取消一个已调度的任务 * @param taskId - scheduleWorkflow 或 scheduleNode 返回的任务 ID */ cancel(taskId: string): Promise<boolean>; } /** * MARK: 调度选项 * @interface IScheduleOptions * @description 调度选项 */ export interface IScheduleOptions { delay?: number; // 延迟执行时间 (毫秒) priority?: number; // 任务优先级 tenantId?: string; // 租户 ID userId?: string; // 用户 ID parentWorkflowId?: string; // 父工作流 ID (用于跟踪) queueName?: string; // 指定的目标队列 (用于 RabbitMQ/Kafka 等) kubernetesJobConfig?: any; // Kubernetes Job 相关配置 (用于 K8s 调度) } /** * MARK: 本地调度器 * @class LocalScheduler * @implements IScheduler * @description 简单的本地调度器 (示例,不真正调度,立即执行) */ export class LocalScheduler implements IScheduler { // 需要注入 Executor 或类似机制来执行 // constructor(private executorFactory: ...) {} async scheduleWorkflow(definitionId: string, input: Record<string, any>, options?: IScheduleOptions): Promise<string> { console.log(`[LocalScheduler] 立即执行工作流 (模拟调度): ${definitionId}`, input, options); // TODO: 调用 Executor 执行 // const context = createWorkflowContext(definitionId, input, options?.tenantId, options?.userId); // const executor = this.executorFactory(definitionId, context); // 需要工厂模式 // executor.run(); return `local_${Date.now()}`; // 返回一个模拟的任务 ID } async scheduleNode(nodeId: string, context: Partial<INodeContext>, options?: IScheduleOptions): Promise<string> { console.log(`[LocalScheduler] 立即执行节点 (模拟调度): ${nodeId}`, context, options); // TODO: 找到节点并执行 (需要更复杂的逻辑) return `local_node_${Date.now()}`; } async cancel(taskId: string): Promise<boolean> { console.log(`[LocalScheduler] 取消任务 (模拟): ${taskId}`); // 本地模拟无法真正取消 // FIXME: 需要实现真正的取消逻辑 return false; } }