my-typescript-project

1.0.23 • Public • Published

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

  1. Installation: Install the task-manager package via npm or yarn.

    npm install task-manager

    or

    yarn add task-manager
  2. Import in Your Project:

    import { TaskManager, TaskType, TaskStatus, ChainedTask, FunctionTask, EventTask } from 'task-manager';
  3. Create an Instance:

    const taskManager = new TaskManager();
  4. 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
  5. 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');
  6. List Tasks:

    const tasks = taskManager.listTasks();
    console.log(tasks);
  7. Update Tasks:

    const updatedTask: Partial<ChainedTask> = { status: TaskStatus.InProgress };
    taskManager.updateTask(updatedTask);
  8. 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): 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.

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 of TBrainStackContext from the Brainstack framework.
  • chainList: A record representing the chain list.
  • logger: An instance of the Logger from the Brainstack framework.

Methods

  • 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.

Events

The TaskManagerService class emits the same events as TaskManager.

  • taskCreated
  • taskUpdated
  • taskStarted
  • taskStopped
  • taskCanceled
  • taskPaused
  • taskResumed

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-manager requires 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.

Readme

Keywords

none

Package Sidebar

Install

npm i my-typescript-project

Weekly Downloads

5

Version

1.0.23

License

ISC

Unpacked Size

57.9 kB

Total Files

23

Last publish

Collaborators

  • infinisoft-world