Skip to content

Refine startPolling time option handling#6

Merged
vkarpov15 merged 2 commits intomainfrom
codex/add-getcurrenttime-option-to-startpolling
Feb 4, 2026
Merged

Refine startPolling time option handling#6
vkarpov15 merged 2 commits intomainfrom
codex/add-getcurrenttime-option-to-startpolling

Conversation

@vkarpov15
Copy link
Copy Markdown
Member

Motivation

  • Prevent mutating the global/shared clock when callers provide a custom clock to startPolling() so multiple pollers can safely run in the same process.
  • Ensure all polling logic (task selection and timeout expiration) consistently uses the supplied getCurrentTime function when provided.
  • Avoid the antipattern of returning the same Date instance from the example and make the README example return a fresh Date per call.

Description

  • Thread the getCurrentTime option through startPolling() into doPoll(), poll() and expireTimedOutTasks() instead of overwriting time.now, implemented in src/taskSchema.js.
  • In poll() use getCurrentTime() (when provided) to compute now for selecting pending tasks and setting startedRunningAt/timeoutAt.
  • Change expireTimedOutTasks() to accept an options object and use getCurrentTime() (when provided) to determine expirations.
  • Update README.md example to return a new Date instance each call: getCurrentTime: () => new Date(currentTime).
  • Add/update the unit test allows startPolling() to use getCurrentTime() in test/task.test.js to verify the custom clock is invoked and tasks run while using the provided clock.

Testing

  • Updated unit test allows startPolling() to use getCurrentTime() was added/modified in test/task.test.js to assert the stubbed getCurrentTime is called and that the scheduled task runs and succeeds.
  • No automated test suite was executed as part of this change, so no pass/fail results are available.

Codex Task

Copilot AI review requested due to automatic review settings February 4, 2026 15:31
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4c5e437327

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/taskSchema.js
Comment on lines 263 to 267
while (true) {
const tasksInProgress = [];
for (let i = 0; i < parallel; ++i) {
const now = time.now();
const now = typeof getCurrentTime === 'function' ? getCurrentTime() : time.now();
const task = await this.findOneAndUpdate(
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Keep custom clock consistent with execute() timing

When startPolling() supplies getCurrentTime, the poller now computes now from that custom clock, but execute() still uses time.now() for startedRunningAt and the schedulingTimeoutAt check. That creates inconsistent timing if callers advance a fake clock (e.g., for tests or demos): tasks that are overdue under the custom clock can still execute because execute() compares schedulingTimeoutAt against real time. This is a behavioral regression compared to the previous global time.now override; if custom time is meant to control polling, consider threading the same clock into execute() (or at least the scheduling timeout check) so timeouts behave predictably under getCurrentTime.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
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 refactors the polling time handling in the task system to support custom clock functions, preventing mutations to the global time module and enabling safer concurrent polling scenarios with independent time sources.

Changes:

  • Thread getCurrentTime option from startPolling() through to poll() and expireTimedOutTasks() functions
  • Add comprehensive test coverage for the custom clock functionality
  • Document the getCurrentTime option in README with a clear example

Reviewed changes

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

File Description
test/task.test.js Adds test case verifying that startPolling() correctly uses a custom getCurrentTime function for task scheduling and execution
src/taskSchema.js Updates startPolling(), expireTimedOutTasks(), and poll() to accept and use a custom getCurrentTime function instead of mutating the global time module
README.md Documents the new getCurrentTime polling option with an example showing how to provide a custom clock for testing or demos

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

@vkarpov15 vkarpov15 merged commit b0eb450 into main Feb 4, 2026
4 checks passed
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