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-5/

Task Manager with Timeout and Cancellation

A task execution system that processes computational tasks with built-in timeout and cancellation capabilities. The system should handle multiple concurrent tasks, provide cleanup operations, and properly handle different error scenarios.

Requirements

Implement a task manager that:

  1. Execute multiple tasks concurrently - Run multiple computational tasks in parallel and wait for all to complete
  2. Support task cancellation - Allow tasks to be cancelled while running, with proper error handling for cancelled tasks
  3. Enforce task timeouts - Automatically cancel tasks that exceed a specified time limit
  4. Provide cleanup operations - Execute cleanup logic regardless of whether tasks succeed, fail, or are cancelled

Specifications

Task Execution

The system should execute tasks in a worker pool and return results. Tasks may succeed, fail, timeout, or be cancelled.

Cancellation Handling

When a task is cancelled:

  • The task should stop execution
  • A cancellation error should be thrown
  • The system should be able to detect and handle this specific error type

Timeout Handling

When a task exceeds its timeout:

  • The task should be automatically cancelled
  • A timeout error should be thrown
  • The system should be able to detect and handle this specific error type

Cleanup Operations

Cleanup functions should execute in all scenarios:

  • After successful task completion
  • After task failure
  • After task cancellation
  • After task timeout

Test Cases

  • Execute 3 concurrent tasks and receive all results in an array @test
  • Cancel a running task and catch the cancellation error @test
  • Apply a timeout to a task and catch the timeout error @test
  • Execute cleanup logic after task completion regardless of outcome @test

Implementation

@generates

API

/**
 * Create a task manager instance
 */
function createTaskManager() {
  // Returns object with methods to execute and manage tasks
}

/**
 * Execute multiple tasks concurrently
 * @param {Array} tasks - Array of task specifications
 * @returns {Promise<Array>} Results from all tasks
 */
function executeAllTasks(tasks) {
  // Execute tasks and return array of results
}

/**
 * Execute a task with cancellation support
 * @param {string} taskName - Name of the task to execute
 * @param {Array} params - Parameters for the task
 * @returns {Promise} Task promise that can be cancelled
 */
function executeWithCancellation(taskName, params) {
  // Returns promise with cancel() method
}

/**
 * Execute a task with timeout
 * @param {string} taskName - Name of the task to execute
 * @param {Array} params - Parameters for the task
 * @param {number} timeoutMs - Timeout in milliseconds
 * @returns {Promise} Task promise that will timeout
 */
function executeWithTimeout(taskName, params, timeoutMs) {
  // Returns promise that rejects on timeout
}

/**
 * Execute a task with cleanup
 * @param {string} taskName - Name of the task to execute
 * @param {Array} params - Parameters for the task
 * @param {Function} cleanupFn - Function to run after task completes
 * @returns {Promise} Task promise with cleanup
 */
function executeWithCleanup(taskName, params, cleanupFn) {
  // Returns promise that runs cleanup regardless of outcome
}

/**
 * Check if an error is a cancellation error
 * @param {Error} error - Error to check
 * @returns {boolean} True if cancellation error
 */
function isCancellationError(error) {
  // Returns true for cancellation errors
}

/**
 * Check if an error is a timeout error
 * @param {Error} error - Error to check
 * @returns {boolean} True if timeout error
 */
function isTimeoutError(error) {
  // Returns true for timeout errors
}

module.exports = {
  createTaskManager,
  executeAllTasks,
  executeWithCancellation,
  executeWithTimeout,
  executeWithCleanup,
  isCancellationError,
  isTimeoutError
};

Dependencies { .dependencies }

workerpool { .dependency }

Provides worker pool functionality for task execution with advanced promise features including cancellation, timeouts, and enhanced error handling.

Install with Tessl CLI

npx tessl i tessl/npm-workerpool

tile.json