Skip to content

Commit b485691

Browse files
fix: Clear cronjobs when installing a Snap (#3514)
Clear cronjobs when installing Snaps, this fixes an issue with local Snaps. Fixes #3513
1 parent 0e13483 commit b485691

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

packages/snaps-controllers/src/cronjob/CronjobController.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,22 @@ describe('CronjobController', () => {
498498

499499
const cronjobController = new CronjobController({
500500
messenger: controllerMessenger,
501+
state: {
502+
events: {
503+
[`cronjob-${MOCK_SNAP_ID}-0`]: {
504+
id: `cronjob-${MOCK_SNAP_ID}-0`,
505+
recurring: true,
506+
date: '2022-01-01T00:01:00.000Z',
507+
schedule: '* * * * *',
508+
scheduledAt: '2022-01-01T00:00:00.000Z',
509+
snapId: MOCK_SNAP_ID,
510+
request: {
511+
method: 'exampleMethodTwo',
512+
params: ['p1'],
513+
},
514+
},
515+
},
516+
},
501517
});
502518

503519
cronjobController.init();
@@ -533,6 +549,8 @@ describe('CronjobController', () => {
533549
},
534550
);
535551

552+
expect(rootMessenger.call).toHaveBeenCalledTimes(2);
553+
536554
cronjobController.destroy();
537555
});
538556

packages/snaps-controllers/src/cronjob/CronjobController.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,8 @@ export class CronjobController extends BaseController<
521521
* @param snap - Basic Snap information.
522522
*/
523523
readonly #handleSnapInstalledEvent = (snap: TruncatedSnap) => {
524+
// In case of local Snaps, they may already have cronjobs that should be cleared.
525+
this.unregister(snap.id);
524526
this.register(snap.id);
525527
};
526528

0 commit comments

Comments
 (0)