crewai-ts
Version:
TypeScript port of crewAI for agent-based workflows
235 lines • 6.51 kB
TypeScript
/**
* FlowScheduler
*
* Resource-aware scheduling system for optimizing multi-flow execution,
* implementing advanced performance optimization strategies to maximize
* throughput while respecting system constraints.
*/
import { EventEmitter } from 'events';
import { FlowExecutionTracker } from './FlowExecutionTracker.js';
declare global {
namespace NodeJS {
interface Process {
cpuUsage(): {
user: number;
system: number;
};
}
}
}
interface ResourceEstimate {
cpu: number;
memory: number;
io: number;
network: number;
}
interface ResourceUsage extends ResourceEstimate {
availableCpu: number;
availableMemory: number;
maxConcurrentIo: number;
maxConcurrentNetwork: number;
}
interface ResourceLimits {
availableCpu: number;
availableMemory: number;
maxConcurrentIo: number;
maxConcurrentNetwork: number;
}
interface FlowExecutionMetrics {
startTime: number;
endTime: number;
executionTime: number;
duration: number;
resourceUsage?: ResourceUsage;
error?: Error;
}
interface Flow<T> {
execute(): Promise<T>;
id: string;
}
interface SchedulableFlow {
id: string;
flow: any;
resourceUsage?: ResourceUsage;
estimatedResourceUsage?: ResourceEstimate;
metrics?: FlowExecutionMetrics;
state: string;
readyTime?: number;
timeInQueue?: number;
priority: number;
dependencies: Set<string>;
dependents: Set<string>;
attempt: number;
errors: Error[];
startTime?: number;
endTime?: number;
executionTime?: number;
}
export interface SchedulerOptions {
/** Maximum concurrent flows to execute */
maxConcurrency?: number;
/** Whether to optimize for speed or memory */
optimizeFor?: 'speed' | 'memory' | 'balanced';
/** Resource limits for scheduling */
resourceLimits?: Partial<ResourceLimits>;
/** Enable predictive scheduling based on flow history */
enablePredictiveScheduling?: boolean;
/** Window size for adaptive scheduling algorithms */
adaptiveWindowSize?: number;
/** Default priority for flows (higher is more important) */
defaultPriority?: number;
/** Use work-stealing algorithm for load balancing */
enableWorkStealing?: boolean;
/** Maximum time a flow can be in queue before priority boost */
maxQueueTime?: number;
/** Amount to boost priority after max queue time */
priorityBoostAmount?: number;
/** Re-evaluate schedule after this many milliseconds */
scheduleInterval?: number;
/** Apply backpressure when system is overloaded */
enableBackpressure?: boolean;
}
export interface FlowSchedulerOptions extends SchedulerOptions {
tracker?: FlowExecutionTracker;
}
export declare class FlowScheduler extends EventEmitter {
private readyQueue;
private pendingFlows;
private runningFlows;
private completedFlows;
private failedFlows;
private flowMap;
private resourceUsage;
private timer?;
private tracker;
private metrics;
private logger;
private flowsBlockedBy;
private flowsBlocking;
private flowExecutionHistory;
private lastScheduleTime;
private priorityChanged;
private options;
constructor(options?: FlowSchedulerOptions, tracker?: FlowExecutionTracker);
private initializeResources;
private initializeMetrics;
/**
* Register a flow with the scheduler
*/
registerFlow(id: string, flow: Flow<any>, options?: {
priority?: number;
dependencies?: string[];
resourceEstimate?: Partial<ResourceEstimate>;
}): void;
/**
* Add a dependency between flows
*/
addDependency(dependentId: string, dependencyId: string): void;
/**
* Start scheduling flows
*/
start(): void;
/**
* Stop scheduling flows
*/
stop(): void;
/**
* Reset the scheduler to initial state
*/
reset(): void;
private resetResources;
/**
* Update the set of flows that are ready to run
* Uses optimized dependency checking
*/
private updateReadyFlows;
/**
* Sort the ready queue based on priority and other factors
* Uses optimized sorting algorithm
*/
private sortReadyQueue;
/**
* Add flow to ready queue
*/
private addFlowToReadyQueue;
/**
* Mark flow as ready
*/
private markFlowAsReady;
/**
* Calculate resource efficiency score for a flow
* Higher is better
*/
private calculateResourceEfficiency;
/**
* Predict execution time for a flow based on historical data
* Returns estimate in milliseconds
*/
private predictExecutionTime;
/**
* Schedule flows for execution based on resources and priorities
*/
private scheduleFlows;
/**
* Check if the system is overloaded and should apply backpressure
*/
private isSystemOverloaded;
/**
* Execute a flow
*/
private executeFlow;
private markFlowCompleted;
/**
* Update the set of flows that are dependent on a given flow
*/
private updateDependentFlows;
/**
* Check if adding a dependency would create a cycle
* Using optimized DFS with visited sets
*/
private wouldCreateCycle;
/**
* Start flow execution
*/
startFlowExecution(flowId: string): void;
/**
* Get all scheduled flows
*/
getFlows(): Map<string, SchedulableFlow>;
/**
* Get a flow by ID
*/
getFlow(flowId: string): SchedulableFlow | undefined;
/**
* Get current scheduler stats
*/
getStats(): {
pendingCount: number;
readyCount: number;
runningCount: number;
completedCount: number;
totalFlows: number;
resourceUsage: {
availableCpu: number;
availableMemory: number;
maxConcurrentIo: number;
maxConcurrentNetwork: number;
cpu: number;
memory: number;
io: number;
network: number;
};
};
private updateResourceUsage;
private handleFlowError;
private canScheduleFlow;
private calculateResourceUsage;
private getResourceUsage;
private scheduleFlow;
private updateFlowMetrics;
private getFlowMetrics;
private getFlowExecutionTime;
private getFlowResourceUsage;
}
export {};
//# sourceMappingURL=FlowScheduler.d.ts.map