Skip to content

Commit 02995f1

Browse files
authored
refactor!: Refactor cronjob controller to reduce duplication (#3421)
The cronjob controller contained a lot of essentially duplicated logic after the addition of background events. To resolve this, I've refactored the cronjob controller to treat cronjobs as recurring background events. This means: - Cronjobs and background events are now (re)scheduled and executed using the same logic. - Cronjobs are stored in state just like background events. - I removed the `lastRun` property in favour of a `date` property. We can now simply check if the current date is past the scheduled date to know if a cronjob should be executed immediately. - This simplifies the logic for rescheduling cronjobs significantly. - Cronjobs can now use ISO 8601 durations as well as the usual cron expression. - The logic for getting all cronjobs from all Snaps was removed, since the full cronjobs are stored in state now. - This works fine for new Snaps, but I need to consider how to handle existing Snaps at the time of the cronjob controller update, either with a migration or by re-adding some of this logic. In addition to these changes, I've made some smaller changes to the controller like replacing TypeScript's `private` modifier with true hash private functions. ## Breaking changes - The `jobs` state property was removed, cronjobs are now stored in the `events` property as well. - `CronjobController:schedule` now expects a `schedule` field instead of `date`. - Some actions were renamed to remove the `BackgroundEvent` suffix. - `CronjobController:scheduleBackgroundEvent` -> `CronjobController:schedule` - `CronjobController:cancelBackgroundEvent` -> `CronjobController:cancel`. - `CronjobController:getBackgroundEvents` -> `CronjobController:get`.
1 parent 8854c9f commit 02995f1

File tree

18 files changed

+1123
-1082
lines changed

18 files changed

+1123
-1082
lines changed
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"branches": 95.26,
3-
"functions": 98.42,
4-
"lines": 98.79,
5-
"statements": 98.62
2+
"branches": 95.29,
3+
"functions": 98.65,
4+
"lines": 98.83,
5+
"statements": 98.66
66
}

packages/snaps-controllers/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
"@xstate/fsm": "^2.0.0",
9999
"async-mutex": "^0.5.0",
100100
"concat-stream": "^2.0.0",
101+
"cron-parser": "^4.5.0",
101102
"fast-deep-equal": "^3.1.3",
102103
"get-npm-tarball-url": "^2.0.3",
103104
"immer": "^9.0.6",

0 commit comments

Comments
 (0)