Skip to content

Conversation

ericallam
Copy link
Member

As they always follow a deterministic step (instead of an interval from an non-deterministic starting point) and previous timestamps don't matter

Copy link
Contributor

coderabbitai bot commented May 23, 2025

Walkthrough

The changes update the scheduling logic across several modules by replacing the use of the calculateNextScheduledTimestamp function with a new function, calculateNextScheduledTimestampFromNow. The new function calculates the next scheduled timestamp based strictly on the current time, removing the previous logic that attempted to skip ahead if the next scheduled time was in the past. The utility module introduces calculateNextScheduledTimestampFromNow and simplifies the original function. Corresponding imports and function calls in presenters, services, and tests are updated to use the new function and interface. No changes were made to the signatures of exported or public entities except for the addition of the new function and a parameter rename for clarity.

Note

⚡️ AI Code Reviews for VS Code, Cursor, Windsurf

CodeRabbit now has a plugin for VS Code, Cursor and Windsurf. This brings AI code reviews directly in the code editor. Each commit is reviewed immediately, finding bugs before the PR is raised. Seamless context handoff to your AI code agent ensures that you can easily incorporate review feedback.
Learn more here.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🔭 Outside diff range comments (6)
apps/webapp/test/calculateNextSchedule.test.ts (6)

15-23: ⚠️ Potential issue

Test logic inconsistency: unused variable and misleading description.

This test defines lastRun but doesn't use it in the function call since calculateNextScheduledTimestampFromNow always calculates from the current time. The test description mentions "recent timestamp" but the function behavior no longer depends on any previous timestamp.

Apply this diff to remove the unused variable and update the test description:

