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.
- 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.
-
Installation: Install the
task-manager
package via npm or yarn.npm install task-manager
or
yarn add task-manager
-
Import 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 EventTask
-
Start, 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');
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');
constructor()
Creates a new instance of TaskManager
.
-
createTask(task: ChainedTask | FunctionTask | EventTask): void
Creates a new task and adds it to the task
list.
-
updateTask(updatedTask: Partial<ChainedTask | FunctionTask | EventTask>): void
Updates an existing task with the provided properties.
-
readTask(taskId: TaskId): ChainedTask | FunctionTask | EventTask | undefined
Retrieves a task by its ID.
-
deleteTask(taskId: TaskId): void
Deletes a task by its ID.
-
listTasks(): Record<TaskId, ChainedTask | FunctionTask | EventTask>
Retrieves a copy of the current task list.
-
startTask(taskId: TaskId): void
Starts the execution of a task.
-
stopTask(taskId: TaskId): void
Stops the execution of a task.
-
cancelTask(taskId: TaskId): void
Cancels a task.
-
pauseTask(taskId: TaskId): void
Pauses the execution of a task.
-
resumeTask(taskId: TaskId): void
Resumes the execution of a paused task.
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.
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(bstack: TBrainStackContext, chainList: Record<any, any>, logger: Logger)
Creates a new instance of TaskManagerService
.
-
bstack
: An instance ofTBrainStackContext
from the Brainstack framework. -
chainList
: A record representing the chain list. -
logger
: An instance of theLogger
from the Brainstack framework.
-
createTask(task: ChainedTask | FunctionTask | EventTaskCreation, delayInSeconds: number): ChainedTask | FunctionTask | EventTask
Creates 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>): void
Updates an existing task with the provided properties.
-
updatedTask
: The updated task properties.
-
-
readTask(taskId: TaskId): ChainedTask | FunctionTask | EventTask | undefined
Retrieves a task by its ID.
-
taskId
: The ID of the task to retrieve.
-
-
deleteTask(taskId: TaskId): void
Deletes 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): void
Starts the execution of a task.
-
taskId
: The ID of the task to start.
-
-
stopTask(taskId: TaskId): void
Stops the execution of a task.
-
taskId
: The ID of the task to stop.
-
-
cancelTask(taskId: TaskId): void
Cancels a task.
-
taskId
: The ID of the task to cancel.
-
-
pauseTask(taskId: TaskId): void
Pauses the execution of a task.
-
taskId
: The ID of the task to pause.
-
-
resumeTask(taskId: TaskId): void
Resumes the execution of a paused task.
-
taskId
: The ID of the task to resume.
-
The TaskManagerService
class emits the same events as TaskManager
.
taskCreated
taskUpdated
taskStarted
taskStopped
taskCanceled
taskPaused
taskResumed
The TaskManagerService
uses the Logger
integration provided by the Brainstack framework for logging. Ensure that your Brainstack configuration includes the necessary logger integrations.
-
task-manager
requires the Brainstack framework for logging and event handling. - Ensure that the necessary dependencies are installed and configured.
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.