|
| 1 | +import { getCustomStaticPath } from '@/utils/getCustomStaticPath'; |
| 2 | + |
| 3 | +export const meta = { |
| 4 | + title: 'Scheduling Functions', |
| 5 | + description: |
| 6 | + 'Learn how to schedule functions to run at specific intervals', |
| 7 | + platforms: [ |
| 8 | + 'android', |
| 9 | + 'angular', |
| 10 | + 'flutter', |
| 11 | + 'javascript', |
| 12 | + 'nextjs', |
| 13 | + 'react', |
| 14 | + 'react-native', |
| 15 | + 'swift', |
| 16 | + 'vue' |
| 17 | + ] |
| 18 | +}; |
| 19 | + |
| 20 | +export function getStaticPaths() { |
| 21 | + return getCustomStaticPath(meta.platforms); |
| 22 | +} |
| 23 | + |
| 24 | +export function getStaticProps() { |
| 25 | + return { |
| 26 | + props: { |
| 27 | + meta |
| 28 | + } |
| 29 | + }; |
| 30 | +} |
| 31 | + |
| 32 | +Amplify offers the ability to schedule Functions to run on specific intervals using natural language or [cron expressions](https://en.wikipedia.org/wiki/Cron). To get started, specify the `schedule` property in `defineFunction`: |
| 33 | + |
| 34 | +```ts title="amplify/jobs/weekly-digest/resource.ts" |
| 35 | +import { defineFunction } from "@aws-amplify/backend"; |
| 36 | + |
| 37 | +export const weeklyDigest = defineFunction({ |
| 38 | + name: "weekly-digest", |
| 39 | + schedule: "every week", |
| 40 | +}); |
| 41 | +``` |
| 42 | + |
| 43 | +Function schedules are powered by [Amazon EventBridge rules](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-rules.html), and can be leveraged to address use cases such as: |
| 44 | + |
| 45 | +- generating a "front page" of top-performing posts |
| 46 | +- generating a weekly digest of top-performing posts |
| 47 | +- generating a monthly report of warehouse inventory |
| 48 | + |
| 49 | +Their handlers can be typed using the `EventBridgeHandler` type: |
| 50 | + |
| 51 | +```ts title="amplify/jobs/weekly-digest/handler.ts" |
| 52 | +import type { EventBridgeHandler } from "aws-lambda"; |
| 53 | + |
| 54 | +export const handler: EventBridgeHandler = async (event) => { |
| 55 | + console.log("event", JSON.stringify(event, null, 2)) |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +<Callout info> |
| 60 | + |
| 61 | +**Note**: AWS Lambda types can be installed with |
| 62 | + |
| 63 | +```bash title="Terminal" showLineNumbers={false} |
| 64 | +npm install --save-dev @types/aws-lambda |
| 65 | +``` |
| 66 | + |
| 67 | +</Callout> |
| 68 | + |
| 69 | +Schedules can either be a single interval, or multiple intervals: |
| 70 | + |
| 71 | +```ts title="amplify/jobs/generate-report/resource.ts" |
| 72 | +import { defineFunction } from "@aws-amplify/backend"; |
| 73 | + |
| 74 | +export const generateReport = defineFunction({ |
| 75 | + name: "generate-report", |
| 76 | + schedule: ["every week", "every month", "every year"], |
| 77 | +}); |
| 78 | +``` |
| 79 | + |
| 80 | +Schedules can also be defined to execute using minutes or hours with a shorthand syntax: |
| 81 | + |
| 82 | +```ts title="amplify/jobs/drink-some-water/resource.ts" |
| 83 | +import { defineFunction } from "@aws-amplify/backend"; |
| 84 | + |
| 85 | +export const drinkSomeWater = defineFunction({ |
| 86 | + name: "drink-some-water", |
| 87 | + schedule: "every 1h" |
| 88 | +}) |
| 89 | +``` |
| 90 | + |
| 91 | +Or combined to create complex schedules: |
| 92 | + |
| 93 | +```ts title="amplify/jobs/remind-me/resource.ts" |
| 94 | +import { defineFunction } from "@aws-amplify/backend"; |
| 95 | + |
| 96 | +export const remindMe = defineFunction({ |
| 97 | + name: "remind-me", |
| 98 | + schedule: [ |
| 99 | + // every sunday at midnight |
| 100 | + "every week", |
| 101 | + // every tuesday at 5pm |
| 102 | + "0 17 * * 2", |
| 103 | + // every wednesday at 5pm |
| 104 | + "0 17 * * 3", |
| 105 | + // every thursday at 5pm |
| 106 | + "0 17 * * 4", |
| 107 | + // every friday at 5pm |
| 108 | + "0 17 * * 5", |
| 109 | + ] |
| 110 | +}) |
| 111 | +``` |
| 112 | + |
| 113 | +## Using natural language |
| 114 | + |
| 115 | +Schedules can be written using natural language, using terms you use every day. Amplify supports the following time periods: |
| 116 | + |
| 117 | +- `day` will always start at midnight |
| 118 | +- `week` will always start on Sunday at midnight |
| 119 | +- `month` will always start on the first of the month at midnight |
| 120 | +- `year` will always start on the first of the year at midnight |
| 121 | +- `m` for minutes |
| 122 | +- `h` for hours |
| 123 | + |
| 124 | +Natural language expressions are prefixed with "every": |
| 125 | + |
| 126 | +```ts title="amplify/jobs/drink-some-water/resource.ts" |
| 127 | +import { defineFunction } from "@aws-amplify/backend"; |
| 128 | + |
| 129 | +export const drinkSomeWater = defineFunction({ |
| 130 | + name: "drink-some-water", |
| 131 | + schedule: "every 1h" |
| 132 | +}) |
| 133 | +``` |
| 134 | + |
| 135 | +## Using cron expressions |
| 136 | + |
| 137 | +Schedules can be written using cron expressions. |
| 138 | + |
| 139 | +```ts title="amplify/jobs/remind-me/resource.ts" |
| 140 | +import { defineFunction } from "@aws-amplify/backend"; |
| 141 | + |
| 142 | +export const remindMe = defineFunction({ |
| 143 | + name: "remind-me-to-take-the-trash-out", |
| 144 | + schedule: [ |
| 145 | + // every tuesday at 9am |
| 146 | + "0 9 * * 2", |
| 147 | + // every friday at 9am |
| 148 | + "0 9 * * 5", |
| 149 | + ] |
| 150 | +}) |
| 151 | +``` |
0 commit comments