my-typescript-project v1.0.34
TaskManager
Description
TaskManager is a TypeScript class designed to manage and execute different types of tasks asynchronously. It provides functionality to create, update, and control the execution of tasks. The class is designed to be agnostic and event-driven, making it suitable for various applications.
Features
- Task Types: Supports different types of tasks, including Chain Tasks, Function Tasks, and Event Tasks.
- Task Lifecycle: Manages the entire lifecycle of tasks, including creation, execution, pausing, resuming, stopping, and cancellation.
- Event-Driven: Utilizes an event-driven approach for task execution and management, making it flexible and easy to integrate into different systems.
- Task Logging: Maintains a log of each task, recording relevant events and messages.
- Task Status Tracking: Tracks the status of each task, allowing for easy monitoring and management.
How to Use
Installation: Install the
task-managerpackage via npm or yarn.npm install task-manageror
yarn add task-managerImport in Your Project:
import { TaskManager, TaskType, TaskStatus, ChainedTask, FunctionTask, EventTask } from 'task-manager';Create an Instance:
const taskManager = new TaskManager();Create Tasks:
// Example Chained Task const chainedTask: ChainedTask = { id: 'chained-task-id', description: 'Chained Task', status: TaskStatus.Draft, startTime: new Date(), elapsedTime: 0, totalDuration: 0, log: [], executionDateTime: new Date(), type: TaskType.ChainTask, chain: 'someChain', chainArgs: () => {}, execute: async () => {}, abort: async () => {}, }; taskManager.createTask(chainedTask); // Similar creation for FunctionTask and EventTaskStart, Stop, Cancel, Pause, and Resume Tasks:
taskManager.startTask('chained-task-id'); taskManager.stopTask('chained-task-id'); taskManager.cancelTask('chained-task-id'); taskManager.pauseTask('chained-task-id'); taskManager.resumeTask('chained-task-id');List Tasks:
const tasks = taskManager.listTasks(); console.log(tasks);Update Tasks:
const updatedTask: Partial<ChainedTask> = { status: TaskStatus.InProgress }; taskManager.updateTask(updatedTask);Read and Delete Tasks:
const readTask = taskManager.readTask('chained-task-id'); taskManager.deleteTask('chained-task-id');
Example
import { TaskManager, TaskType, TaskStatus, ChainedTask, FunctionTask, EventTask } from 'task-manager';
// Create an instance of TaskManager
const taskManager = new TaskManager();
// Create a Chained Task
const chainedTask: ChainedTask = {
id: 'chained-task-id',
description: 'Chained Task',
status: TaskStatus.Draft,
startTime: new Date(),
elapsedTime: 0,
totalDuration: 0,
log: [],
executionDateTime: new Date(),
type: TaskType.ChainTask,
chain: 'someChain',
chainArgs: () => {},
execute: async () => {
// Task execution logic
},
abort: async () => {
// Task abortion logic
},
};
// Create a Function Task
const functionTask: FunctionTask = {
id: 'function-task-id',
description: 'Function Task',
status: TaskStatus.Draft,
startTime: new Date(),
elapsedTime: 0,
totalDuration: 0,
log: [],
executionDateTime: new Date(),
type: TaskType.FunctionTask,
func: async () => {
// Task execution logic
},
funcArgs: () => {
// Function arguments logic
},
execute: async () => {
// Task execution logic
},
abort: async () => {
// Task abortion logic
},
};
// Create an Event Task
const eventTask: EventTask = {
id: 'event-task-id',
description: 'Event Task',
status: TaskStatus.Draft,
startTime: new Date(),
elapsedTime: 0,
totalDuration: 0,
log: [],
executionDateTime: new Date(),
executionDateTime: new Date(),
type: TaskType.EventTask,
event: 'someEvent',
eventArgs: () => {
// Event arguments logic
},
execute: async () => {
// Task execution logic
},
abort: async () => {
// Task abortion logic
},
};
// Add tasks to TaskManager
taskManager.createTask(chainedTask);
taskManager.createTask(functionTask);
taskManager.createTask(eventTask);
// Start a task
taskManager.startTask('chained-task-id');
// List tasks
const tasks = taskManager.listTasks();
console.log(tasks);
// Update a task
const updatedTask: Partial<ChainedTask> = { status: TaskStatus.InProgress };
taskManager.updateTask(updatedTask);
// Read and delete tasks
const readTask = taskManager.readTask('chained-task-id');
taskManager.deleteTask('chained-task-id');API
TaskManager
Constructor
constructor()Creates a new instance of TaskManager.
Methods
createTask(task: ChainedTask | FunctionTask | EventTask): voidCreates a new task and adds it to the task
list.
updateTask(updatedTask: Partial<ChainedTask | FunctionTask | EventTask>): voidUpdates an existing task with the provided properties.
readTask(taskId: TaskId): ChainedTask | FunctionTask | EventTask | undefinedRetrieves a task by its ID.
deleteTask(taskId: TaskId): voidDeletes a task by its ID.
listTasks(): Record<TaskId, ChainedTask | FunctionTask | EventTask>Retrieves a copy of the current task list.
startTask(taskId: TaskId): voidStarts the execution of a task.
stopTask(taskId: TaskId): voidStops the execution of a task.
cancelTask(taskId: TaskId): voidCancels a task.
pauseTask(taskId: TaskId): voidPauses the execution of a task.
resumeTask(taskId: TaskId): voidResumes the execution of a paused task.
Events
The TaskManager class emits the following events:
taskCreated: Emitted when a new task is created.taskUpdated: Emitted when a task is updated.taskStarted: Emitted when a task is started.taskStopped: Emitted when a task is stopped.taskCanceled: Emitted when a task is canceled.taskPaused: Emitted when a task is paused.taskResumed: Emitted when a paused task is resumed.
TaskManagerService
Note: The TaskManagerService is designed to be used with the TBrainStackContext and Logger integration from the Brainstack framework. Please ensure that you have the necessary dependencies installed.
Constructor
constructor(bstack: TBrainStackContext, chainList: Record<any, any>, logger: Logger)Creates a new instance of TaskManagerService.
bstack: An instance ofTBrainStackContextfrom the Brainstack framework.chainList: A record representing the chain list.logger: An instance of theLoggerfrom the Brainstack framework.
Methods
createTask(task: ChainedTask | FunctionTask | EventTaskCreation, delayInSeconds: number): ChainedTask | FunctionTask | EventTaskCreates a new task with the specified type and delay.
task: The task creation parameters.delayInSeconds: The delay before the task is executed.
updateTask(updatedTask: Partial<ChainedTask | FunctionTask | EventTask>): voidUpdates an existing task with the provided properties.
updatedTask: The updated task properties.
readTask(taskId: TaskId): ChainedTask | FunctionTask | EventTask | undefinedRetrieves a task by its ID.
taskId: The ID of the task to retrieve.
deleteTask(taskId: TaskId): voidDeletes a task by its ID.
taskId: The ID of the task to delete.
listTasks(): Record<TaskId, ChainedTask | FunctionTask | EventTask>Retrieves a copy of the current task list.
startTask(taskId: TaskId): voidStarts the execution of a task.
taskId: The ID of the task to start.
stopTask(taskId: TaskId): voidStops the execution of a task.
taskId: The ID of the task to stop.
cancelTask(taskId: TaskId): voidCancels a task.
taskId: The ID of the task to cancel.
pauseTask(taskId: TaskId): voidPauses the execution of a task.
taskId: The ID of the task to pause.
resumeTask(taskId: TaskId): voidResumes the execution of a paused task.
taskId: The ID of the task to resume.
Events
The TaskManagerService class emits the same events as TaskManager.
taskCreatedtaskUpdatedtaskStartedtaskStoppedtaskCanceledtaskPausedtaskResumed
Logger Integration
The TaskManagerService uses the Logger integration provided by the Brainstack framework for logging. Ensure that your Brainstack configuration includes the necessary logger integrations.
Dependencies
task-managerrequires the Brainstack framework for logging and event handling.- Ensure that the necessary dependencies are installed and configured.
Scope
The TaskManager is specifically designed for managing short-lived tasks on the frontend, distinct from traditional backend task management systems. Its primary aim is to facilitate the coordination of intricate interactions between various components on the frontend and AI processes, providing a centralized mechanism for orchestrating complex workflows. This system is particularly well-suited for scenarios where tasks have a relatively short lifespan and need to be executed within the frontend environment. It empowers developers to efficiently handle asynchronous operations, schedule tasks with specific delays, and manage their lifecycle seamlessly. It's essential to note that while the TaskManager excels at enhancing frontend capabilities and unlocking new possibilities for interactive and dynamic applications, it does not replace the need for a robust backend task manager. The backend continues to play a crucial role in handling other aspects that fall outside the scope of frontend-centric task management. Together, these complementary systems contribute to a comprehensive and efficient solution for handling diverse tasks in both frontend and backend environments.
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago