Offload tasks to a pool of workers on node.js and in the browser
Overall
score
95%
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
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.
The manager should execute tasks using a worker pool and return results when tasks complete successfully.
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.
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.
The system must distinguish between three types of task failures:
Each type should be reported with a specific status string: "cancelled", "timeout", or "error".
/**
* 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>Provides worker pool functionality with task cancellation and timeout capabilities.
Install with Tessl CLI
npx tessl i tessl/npm-workerpoolevals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10