CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-workerpool

Offload tasks to a pool of workers on node.js and in the browser

Overall
score

95%

Overview
Eval results
Files

task.mdevals/scenario-4/

Task Progress Manager with Cancellation

Build a task progress manager that executes long-running computational tasks with the ability to cancel them based on user input or system conditions.

@generates

Requirements

The system should allow users to start multiple computational tasks, monitor their progress, and cancel individual tasks when needed. The manager must handle cancellation cleanly and provide clear feedback about why tasks were stopped.

Task Execution

The manager should execute tasks using a worker pool and return results when tasks complete successfully.

Cancellation Support

Users should be able to cancel running tasks at any time. When a task is cancelled, the system should catch the cancellation error and return a clear status indicating the task was cancelled by the user.

Timeout Handling

Tasks should automatically timeout if they exceed a configurable time limit. When a timeout occurs, the system should catch the timeout error and return a clear status indicating the task exceeded its time limit.

Error Differentiation

The system must distinguish between three types of task failures:

  • User-initiated cancellations
  • Automatic timeouts
  • Normal execution errors

Each type should be reported with a specific status string: "cancelled", "timeout", or "error".

Test Cases

  • Starting a task that completes normally within 100ms returns status "completed" and the computed result @test
  • Cancelling a running task that takes 500ms to complete (cancelled after 50ms) returns status "cancelled" @test
  • A task that runs longer than its 100ms timeout returns status "timeout" @test
  • A task that throws an error during execution returns status "error" and includes the error message @test

API

/**
 * Creates a task manager with cancellation support
 * @param {object} options - Configuration options
 * @param {number} options.maxWorkers - Maximum number of workers in the pool
 * @param {number} options.defaultTimeout - Default timeout in milliseconds for tasks
 */
function createTaskManager(options) {
  // Returns an object with methods to execute and cancel tasks
}

/**
 * Execute a computational task with cancellation support
 * @param {Function} taskFn - The function to execute in a worker
 * @param {Array} args - Arguments to pass to the task function
 * @returns {Promise<object>} Result object with status and data/error
 */
taskManager.executeTask(taskFn, args) -> Promise<{status: string, data?: any, error?: string}>

/**
 * Cancel a running task by its promise
 * @param {Promise} taskPromise - The promise returned from executeTask
 */
taskManager.cancelTask(taskPromise) -> void

/**
 * Terminate the task manager and cleanup resources
 */
taskManager.terminate() -> Promise<void>

Dependencies { .dependencies }

workerpool { .dependency }

Provides worker pool functionality with task cancellation and timeout capabilities.

Install with Tessl CLI

npx tessl i tessl/npm-workerpool

tile.json