Repository navigation
Add server-side scheduling that runs jobs the way the cloud does - #179
Conversation
297fc12 to
59bf2e6
Compare
59bf2e6 to
1cf1dd1
Compare
|
A status note, in case it helps with review: this PR is still mergeable and clean against main after today's commits, and I have not needed to change it since the last push. Other work now builds on it, so it is the piece the rest is waiting for:
If it is easier to review in pieces, I can split it: the store and its API first, then the executor that fires jobs. Just say which you would prefer, or point me at anything you want changed first. |
a14aa15 to
ce54133
Compare
|
Test report: S6 MaxV ( Before this PR (on
With this PR
One rough edge: right after a container restart, |
|
Thanks for the thorough test. I fixed the restart traceback: a timed-out MQTT read was being caught by the save-failure handler because TimeoutError is an OSError, so it now logs quietly at debug level and only real save failures warn. I'm leaving the full-entry timer format as an observation for now, since the sync only replays what the app sent. |
The map backup and capabilities probes reach the robot through the routine runner's get_routine_runner(), which comes with the schedule store (Python-roborock#179). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
@C9H13NO3-dev I appreciate you testing out this PR! @virtitnerd - this PR has conflicts now, can you fix? |
1927b43 to
a1a1991
Compare
|
Rebased on the current main and fixed the conflicts. Same 9 commits. The conflicts were in |
|
Follow-up test on the rebased head
Thanks for the quick fix! |
Lash-L
left a comment
There was a problem hiding this comment.
Automated review: changes requested
The core design holds up. The store, the at-most-once occurrence claims, atomic persistence (temp file, fsync, os.replace) and restart/rollback safety are all sound, and 611 tests pass at a1a1991.
Confirmed:
- The four job routes match
IotApi.java: GET/POSTuser/devices/{duid}/jobsand PUT/DELETE.../jobs/{jobId}. TimerBeanhas the fieldscron, enabled, id, param, repeated.- The app's native flow (
set_server_timerto the robot, then a cloud job{id, method:"server_scheduled_start", params}viarrAddOrEditTimer) matches your job model. TimerBeanhas notimeZoneId, so defaulting tonetwork.timezoneis reasonable.- Scene TIMER triggers are correctly left alone.
Blocking:
- The official app's
set_timerlayout is rejected (shared/timer_rpc.py:88-104, interceptor at:183-228).- The app writes
[[id, [cron, ["start_clean", {...}]]]], with no status element (tanosmodule_3621.jssetTimerOnRobot; patrol timers useapp_start_patrol). _parse_set_timerreads that as status=cron and cron="start_clean", giving('id', False, 'start_clean', {}), and the client gets back an error with code -1.- Because every
get/set/upd/del_timeris now answered locally, the robot's own legacy timers also disappear fromget_timer. - The tests only cover LocalRock's
["on", [cron, action]]layout.
- The app writes
- The write responses probably don't match the app's types (
routes/user/devices/jobs.py:101,148-149,214).IotApideclares POST/PUT/DELETE asApiReturn<String>; only GET isApiReturn<List<TimerBean>>. Returning the job object inresultwould likely fail Gson parsing, so the app would show failure even though the job was saved. Not tested live. - Native dispatch and sync assume
[name, "on"|"off", …](routine_runner.py_run_steps,native_schedule_sync.py:29-37,97-111). The S6 MaxV reported["Y <id> 1 1 <cron> app_start", 0], andupd_server_timerneeds the full entry string. On that firmware, native-linked jobs never match, so they don't fire and enable/disable never syncs. The successful S6 run was on a head from before #213's verification step.
Should fix:
4. Constant polling. Every device with a native-linked job gets a new MQTT connection and a get_server_timer call every 30s, indefinitely (native_schedule_sync.py:240-266). The scheduler also reloads the whole inventory JSON every 20s (schedule_runner.py:268). Both block the event loop.
5. Smaller issues:
- The PR body's fallback description contradicts
docs/schedules.mdandfallback_commands_for_step, which say native starts never fall back. - Day names like
MONare rejected (schedule_runner.py:95-98). - A failed
set_timersave raises inside the interceptor, and the request is then forwarded to the robot anyway.
- Please document the missed-run behaviour.
- There's no catch-up, and the first tick only sets a baseline, so a job due during a restart is lost.
- An occurrence is consumed when the robot is offline or a routine is running.
- A one-time job is then disabled.
Conflicts: clean with main, #224, #283 and #285. #247 conflicts in routine_runner.py and its tests; keep both sides.
Tests: add the official app's real set_timer payloads, get_server_timer replies from other firmware, and the write response shape.
I've converted this PR to draft while these are addressed. Mark it ready for review again once they're fixed and it will go through another review.
This is an automated review by Claude Code, checked against the decompiled Roborock app (APK + device plugin bundles), python-roborock and a vacuum firmware dump. It can be incorrect, so please reply if any finding looks wrong.
A cron such as "00 11 ? * MON-FRI" was rejected by the scheduler and by the job routes. Day names (SUN to SAT) and month names (JAN to DEC) now work in lists, ranges and steps, in any case. get_timer writes them out as numbers, since robot clients read digits. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…les.md States what happens to an occurrence when the server is down, the robot is offline or busy, or its native timer is off: no catch-up, the first tick is only a baseline, the occurrence is consumed, and a one-time job is disabled either way. Also records the string results of the job writes (unverified against the cloud), how get_timer is merged, the S6 MaxV timer layout and the read back-off. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The official app posts a new schedule with "id":"" and expects the cloud to assign one. The store only generated an id for None, so the job was saved with an empty id, the create answered with an empty string, a second create was refused as a duplicate and the scheduler skipped the job. None, an empty string and whitespace now all mean assign an id. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Writes replace the file through a temp file, so two writes inside one coarse mtime tick that leave the size unchanged would have been missed. The file id changes with every replacement. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
MON-SUN, FRI-SUN and 6-0 style ranges were rejected as out of range; they now run to the end of the week or wrap past Saturday. A day-of-month or day-of-week field that starts with * (such as */2) now counts as unrestricted, which is how Vixie cron decides between AND and OR for the two day fields (from memory of its entry.c, not re-checked against the source here; the cloud's own rule is not known). Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…bot errors - With the store holding timers and the robot offline, a forwarded get_timer got no reply at all. The bridge now asks the interceptor for a fallback and, if the robot has not answered after 6 seconds, replies with the store's timers. - A robot error reply to get_timer was turned into a success carrying only the store's timers. Error replies now pass through unchanged. - One imported job with a cron get_timer cannot show (for example an unknown day name) raised KeyError and hid every other timer. That job is skipped. get_timer also writes Sunday as 0 and expands wrapping ranges for clients that only know 0 to 6. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Comments and docstrings added by this PR are cut to two lines, and the two overlapping missed-run sections of schedules.md are one. The docs also state the get_timer fallback. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
a1a1991 to
e3ca337
Compare
|
Thanks for the careful review. Everything below is on the pushed head Blocking
Should fix
Tests. Added: the official app's Conflicts. #247 still conflicts in For #213 (pponce). It builds on this PR, so these are the names to know: The PR description is updated to match. I will mark this ready for review again. |
Lash-L
left a comment
There was a problem hiding this comment.
Automated review: passed (nits) (re-review at e3ca3378)
Now that this has passed the automated review, I'll do a human review and then either request changes or merge it.
Earlier findings:
| Finding | Status |
|---|---|
The official app's set_timer layout was rejected, and the robot's own timers hidden |
Fixed. [id,[cron,[action,{…}]]] is accepted. Patrol timers go to the robot. get_timer asks the robot and adds the store's entries to its reply, with a 6s fallback. Unknown ids for upd_timer/del_timer are forwarded. Matches tanos setTimerOnRobot. |
Write responses didn't match ApiReturn<String> |
Fixed. POST returns the id as a string and PUT/DELETE return "done" (IotApi.java). The exact cloud strings are unverifiable. |
Native timers assumed [name,"on"/"off"] |
Fixed in unit tests. Both layouts are parsed, and upd_server_timer sends the full entry string. Not verified on an S6, including whether it accepts server_scheduled_start. |
| Polling and inventory reloads | Mostly fixed. One reused MQTT connection per robot with back-off, and the inventory is cached. A 30s get_server_timer read per robot remains, which is acceptable. |
Fallback description, MON day names, failed save still forwarded, missed-run docs |
Fixed. |
Worth fixing before merge:
- Editing a robot-owned legacy timer creates a copy (
timer_rpc.py:162-182).- The app re-sends
set_timerwith the existing timer's id (name = state.name || …). - The store saves a copy while the robot keeps the original, and
merge_responsethen hides the store's copy, so the edit looks lost. - On firmware that runs its own timers, the robot could also start a second clean.
- Fix: forward
set_timerto the robot when the store doesn't own the id.
- The app re-sends
- The app may send a positional action,
["start_clean", fan_power, segments(, map_id)](tanosmodule_3621.js~4229)._parse_set_timerwould then see no settings and run a whole-home clean at the default fan power. The decompiled code is ambiguous here, so map the positional fields or forward the request. - Merging with #294 breaks this PR. #294 deletes
DEFAULT_TIMEZONE, whichjobs.py:8andschedule_runner.py:14import, sotests/test_schedules.pyandtests/test_timer_rpc.pyfail with anImportError. Use #294'sdefault_timezone(ctx)instead, which also stops EU users falling back to New York time.
Minor:
- A robot error reply to
get_timerhides the store's timers. _pendingentries are never cleared after a merge.SUN-SUNparses as the whole week.
Checks: all 692 tests pass. It merges cleanly with main. #247 still conflicts in routine_runner.py; keep both sides.
This is an automated review by Claude Code, checked against the decompiled Roborock app (APK + device plugin bundles, refreshed 2026-11-21), python-roborock and a vacuum firmware dump. It can be incorrect, so please reply if any finding looks wrong.
PR 294 deletes shared.constants.DEFAULT_TIMEZONE, which jobs.py and schedule_runner.py import, so merging both broke test collection. server_timezone() uses data_helpers.default_timezone(ctx) when it exists (294) and otherwise the configured zone, then America/New_York, so the branch works with and without 294. ScheduleRunner and the timer interceptor now fall back to it instead of a fixed New York default, so EU installs get their region's zone once 294 lands. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
With 294 merged, default_timezone(ctx) reads ctx.timezone and raised on the minimal contexts several tests and callers pass. Fall back to the configured zone and then America/New_York instead of failing. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
A robot that answers get_timer with an error (firmware without the legacy timers) made the app show that error and none of the store's schedules. Reply with the store's timers instead; with an empty store the error still passes through unchanged. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Entries lingered for 30 seconds after the merge and only left when the next get_timer pruned them. The reply path now removes its own entry; the fallback still sees the answered flag on the object it holds. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
The app edits a legacy timer by re-sending set_timer with its id. The store saved a copy while the robot kept the original, and the merged get_timer then hid the copy, so the edit looked lost (and a robot running its own timers could start twice). The interceptor now remembers the ids in each robot's last get_timer reply and forwards set_timer for one the store does not own. New ids still go to the store. The list is in memory, so it is empty after a server restart until the app reads the timer list again, which it does before offering an edit. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
For robots without order-segment clean the app sends ["start_clean", fan_power, segments(, map_id)] (decompiled timer screen builds mode as fan_power). _parse_set_timer saw no settings object and stored a whole-home clean at the default fan power. Map fan_power and segments; forward the request to the robot when a map id is present or the fan power is not a number, since the store has no per-map timers and the exact map id meaning is unverified. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
A range ending on SUN was stretched to the end of the week, which turned SUN-SUN into every day. The stretch now applies only when the range starts after Sunday. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Python-roborock#179, Python-roborock#213 and Python-roborock#214 import DEFAULT_TIMEZONE from shared.constants. Removing it would break them with an ImportError, whichever PR merges second. default_timezone now falls back to it instead of a new FALLBACK_TIMEZONE, so the value stays America/New_York. config.example.toml now asks users to set network.timezone if their timezone differs from the region default, such as Europe/London in the eu region.
Lash-L
left a comment
There was a problem hiding this comment.
Thanks for the back and forth on this. Looks good and I appreciate the contribution!
GET /user/devices/<duid>/jobs computed nextFireTime in network.timezone, else America/New_York. The schedule runner of Python-roborock#179 uses default_timezone, which falls back to the region's zone. Take an eu server without network.timezone and a job at 08:00 without timeZoneId: the job ran at 08:00 in Berlin, but its nextFireTime named 08:00 in New York. The route now uses default_timezone too.
Python-roborock#179, Python-roborock#213 and Python-roborock#214 import DEFAULT_TIMEZONE from shared.constants. Removing it would break them with an ImportError, whichever PR merges second. default_timezone now falls back to it instead of a new FALLBACK_TIMEZONE, so the value stays America/New_York. config.example.toml now asks users to set network.timezone if their timezone differs from the region default, such as Europe/London in the eu region.
GET /user/devices/<duid>/jobs computed nextFireTime in network.timezone, else America/New_York. The schedule runner of Python-roborock#179 uses default_timezone, which falls back to the region's zone. Take an eu server without network.timezone and a job at 08:00 without timeZoneId: the job ran at 08:00 in Berlin, but its nextFireTime named 08:00 in New York. The route now uses default_timezone too.
) * fix(devices): default the device time zone to the server time zone Devices without their own time zone got America/New_York in the home data and in the device detail. network.timezone and the region reached only the location route. The device lists now use the same default: network.timezone if set, else the time zone of the region, for example Europe/Berlin for eu. * fix(devices): keep DEFAULT_TIMEZONE as the fallback of default_timezone #179, #213 and #214 import DEFAULT_TIMEZONE from shared.constants. Removing it would break them with an ImportError, whichever PR merges second. default_timezone now falls back to it instead of a new FALLBACK_TIMEZONE, so the value stays America/New_York. config.example.toml now asks users to set network.timezone if their timezone differs from the region default, such as Europe/London in the eu region. * fix(devices): compute nextFireTime in the schedule runner's time zone GET /user/devices/<duid>/jobs computed nextFireTime in network.timezone, else America/New_York. The schedule runner of #179 uses default_timezone, which falls back to the region's zone. Take an eu server without network.timezone and a job at 08:00 without timeZoneId: the job ran at 08:00 in Berlin, but its nextFireTime named 08:00 in New York. The route now uses default_timezone too. * test(schedules): cover server_timezone with the real region helper The comment that main has no data_helpers.default_timezone goes stale with this branch, so it now says what the test covers.
Problem
Roborock schedules are run by the cloud: the official app writes each one as a job (
/user/devices/{duid}/jobs) and the cloud starts the clean when it is due. Against this server those calls had nowhere to go, so clients like LocalRock saw no schedules, could never create one, and a schedule made in the official app never ran. This covers both the legacyget/set/upd/del_timerrobots and robots on the newerserver_timerprotocol.Changes
/jobscalls). POST returns the new job id as a string and PUT/DELETE return"done", becauseIotApideclares these calls asApiReturn<String>(the exact cloud strings are unverified). Creating with an empty id, which is what the official app posts, assigns one.set_timer, andupd_timer/del_timerfor a stored job, are answered from the store, in LocalRock's layout and in the official app's (no status element).get_timergoes to the robot and the store's jobs are added to its reply; if the robot has not answered after 6 seconds, the store's timers are sent alone. Timers the store does not hold, and patrol timers, go to the robot. Editing merges settings rather than resetting them; an empty action object leaves existing settings alone. One-time schedules (day and month pinned, e.g.15 23 18 10 *) are listed with their date, the form the robot and the official app use. A settings entry'sname, the official app's link to the robot-side timer, is kept so a client can find that timer to edit or delete it.MON-FRI,JAN) and weekday ranges that wrap or reach Sunday.name) is run the way the cloud runs it: the robot is sentserver_scheduled_startwith the job's settings entry. The robot accepts it for room and whole-home schedules, and for a room schedule it records the clean as "Schedules" in its history. A job without a timer id (one saved through legacyset_timer) is started with the plainer commands: a whole-home clean is oneapp_startcarrying the schedule's fields plussource: 3(which the robot also records as "Schedules"), and a room schedule is the settings commands followed by a segment clean. If the robot refuses that singleapp_startwith -10005, the settings are sent as separate commands. A job that has a timer id is never retried another way: a refusal can mean the timer is off or gone, and an ordinary clean would bypass that. Scenes and routines are untouched; the new step types are produced only by schedules.[name, on|off]layout and the S6 MaxV layout (["Y <name> 1 1 <cron> app_start", 0], whereupd_server_timerneeds the whole string).docs/schedules.md.Deliberately out of scope
get/set/upd/del_server_timerare not answered from the store. They are forwarded to the robot, which keeps those entries and from which the official app reads them back. Answering them locally, which I tried and reverted during development, hid the robot's real schedules from every client, and the official app then deleted the entries it no longer recognized. A parametrized test guards that this stays so. What runs aserver_timerschedule here is its/jobsrecord, which the runner fires like any other job.How the start was found
The robot does not run a
server_timerentry by itself: with the runner told to skip those jobs, four scheduled times passed with the robot idle. So the server has to start them. Starting a clean with the settings commands and an emptyapp_startworks but the robot records it as started by "App". Sendingserver_scheduled_start(the method name inside the job) with only the job's settings is refused with-10005 "Invalid timer", and with a bare id with"Param should be name + fan + segment"; with the job's whole settings entry includingnameit runs (a room schedule's history reads "Schedules | Rooms"). Separately,app_starttakes asourcefield andsource: 3is recorded as "Schedules" (other spellings tried, such asstart_type, are ignored).Merge order and dependencies
routine_runner.pyand its tests. Whichever lands second keeps both sides.Related
#88 is the same territory; I held this back for a bit specifically to avoid stepping on that work. Happy to coordinate however's easiest, this doesn't need to be the one that lands if yours is further along, consider it an alternative or reference rather than a race.
Tested
pytest: 692 passed. Verified live on a Roborock Qrevo. End to end: a one-time whole-home schedule created from LocalRock was run by the server at its scheduled minute with exactly one command,server_scheduled_startcarrying the job's settings entry, which the robot answeredok; the robot's history shows it as "Schedules | Full". Commands sent to the robot directly:server_scheduled_startfor a room schedule (history: "Schedules | Rooms"), andapp_startwithsource: 3(history: "Schedules | Full"). The fallback paths are covered by unit tests against a fake robot that answers-10005.Not run on hardware by me: the S6 MaxV native layout and the official app's
set_timerpayloads are covered by unit tests only. C9H13NO3-dev ran an S6 MaxV (a10, firmware02.62.46) against earlier heads and reported schedules firing (see the comments above); the changes since then have not been run on an S6. The write response strings are not verified against the real cloud.Closes #88