Skip to content

Conversation

@vkarpov15
Copy link
Member

Motivation

  • Avoid expensive and racy database countDocuments() calls when enforcing per-handler concurrency limits and instead track running counts in memory during polling.
  • Ensure worker-level concurrency configured via startPolling({ parallel }) is honored by poll() so the worker can limit overall parallelism.
  • Allow handlers to express a per-handler concurrency cap so a single task type cannot monopolize a worker.

Description

  • Update registerHandler to accept an options parameter and store handlers as { handler, maxParallelTasks } so maxParallelTasks is available to the poll loop.
  • Change startPolling to forward the parallel option into poll() via this._currentPoll so poll() knows worker-level concurrency.
  • Replace DB counting in poll() with an in-memory handlerLimits map and a runningByName map, and filter out blocked names using filter.name = { $nin: blockedNames } before findOneAndUpdate, while incrementing/decrementing runningByName around each execute() call.
  • Update execute() to resolve the stored handler entry shape and call the handler function via the resolved handlerFn (preserving timeouts and repeat behavior).

Testing

  • Added unit tests in test/task.test.js including a waitFor helper and tests for forwarding parallel to startPolling() and for enforcing maxParallelTasks per handler.
  • Automated tests were not run in this environment because the test suite depends on a running MongoDB instance and timed out during setup, so no test results are available here.

Codex Task

Copilot AI review requested due to automatic review settings January 30, 2026 15:48
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements in-memory tracking for per-handler concurrency limits to replace expensive database counting operations. It allows workers to enforce both worker-level and per-handler concurrency limits simultaneously.

Changes:

  • Added in-memory tracking via handlerLimits and runningByName maps in the poll() method to efficiently enforce per-handler concurrency without database queries
  • Extended registerHandler() to accept an optional options parameter with maxParallelTasks to specify per-handler concurrency limits
  • Modified startPolling() to forward the parallel option to poll() for worker-level concurrency control
  • Updated execute() to handle both the new object-based handler format and legacy function-based format for backward compatibility

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/taskSchema.js Core implementation of in-memory concurrency tracking, handler registration changes, and backward-compatible handler resolution
test/task.test.js Added test utilities (waitFor) and comprehensive tests for parallel option forwarding and per-handler concurrency limits
README.md Added documentation explaining the new parallelism features with clear code examples

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants