Skip to content

Conversation

@brookqin
Copy link
Contributor

@brookqin brookqin commented Nov 19, 2025

hi @charlie0129,

I'm working on schedule command, will add scheduled calibration functionality to batt, allowing users to set up and manage calibration schedules using cron expressions.

Scheduled calibration feature:

  • Added a new /calibration/schedule endpoint to accept a cron expression or null to enable or disable scheduling.
  • Added the CLI command batt schedule.
  • Before executing a scheduled task, a notification is sent and the daemon waits for 5 minutes. If no explicit postpone/skip command is received, the calibration process begins.
  • If the power is not connected, the scheduled task will not start until plugged in.
  • During calibration, users can control the status via the calibrate command.

CLI examples:

batt schedule '0 10 * * 0'   # every Sunday at 10:00 (quote * so the shell doesn't expand it)
batt schedule disable        # remove the schedule and stop future runs
batt schedule postpone 90m   # push the next run back by 90 minutes (defaults to 1h if omitted)
batt schedule skip           # skip only the upcoming run and keep the schedule

# Conflicts:
#	pkg/daemon/calibration.go
@brookqin brookqin marked this pull request as ready for review November 29, 2025 08:10
Copilot AI review requested due to automatic review settings November 29, 2025 08:10
Copilot finished reviewing on behalf of brookqin November 29, 2025 08:14
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 adds scheduled automatic calibration functionality to batt, allowing users to configure recurring calibration runs using cron expressions. The feature includes a 5-minute lead notification period before execution and supports postpone/skip operations.

Key changes:

  • New Scheduler component with cron-based scheduling and precheck retry logic
  • REST API endpoints (/schedule, /schedule/postpone, /schedule/skip) for schedule management
  • CLI command batt schedule with subcommands for setting, disabling, postponing, and skipping schedules

Reviewed changes

Copilot reviewed 14 out of 15 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
pkg/daemon/scheduler.go Core scheduler implementation with cron parsing, timer management, and control message handling
pkg/daemon/scheduler_test.go Unit tests for scheduler functionality including postpone, skip, and precheck failure scenarios
pkg/daemon/daemon.go Scheduler initialization with task and precheck callbacks; adds schedule-related event publishing
pkg/daemon/calibration.go Schedule persistence logic and helper functions for schedule, postpone, and skip operations
pkg/daemon/handlers.go HTTP handlers for schedule management endpoints with validation
pkg/config/config.go Config interface additions for cron expression getter/setter
pkg/config/file.go File-based config implementation for persisting cron expressions
pkg/client/apis.go Client API methods for schedule operations
cmd/batt/schedule.go CLI command implementation for schedule management
cmd/batt/main.go Registration of schedule command
README.md Documentation for scheduled calibration feature
go.mod, go.sum Added robfig/cron/v3 dependency
pkg/calibration/types.go New action types and ScheduledAt field for status

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

@brookqin brookqin requested a review from Copilot November 29, 2025 09:31
Copilot finished reviewing on behalf of brookqin November 29, 2025 09:36
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

Copilot reviewed 14 out of 15 changed files in this pull request and generated 9 comments.


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

@brookqin
Copy link
Contributor Author

brookqin commented Dec 1, 2025

@charlie0129 pls take a look.

I am still testing.
The core logic is mainly in the scheduler.go, which automatically calls calibration-related api through Timer.

@charlie0129
Copy link
Owner

@brookqin Thanks! I will review and test it in this week when I am free.

Aliases: []string{"sch"},
Short: "Manage automatic calibration schedule",
Long: `Examples:
batt schedule '0 10 * * 0' # Note: Cron expressions containing * must be quoted!
Copy link
Owner

Choose a reason for hiding this comment

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

Maybe add some examples on cron expressions?

For example:

batt schedule 'minute hour day month weekday'
batt schedule '0 10 * * 0' (At 10:00 on Sunday)
batt schedule '0 10 1 * *' (At 10:00 on the first day of every month)
batt schedule '0 10 1 */2 *' (At 10:00 on the first day of every two months)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

@charlie0129
Copy link
Owner

By the way, do you know what timezone is the cron using? UTC or local timezone?

@brookqin
Copy link
Contributor Author

brookqin commented Dec 5, 2025

By the way, do you know what timezone is the cron using? UTC or local timezone?

time.Now() should be UTC.

Scheduler may have a bug, every time computer goes to sleep, timer trigger will be delayed.

time: NewTimer firing later if computer sleeps, how to use wall clock?
time: add ExternalNow, etc for external time and timers

seems have to use a loop with a short timer.

@charlie0129
Copy link
Owner

Scheduler may have a bug, every time computer goes to sleep, timer trigger will be delayed.

You mean if a timer is supposed to fire in 5 minutes, but if the computer goes to sleep now, the timer will fire 5 minutes after waking up?

I think I experienced the opposite. If the computer goes to sleep, the timer will fire at the correct time. If the computer slept for longer than 5 minutes, it fires immediately after waking up. Correct me if I am wrong.

@charlie0129
Copy link
Owner

charlie0129 commented Dec 5, 2025

I think the cron should use user's local time. Using UTC will cause confusion for users. Adding TZ=xx CRON_TZ=xx to user's cron expression should be enough.

@brookqin
Copy link
Contributor Author

brookqin commented Dec 5, 2025

I think the cron should use user's local time. Using UTC will cause confusion for users. Adding TZ=xx CRON_TZ=xx to user's cron expression should be enough.

you are right, I will fix this.

time="2025-12-01T19:29:25+08:00" level=debug msg="upcoming scheduled task at 2025-11-30 10:00:00"
time="2025-12-01T19:29:25+08:00" level=debug msg="running scheduled task at 2025-11-30 10:00:00"

My mac sleep whole day last sunday, seems there are issues with both time zone and timer.

@charlie0129
Copy link
Owner

Maybe the implementation of robfig/cron will be prolonged by sleep?

I remember that time.After will not be affected by sleep.

I will check how robfig/cron handles this.

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