Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make scheduler use lastExecutedAt instead of next planned executeAt #73

Merged
merged 4 commits into from
Dec 3, 2024

Conversation

evlekht
Copy link
Member

@evlekht evlekht commented Nov 26, 2024

  • Improves scheduler initial timer code to support canceling by context.
  • Modifies scheduled job: instead of next execution time, job now stores last execution time.

Summary by CodeRabbit

  • New Features
    • Enhanced job scheduling and execution logic by replacing the ExecuteAt field with LastExecutedAt, improving tracking of job execution times.
  • Bug Fixes
    • Adjusted job execution timing calculations to ensure accurate scheduling based on the new LastExecutedAt field.
  • Tests
    • Updated test cases to reflect changes in job attributes and ensure consistency in job execution timing.
  • Chores
    • Refined logging capabilities during database migrations for better tracking of migration events.

@evlekht evlekht force-pushed the evlekht/cash-in-schedule branch from 200cd6f to cc10514 Compare November 28, 2024 15:49
@evlekht evlekht marked this pull request as ready for review November 28, 2024 16:00
@evlekht evlekht changed the title Evlekht/cash in schedule Make scheduler use lastExecutedAt instead of next planned executeAt Nov 28, 2024
@evlekht evlekht requested review from havan and VjeraTurk November 28, 2024 16:30
havan
havan previously approved these changes Nov 29, 2024
Copy link
Contributor

coderabbitai bot commented Dec 3, 2024

📝 Walkthrough

Walkthrough

The changes in this pull request involve modifications to the job scheduling system within the application. The primary alteration is the replacement of the ExecuteAt column with a new LastExecutedAt column in the jobs table, which is now used to track job execution times more effectively. Additionally, the migration functions have been updated to include a logging parameter, enhancing the logging capabilities during database migrations. Various struct and method signatures have been updated to reflect these changes, ensuring consistency across the job scheduling and execution logic.

Changes

File Change Summary
migrations/scheduler/2_last_executed_at.down.sql Dropped column last_executed_at, added execute_at BIGINT NOT NULL in jobs table.
migrations/scheduler/2_last_executed_at.up.sql Dropped column execute_at, added last_executed_at BIGINT NOT NULL DEFAULT 0 in jobs table.
pkg/database/sqlite/storage.go Updated migrate method signature to include logMigrations boolean. Added migrationLogger type.
pkg/scheduler/job.go Removed field ExecuteAt, added field LastExecutedAt in Job struct.
pkg/scheduler/scheduler.go Updated job scheduling logic to use LastExecutedAt instead of ExecuteAt. Modified method signatures accordingly.
pkg/scheduler/scheduler_test.go Adjusted test cases to reflect changes from ExecuteAt to LastExecutedAt. Updated method signatures.
pkg/scheduler/storage/sqlite/jobs.go Updated job struct to replace ExecuteAt with LastExecutedAt. Modified associated methods for consistency.

Poem

In the meadow where the jobs do play,
A new time's set to guide their way.
LastExecutedAt hops with glee,
While ExecuteAt rests under a tree.
With logs to track each joyful leap,
Our scheduler's dance is now more deep! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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 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: 3

🧹 Outside diff range and nitpick comments (4)
pkg/scheduler/scheduler.go (2)

120-123: Consider adding a log statement on context cancellation

In the select statement, the case for <-timersCtx.Done()> is empty. Adding a log statement here can help with debugging and monitoring by indicating that the timer has been cancelled.


174-183: Consider initializing LastExecutedAt to current time for new jobs

When scheduling a new job (job == nil), LastExecutedAt is set to the zero time (time.Time{}). This causes the job to execute immediately upon scheduler start. If it's preferable for new jobs to have their first execution after a full period, consider initializing LastExecutedAt to s.clock.Now(). This ensures the first execution occurs after the specified period.

pkg/database/sqlite/storage.go (1)

38-40: Consider making logMigrations configurable

Currently, logMigrations is hardcoded to false in the call to s.migrate. Consider adding a configuration option or command-line flag to enable migration logging when needed.

pkg/scheduler/scheduler_test.go (1)

Line range hint 268-273: Consider adding test cases for edge cases

While the current test coverage is good, consider adding test cases for:

  • Zero Period validation
  • Very large Period values
  • Negative LastExecutedAt values
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 804433f and 2df30bd.

📒 Files selected for processing (7)
  • migrations/scheduler/2_last_executed_at.down.sql (1 hunks)
  • migrations/scheduler/2_last_executed_at.up.sql (1 hunks)
  • pkg/database/sqlite/storage.go (3 hunks)
  • pkg/scheduler/job.go (1 hunks)
  • pkg/scheduler/scheduler.go (5 hunks)
  • pkg/scheduler/scheduler_test.go (7 hunks)
  • pkg/scheduler/storage/sqlite/jobs.go (3 hunks)
🔇 Additional comments (9)
pkg/scheduler/scheduler.go (1)

158-159: Update the comment to reflect current behavior

The comment states: "If there is already scheduled job with the same name, then its period is updated." Please verify that this accurately describes the current behavior and update it if necessary.

pkg/scheduler/job.go (1)

6-8: Updated Job struct fields look good

The changes to the Job struct, replacing ExecuteAt with LastExecutedAt, align with the new scheduling logic and are correctly implemented.

pkg/database/sqlite/storage.go (1)

Line range hint 58-90: Conditional migration logging implementation looks good

The addition of migrationLogger and the logMigrations parameter provides flexibility in logging migration details. The implementation correctly adheres to the migrate.Logger interface.

pkg/scheduler/storage/sqlite/jobs.go (3)

19-21: LGTM: Job struct updated to use LastExecutedAt

The struct field change from ExecuteAt to LastExecutedAt is well-implemented with the correct DB tag.


106-116: LGTM: SQL query updated for LastExecutedAt

The SQL upsert query has been properly updated to use last_executed_at in both INSERT and UPDATE clauses.


138-140: LGTM: Model conversion functions updated correctly

The model conversion functions modelFromJob and jobFromModel have been properly updated to handle the LastExecutedAt field while maintaining correct timestamp conversions.

Also applies to: 146-148

pkg/scheduler/scheduler_test.go (3)

31-65: LGTM: Test cases updated with comprehensive coverage

The test setup has been enhanced with:

  • Updated job definitions using LastExecutedAt
  • Added freshJob test case for never-executed jobs
  • Well-defined periods and execution times for each test case

105-128: LGTM: Job execution timing logic updated correctly

The test execution logic has been properly updated to:

  • Track last execution times
  • Calculate next execution based on LastExecutedAt + Period
  • Handle both initial and subsequent executions

Line range hint 278-312: LGTM: Schedule test cases updated appropriately

The Schedule test has been properly updated with:

  • Simplified storage mock function signature
  • Correct handling of LastExecutedAt in test cases
  • Comprehensive coverage for both new and existing jobs

pkg/scheduler/scheduler.go Show resolved Hide resolved
@evlekht evlekht merged commit 73fcb4c into dev Dec 3, 2024
5 checks passed
@evlekht evlekht deleted the evlekht/cash-in-schedule branch December 3, 2024 15:02
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.

3 participants