-  test("should calculate next run time for a recent timestamp", () => {
+  test("should calculate next run time from current time", () => {
     const schedule = "0 * * * *"; // Every hour
-    const lastRun = new Date("2024-01-01T11:00:00.000Z"); // 1.5 hours ago

     const nextRun = calculateNextScheduledTimestampFromNow(schedule, null);

25-34: 🛠️ Refactor suggestion

Clean up unused variable.

The lastRun variable is defined but never used since the new function doesn't accept a timestamp parameter.

   test("should handle timezone correctly", () => {
     const schedule = "0 * * * *"; // Every hour
-    const lastRun = new Date("2024-01-01T11:00:00.000Z");

     const nextRun = calculateNextScheduledTimestampFromNow(schedule, "America/New_York");

36-52: ⚠️ Potential issue

Test no longer validates the claimed performance scenario.

This test claims to handle "very old timestamps" for performance, but since calculateNextScheduledTimestampFromNow always calculates from the current time, it's not actually testing performance with old timestamps. The test description and comments are misleading.

The test should either be:

  1. Removed if the performance scenario is no longer relevant
  2. Updated to test a different performance scenario
  3. Renamed to accurately reflect what it's testing
-  test("should efficiently handle very old timestamps (performance fix)", () => {
+  test("should efficiently calculate next schedule from current time", () => {
     const schedule = "*/1 * * * *"; // Every minute
-    const veryOldTimestamp = new Date("2020-01-01T00:00:00.000Z"); // 4 years ago

     const startTime = performance.now();
     const nextRun = calculateNextScheduledTimestampFromNow(schedule, null);
     const duration = performance.now() - startTime;

-    // Should complete quickly (under 10ms) instead of iterating millions of times
+    // Should complete quickly (under 10ms)
     expect(duration).toBeLessThan(10);

54-62: 🛠️ Refactor suggestion

Update test description and remove unused variable.

Similar issue: the test references behavior "when timestamp is within threshold" but no timestamp is being passed to the function.

-  test("should still work correctly when timestamp is within threshold", () => {
+  test("should work correctly with 2-hour intervals from current time", () => {
     const schedule = "0 */2 * * *"; // Every 2 hours
-    const recentTimestamp = new Date("2024-01-01T10:00:00.000Z"); // 2.5 hours ago

     const nextRun = calculateNextScheduledTimestampFromNow(schedule, null);

-    // Should properly iterate: 10:00 -> 12:00 -> 14:00 (since current time is 12:30)
+    // Should return next 2-hour interval: 14:00 (since current time is 12:30)
     expect(nextRun).toEqual(new Date("2024-01-01T14:00:00.000Z"));
   });

64-77: 🛠️ Refactor suggestion

Multiple tests have similar issues with unused variables and misleading descriptions.

These tests all define timestamp variables that are no longer used and have descriptions that reference behavior based on those timestamps. Since calculateNextScheduledTimestampFromNow always calculates from the current time, these tests need significant updates.

Each of these tests should be reviewed to either:

  1. Remove unused timestamp variables
  2. Update test descriptions to accurately reflect what's being tested
  3. Update comments that reference the old behavior
  4. Consider if some tests are now redundant since they all calculate from the same "now" time

For example, lines 64-77 claim to test "frequent schedules with old timestamps efficiently" but don't actually pass any old timestamp to the function.

Also applies to: 79-90, 108-122, 124-138, 140-156, 158-170


173-448: ⚠️ Potential issue

Fuzzy testing section has systematic issues with unused variables.

The fuzzy testing generates random timestamps via generateRandomTimestamp() and assigns them to variables like lastTimestamp, but these are never used in the function calls. This means the fuzzy testing isn't actually testing the claimed scenarios.

Key issues:

  1. Line 252: lastTimestamp is generated but never used
  2. Line 265: Logic checks lastTimestamp but the function doesn't use it
  3. Line 292: veryOldTimestamp is generated but never used
  4. Line 322: lastTimestamp is generated but never used
  5. Line 377: lastTimestamp is generated but never used
  6. Line 407: lastTimestamp is generated but never used

The fuzzy testing needs to be completely rethought since the function behavior has fundamentally changed. Consider:

  1. Removing timestamp generation logic that's no longer relevant
  2. Updating invariant checks that reference old behavior
  3. Focusing fuzzy testing on aspects that are still relevant (schedule parsing, timezone handling, etc.)
🧹 Nitpick comments (1)
apps/webapp/app/v3/services/registerNextTaskScheduleInstance.server.ts (1)

32-34: Consider updating the span attribute for consistency.

The span still sets a "last_scheduled_timestamp" attribute, but the actual calculation no longer uses this value since we're now calculating from the current time. Consider updating this attribute name to reflect the current behavior or removing it if it's no longer relevant for debugging/monitoring.

         span.setAttribute(
-          "last_scheduled_timestamp",
+          "calculation_base_timestamp",
           instance.lastScheduledTimestamp?.toISOString() ?? new Date().toISOString()
         );
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1a6a9ca and f33b638.

📒 Files selected for processing (5)
  • apps/webapp/app/presenters/v3/ScheduleListPresenter.server.ts (2 hunks)
  • apps/webapp/app/v3/services/registerNextTaskScheduleInstance.server.ts (2 hunks)
  • apps/webapp/app/v3/services/upsertTaskSchedule.server.ts (2 hunks)
  • apps/webapp/app/v3/utils/calculateNextSchedule.server.ts (1 hunks)
  • apps/webapp/test/calculateNextSchedule.test.ts (21 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (4)
apps/webapp/app/v3/services/upsertTaskSchedule.server.ts (1)
apps/webapp/app/v3/utils/calculateNextSchedule.server.ts (1)
  • calculateNextScheduledTimestampFromNow (3-5)
apps/webapp/app/v3/services/registerNextTaskScheduleInstance.server.ts (1)
apps/webapp/app/v3/utils/calculateNextSchedule.server.ts (1)
  • calculateNextScheduledTimestampFromNow (3-5)
apps/webapp/test/calculateNextSchedule.test.ts (1)
apps/webapp/app/v3/utils/calculateNextSchedule.server.ts (1)
  • calculateNextScheduledTimestampFromNow (3-5)
apps/webapp/app/presenters/v3/ScheduleListPresenter.server.ts (1)
apps/webapp/app/v3/utils/calculateNextSchedule.server.ts (1)
  • calculateNextScheduledTimestampFromNow (3-5)
⏰ Context from checks skipped due to timeout of 90000ms (25)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (10, 10)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (9, 10)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (8, 10)
  • GitHub Check: units / internal / 🧪 Unit Tests: Internal (8, 8)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (7, 10)
  • GitHub Check: units / internal / 🧪 Unit Tests: Internal (7, 8)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (6, 10)
  • GitHub Check: units / internal / 🧪 Unit Tests: Internal (6, 8)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (5, 10)
  • GitHub Check: units / internal / 🧪 Unit Tests: Internal (5, 8)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (4, 10)
  • GitHub Check: units / internal / 🧪 Unit Tests: Internal (4, 8)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (3, 10)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (2, 10)
  • GitHub Check: units / internal / 🧪 Unit Tests: Internal (3, 8)
  • GitHub Check: units / webapp / 🧪 Unit Tests: Webapp (1, 10)
  • GitHub Check: units / internal / 🧪 Unit Tests: Internal (2, 8)
  • GitHub Check: units / packages / 🧪 Unit Tests: Packages (1, 1)
  • GitHub Check: units / internal / 🧪 Unit Tests: Internal (1, 8)
  • GitHub Check: e2e / 🧪 CLI v3 tests (windows-latest - pnpm)
  • GitHub Check: e2e / 🧪 CLI v3 tests (windows-latest - npm)
  • GitHub Check: e2e / 🧪 CLI v3 tests (ubuntu-latest - pnpm)
  • GitHub Check: typecheck / typecheck
  • GitHub Check: e2e / 🧪 CLI v3 tests (ubuntu-latest - npm)
  • GitHub Check: Analyze (javascript-typescript)
🔇 Additional comments (10)
apps/webapp/app/v3/utils/calculateNextSchedule.server.ts (2)

3-5: LGTM! Well-designed abstraction.

The new function provides a clean, purpose-built interface for calculating next scheduled timestamps from the current time, which aligns perfectly with the PR objective of making scheduling deterministic based on "now".


7-13: LGTM! Simplified and clearer logic.

The function simplification removes the complex skipping logic and the parameter rename from lastScheduledTimestamp to currentDate improves clarity. The default value ensures backward compatibility.

apps/webapp/app/v3/services/upsertTaskSchedule.server.ts (2)

7-7: LGTM! Import updated correctly.

The import change from calculateNextScheduledTimestamp to calculateNextScheduledTimestampFromNow is consistent with the new API.


265-268: LGTM! Function usage updated appropriately.

The function call now correctly uses calculateNextScheduledTimestampFromNow which ensures that nextRun is always calculated from the current time, aligning with the PR objective of deterministic scheduling based on "now".

apps/webapp/app/presenters/v3/ScheduleListPresenter.server.ts (2)

8-8: LGTM! Import updated correctly.

The import change is consistent with the new scheduling API that calculates timestamps from the current time.


260-263: LGTM! Function usage updated appropriately.

The updated function call ensures that the nextRun timestamp in the schedule list is always calculated from the current time, providing users with accurate information about when each schedule will next execute.

apps/webapp/app/v3/services/registerNextTaskScheduleInstance.server.ts (2)

2-2: LGTM! Import updated correctly.

The import change is consistent with the new scheduling API that calculates timestamps from the current time.


36-39: LGTM! Function usage updated appropriately.

The function call now correctly uses calculateNextScheduledTimestampFromNow, ensuring that the next scheduled timestamp is always calculated from the current time rather than from a previous timestamp.

apps/webapp/test/calculateNextSchedule.test.ts (2)

2-2: Import update looks good.

The import statement has been correctly updated to use the new function.


4-4: Test suite name updated appropriately.

The test suite description has been correctly updated to reflect the new function being tested.

@ericallam ericallam merged commit 3fc5630 into main May 23, 2025
35 checks passed
@ericallam ericallam deleted the ea-branch-70 branch May 23, 2025 15:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants