mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-10-08 16:39:42 +02:00
docs: add JSDoc comments to task-queue.ts
Comprehensive documentation for TaskQueue: - Module-level description - Event interface documentation - Class-level description - Method-level comments for all public methods - Singleton accessor documentation Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,18 +1,46 @@
|
|||||||
|
/**
|
||||||
|
* @fileoverview Task queue for managing Claude prompts and operations
|
||||||
|
*
|
||||||
|
* Provides a priority queue for tasks with:
|
||||||
|
* - Priority-based ordering (higher priority first)
|
||||||
|
* - Dependency tracking between tasks
|
||||||
|
* - State persistence via StateStore
|
||||||
|
* - Session assignment for task execution
|
||||||
|
*
|
||||||
|
* @module task-queue
|
||||||
|
*/
|
||||||
|
|
||||||
import { EventEmitter } from 'node:events';
|
import { EventEmitter } from 'node:events';
|
||||||
import { Task, CreateTaskOptions } from './task.js';
|
import { Task, CreateTaskOptions } from './task.js';
|
||||||
import { getStore } from './state-store.js';
|
import { getStore } from './state-store.js';
|
||||||
import { TaskState } from './types.js';
|
import { TaskState } from './types.js';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Events emitted by TaskQueue
|
||||||
|
*/
|
||||||
export interface TaskQueueEvents {
|
export interface TaskQueueEvents {
|
||||||
|
/** Fired when a task is added to the queue */
|
||||||
taskAdded: (task: Task) => void;
|
taskAdded: (task: Task) => void;
|
||||||
|
/** Fired when a task is removed from the queue */
|
||||||
taskRemoved: (taskId: string) => void;
|
taskRemoved: (taskId: string) => void;
|
||||||
|
/** Fired when a task's state changes */
|
||||||
taskUpdated: (task: Task) => void;
|
taskUpdated: (task: Task) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Priority queue for managing tasks with dependency support.
|
||||||
|
*
|
||||||
|
* @description
|
||||||
|
* Tasks are ordered by priority (descending) then creation time (ascending).
|
||||||
|
* Dependencies can be specified to ensure tasks run in correct order.
|
||||||
|
*
|
||||||
|
* @extends EventEmitter
|
||||||
|
*/
|
||||||
export class TaskQueue extends EventEmitter {
|
export class TaskQueue extends EventEmitter {
|
||||||
private tasks: Map<string, Task> = new Map();
|
private tasks: Map<string, Task> = new Map();
|
||||||
private store = getStore();
|
private store = getStore();
|
||||||
|
|
||||||
|
/** Creates a new TaskQueue and loads persisted tasks. */
|
||||||
constructor() {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
this.loadFromStore();
|
this.loadFromStore();
|
||||||
@@ -26,6 +54,7 @@ export class TaskQueue extends EventEmitter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Adds a new task to the queue. */
|
||||||
addTask(options: CreateTaskOptions): Task {
|
addTask(options: CreateTaskOptions): Task {
|
||||||
const task = new Task(options);
|
const task = new Task(options);
|
||||||
this.tasks.set(task.id, task);
|
this.tasks.set(task.id, task);
|
||||||
@@ -34,10 +63,12 @@ export class TaskQueue extends EventEmitter {
|
|||||||
return task;
|
return task;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Gets a task by ID. */
|
||||||
getTask(id: string): Task | undefined {
|
getTask(id: string): Task | undefined {
|
||||||
return this.tasks.get(id);
|
return this.tasks.get(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Removes a task by ID. Returns true if removed. */
|
||||||
removeTask(id: string): boolean {
|
removeTask(id: string): boolean {
|
||||||
const removed = this.tasks.delete(id);
|
const removed = this.tasks.delete(id);
|
||||||
if (removed) {
|
if (removed) {
|
||||||
@@ -47,16 +78,19 @@ export class TaskQueue extends EventEmitter {
|
|||||||
return removed;
|
return removed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Updates a task and persists the change. */
|
||||||
updateTask(task: Task): void {
|
updateTask(task: Task): void {
|
||||||
this.tasks.set(task.id, task);
|
this.tasks.set(task.id, task);
|
||||||
this.store.setTask(task.id, task.toState());
|
this.store.setTask(task.id, task.toState());
|
||||||
this.emit('taskUpdated', task);
|
this.emit('taskUpdated', task);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Gets all tasks in the queue. */
|
||||||
getAllTasks(): Task[] {
|
getAllTasks(): Task[] {
|
||||||
return Array.from(this.tasks.values());
|
return Array.from(this.tasks.values());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Gets pending tasks sorted by priority then creation time. */
|
||||||
getPendingTasks(): Task[] {
|
getPendingTasks(): Task[] {
|
||||||
return this.getAllTasks()
|
return this.getAllTasks()
|
||||||
.filter((t) => t.isPending())
|
.filter((t) => t.isPending())
|
||||||
@@ -69,22 +103,27 @@ export class TaskQueue extends EventEmitter {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Gets tasks currently being executed. */
|
||||||
getRunningTasks(): Task[] {
|
getRunningTasks(): Task[] {
|
||||||
return this.getAllTasks().filter((t) => t.isRunning());
|
return this.getAllTasks().filter((t) => t.isRunning());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Gets successfully completed tasks. */
|
||||||
getCompletedTasks(): Task[] {
|
getCompletedTasks(): Task[] {
|
||||||
return this.getAllTasks().filter((t) => t.isCompleted());
|
return this.getAllTasks().filter((t) => t.isCompleted());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Gets tasks that failed execution. */
|
||||||
getFailedTasks(): Task[] {
|
getFailedTasks(): Task[] {
|
||||||
return this.getAllTasks().filter((t) => t.isFailed());
|
return this.getAllTasks().filter((t) => t.isFailed());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Returns true if there's a task ready for execution. */
|
||||||
hasNext(): boolean {
|
hasNext(): boolean {
|
||||||
return this.getNextAvailable() !== null;
|
return this.getNextAvailable() !== null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Gets the next task ready for execution (with satisfied dependencies). */
|
||||||
getNextAvailable(): Task | null {
|
getNextAvailable(): Task | null {
|
||||||
const pending = this.getPendingTasks();
|
const pending = this.getPendingTasks();
|
||||||
|
|
||||||
@@ -97,6 +136,7 @@ export class TaskQueue extends EventEmitter {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Alias for getNextAvailable(). */
|
||||||
next(): Task | null {
|
next(): Task | null {
|
||||||
return this.getNextAvailable();
|
return this.getNextAvailable();
|
||||||
}
|
}
|
||||||
@@ -111,16 +151,19 @@ export class TaskQueue extends EventEmitter {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Gets all tasks assigned to a specific session. */
|
||||||
getTasksBySession(sessionId: string): Task[] {
|
getTasksBySession(sessionId: string): Task[] {
|
||||||
return this.getAllTasks().filter((t) => t.assignedSessionId === sessionId);
|
return this.getAllTasks().filter((t) => t.assignedSessionId === sessionId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Gets the currently running task for a session, if any. */
|
||||||
getRunningTaskForSession(sessionId: string): Task | null {
|
getRunningTaskForSession(sessionId: string): Task | null {
|
||||||
return this.getAllTasks().find(
|
return this.getAllTasks().find(
|
||||||
(t) => t.isRunning() && t.assignedSessionId === sessionId
|
(t) => t.isRunning() && t.assignedSessionId === sessionId
|
||||||
) || null;
|
) || null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Gets counts of tasks by status. */
|
||||||
getCount(): { total: number; pending: number; running: number; completed: number; failed: number } {
|
getCount(): { total: number; pending: number; running: number; completed: number; failed: number } {
|
||||||
const tasks = this.getAllTasks();
|
const tasks = this.getAllTasks();
|
||||||
return {
|
return {
|
||||||
@@ -132,6 +175,7 @@ export class TaskQueue extends EventEmitter {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Removes all completed tasks. Returns count removed. */
|
||||||
clearCompleted(): number {
|
clearCompleted(): number {
|
||||||
let count = 0;
|
let count = 0;
|
||||||
for (const task of this.getAllTasks()) {
|
for (const task of this.getAllTasks()) {
|
||||||
@@ -143,6 +187,7 @@ export class TaskQueue extends EventEmitter {
|
|||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Removes all failed tasks. Returns count removed. */
|
||||||
clearFailed(): number {
|
clearFailed(): number {
|
||||||
let count = 0;
|
let count = 0;
|
||||||
for (const task of this.getAllTasks()) {
|
for (const task of this.getAllTasks()) {
|
||||||
@@ -154,6 +199,7 @@ export class TaskQueue extends EventEmitter {
|
|||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Removes all tasks from the queue. Returns count removed. */
|
||||||
clearAll(): number {
|
clearAll(): number {
|
||||||
const count = this.tasks.size;
|
const count = this.tasks.size;
|
||||||
for (const id of this.tasks.keys()) {
|
for (const id of this.tasks.keys()) {
|
||||||
@@ -163,6 +209,7 @@ export class TaskQueue extends EventEmitter {
|
|||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Gets tasks from persistent storage. */
|
||||||
getStoredTasks(): Record<string, TaskState> {
|
getStoredTasks(): Record<string, TaskState> {
|
||||||
return this.store.getTasks();
|
return this.store.getTasks();
|
||||||
}
|
}
|
||||||
@@ -171,6 +218,7 @@ export class TaskQueue extends EventEmitter {
|
|||||||
// Singleton instance
|
// Singleton instance
|
||||||
let queueInstance: TaskQueue | null = null;
|
let queueInstance: TaskQueue | null = null;
|
||||||
|
|
||||||
|
/** Gets or creates the singleton TaskQueue instance. */
|
||||||
export function getTaskQueue(): TaskQueue {
|
export function getTaskQueue(): TaskQueue {
|
||||||
if (!queueInstance) {
|
if (!queueInstance) {
|
||||||
queueInstance = new TaskQueue();
|
queueInstance = new TaskQueue();
|
||||||
|
|||||||
Reference in New Issue
Block a user