|
| 1 | +You can create and update your Monitors programmatically with code rather than [creating and configuring them in Sentry.io](https://sentry.io/crons/create/). |
| 2 | + |
| 3 | +To create/update a monitor, use `Sentry.withMonitor()` and pass in your monitor configuration as a third parameter: |
| 4 | + |
| 5 | +```javascript |
| 6 | +const monitorConfig = { |
| 7 | + schedule: { |
| 8 | + type: "crontab", |
| 9 | + value: "* * * * *", |
| 10 | + }, |
| 11 | + checkinMargin: 2, // In minutes. Optional. |
| 12 | + maxRuntime: 10, // In minutes. Optional. |
| 13 | + timezone: "America/Los_Angeles", // Optional. |
| 14 | +}; |
| 15 | + |
| 16 | +Sentry.withMonitor( |
| 17 | + "<monitor-slug>", |
| 18 | + () => { |
| 19 | + // Execute your scheduled task here... |
| 20 | + }, |
| 21 | + monitorConfig |
| 22 | +); |
| 23 | +``` |
| 24 | + |
| 25 | +```typescript |
| 26 | +import { MonitorConfig } from "@sentry/types"; |
| 27 | + |
| 28 | +const monitorConfig: MonitorConfig = { |
| 29 | + schedule: { |
| 30 | + type: "crontab", |
| 31 | + value: "* * * * *", |
| 32 | + }, |
| 33 | + checkinMargin: 2, // In minutes. Optional. |
| 34 | + maxRuntime: 10, // In minutes. Optional. |
| 35 | + timezone: "America/Los_Angeles", // Optional. |
| 36 | +}; |
| 37 | + |
| 38 | +Sentry.withMonitor( |
| 39 | + "<monitor-slug>", |
| 40 | + () => { |
| 41 | + // Execute your scheduled task here... |
| 42 | + }, |
| 43 | + monitorConfig |
| 44 | +); |
| 45 | +``` |
| 46 | + |
| 47 | +To configure the monitor's check-ins, use `Sentry.captureCheckIn()` and pass in your monitor configuration as a second parameter: |
| 48 | + |
| 49 | +```javascript |
| 50 | +const monitorConfig = { |
| 51 | + schedule: { |
| 52 | + type: "crontab", |
| 53 | + value: "* * * * *", |
| 54 | + }, |
| 55 | + checkinMargin: 2, // In minutes. Optional. |
| 56 | + maxRuntime: 10, // In minutes. Optional. |
| 57 | + timezone: "America/Los_Angeles", // Optional. |
| 58 | +}; |
| 59 | + |
| 60 | +// 🟡 Notify Sentry your job is running: |
| 61 | +const checkInId = Sentry.captureCheckIn( |
| 62 | + { |
| 63 | + monitorSlug: "<monitor-slug>", |
| 64 | + status: "in_progress", |
| 65 | + }, |
| 66 | + monitorConfig |
| 67 | +); |
| 68 | + |
| 69 | +// Execute your scheduled task here... |
| 70 | + |
| 71 | +// 🟢 Notify Sentry your job has completed successfully: |
| 72 | +Sentry.captureCheckIn( |
| 73 | + { |
| 74 | + // Make sure this variable is named `checkInId` |
| 75 | + checkInId, |
| 76 | + monitorSlug: "<monitor-slug>", |
| 77 | + status: "ok", |
| 78 | + }, |
| 79 | + monitorConfig |
| 80 | +); |
| 81 | +``` |
| 82 | + |
| 83 | +```typescript |
| 84 | +import { MonitorConfig } from "@sentry/types"; |
| 85 | + |
| 86 | +const monitorConfig: MonitorConfig = { |
| 87 | + schedule: { |
| 88 | + type: "crontab", |
| 89 | + value: "* * * * *", |
| 90 | + }, |
| 91 | + checkinMargin: 2, // In minutes. Optional. |
| 92 | + maxRuntime: 10, // In minutes. Optional. |
| 93 | + timezone: "America/Los_Angeles", // Optional. |
| 94 | +}; |
| 95 | + |
| 96 | +// 🟡 Notify Sentry your job is running: |
| 97 | +const checkInId = Sentry.captureCheckIn( |
| 98 | + { |
| 99 | + monitorSlug: "<monitor-slug>", |
| 100 | + status: "in_progress", |
| 101 | + }, |
| 102 | + monitorConfig |
| 103 | +); |
| 104 | + |
| 105 | +// Execute your scheduled task here... |
| 106 | + |
| 107 | +// 🟢 Notify Sentry your job has completed successfully: |
| 108 | +Sentry.captureCheckIn( |
| 109 | + { |
| 110 | + // Make sure this variable is named `checkInId` |
| 111 | + checkInId, |
| 112 | + monitorSlug: "<monitor-slug>", |
| 113 | + status: "ok", |
| 114 | + }, |
| 115 | + monitorConfig |
| 116 | +); |
| 117 | +``` |
| 118 | + |
| 119 | +### Monitor Configuration Properties |
| 120 | + |
| 121 | +The following are available monitor configuration properties: |
| 122 | + |
| 123 | +`schedule`: |
| 124 | + |
| 125 | +: The job's schedule: |
| 126 | + |
| 127 | +The schedule representation for your monitor, either `crontab` or `interval`. The structure will vary depending on the type: |
| 128 | + |
| 129 | +```json |
| 130 | +{"type": "crontab", "value": "0 * * * *"} |
| 131 | +{"type": "interval", "value": "2", "unit": "hour"} |
| 132 | +``` |
| 133 | + |
| 134 | +`checkinMargin`: |
| 135 | + |
| 136 | +: The amount of time (in minutes) Sentry should wait for your check-in before it's considered missed ("grace period"). Optional. |
| 137 | + |
| 138 | +<Note> |
| 139 | + |
| 140 | +We recommend that your check-in margin be less than or equal to your interval. |
| 141 | + |
| 142 | +</Note> |
| 143 | + |
| 144 | +`maxRuntime`: |
| 145 | + |
| 146 | +: The amount of time (in minutes) your job is allowed to run before it's considered failed. Optional. |
| 147 | + |
| 148 | +`timezone`: |
| 149 | + |
| 150 | +: The `tz` where your job is running. This is usually your server's timezone, (such as `America/Los_Angeles`). See [list of tz database time zones](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones). Optional. |
0 commit comments