Commit 02995f1
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- packages
- snaps-controllers
- src
- cronjob
- test-utils
- snaps-rpc-methods
- src/permitted
- snaps-sdk/src/types
- methods
- snaps-utils
- src
18 files changed
+1123
-1082
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
3 | | - | |
4 | | - | |
5 | | - | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
6 | 6 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
98 | 98 | | |
99 | 99 | | |
100 | 100 | | |
| 101 | + | |
101 | 102 | | |
102 | 103 | | |
103 | 104 | | |
| |||
0 commit comments