-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Document Snaps cron jobs feature (#1271)
* Document Snaps cron jobs feature * update what's new * address reviewer feedback
- Loading branch information
1 parent
b20a6c8
commit 5702d1e
Showing
11 changed files
with
108 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
--- | ||
description: Schedule periodic actions for your users. | ||
sidebar_position: 1 | ||
--- | ||
|
||
# Cron jobs | ||
|
||
You can schedule actions to run periodically at fixed times or intervals, also known as "cron jobs." | ||
For example, you can display a dialog or notification in MetaMask at a specific time each day. | ||
|
||
## Steps | ||
|
||
### 1. Configure a cron job | ||
|
||
To configure a cron job, request the [`endowment:cronjob`](../reference/permissions.md#endowmentcronjob) | ||
permission, specifying one or more cron jobs in the `jobs` array. | ||
Define each job with a [cron expression](https://docs.oracle.com/cd/E12058_01/doc/doc.1014/e12030/cron_expressions.htm) | ||
and a request object, which MetaMask sends to the Snap's cron job handler when the job is executed. | ||
|
||
For example, to configure a job that executes every minute, add the following to your Snap's manifest file: | ||
|
||
```json title="snap.manifest.json" | ||
"initialPermissions": { | ||
"endowment:cronjob": { | ||
"jobs": [ | ||
{ | ||
"expression": "* * * * *", | ||
"request": { | ||
"method": "execute" | ||
} | ||
} | ||
] | ||
} | ||
} | ||
``` | ||
|
||
### 2. Implement a cron job handler | ||
|
||
Expose an [`onCronjob`](../reference/entry-points.md#oncronjob) entry point, which is triggered at | ||
the specified schedule with the requests defined in the `endowment:cronjob` permission. | ||
The following example handles the `execute` method specified in the previous example: | ||
|
||
```typescript title="index.ts" | ||
import type { OnCronjobHandler } from "@metamask/snaps-sdk"; | ||
|
||
export const onCronjob: OnCronjobHandler = async ({ request }) => { | ||
switch (request.method) { | ||
case "execute": | ||
// Cron jobs can execute any method that is available to the Snap. | ||
return snap.request({ | ||
method: "snap_dialog", | ||
params: { | ||
type: "alert", | ||
content: panel([ | ||
heading("Cron job"), | ||
text("This dialog was triggered by a cron job."), | ||
]), | ||
}, | ||
}); | ||
|
||
default: | ||
throw new Error("Method not found."); | ||
} | ||
}; | ||
``` | ||
|
||
:::tip Access data from cron jobs | ||
When accessing encrypted data from cron jobs using | ||
[`snap_manageState`](../reference/snaps-api.md#snap_managestate), MetaMask requires the user to | ||
enter their password if the wallet is locked. | ||
This interaction can be confusing to the user, since the Snap accesses the data in the background | ||
without the user being aware. | ||
|
||
If the cron job requires access to encrypted state, use | ||
[`snap_getClientStatus`](../reference/snaps-api.md#snap_getclientstatus) to ensure that MetaMask is | ||
unlocked before accessing state. | ||
This will prevent an unexpected password request, improving the user's experience. | ||
|
||
If the cron job does not require access to sensitive data, store that data in | ||
unencrypted state by setting `encrypted` to `false` when using | ||
[`snap_manageState`](../reference/snaps-api.md#snap_managestate). | ||
::: | ||
|
||
## Example | ||
|
||
See the [`@metamask/cronjob-example-snap`](https://github.com/MetaMask/snaps/tree/main/packages/examples/packages/cronjobs) | ||
package for a full example of implementing cron jobs. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters