|
| 1 | +# Jobs |
| 2 | + |
| 3 | +## Introduction |
| 4 | + |
| 5 | +The `jobs` section allows defining scheduled or event-driven tasks |
| 6 | +that execute commands or make HTTP requests. Jobs can be useful for |
| 7 | +running periodic maintenance tasks, executing background processes, or |
| 8 | +ensuring essential workflows are automated. |
| 9 | + |
| 10 | + |
| 11 | +## Job Structure |
| 12 | + |
| 13 | +Each job consists of the following fields: |
| 14 | + |
| 15 | +- **name** *(str, required)* – A unique name for the job. |
| 16 | +- **trigger** *(str, required)* – Defines when the job should run. |
| 17 | + - This can be a cron expression (e.g., `'*/15 * * * *'` for every 15 |
| 18 | + minutes). |
| 19 | + - It can also be `'post-deployment'` to run after deployment. |
| 20 | + - `'pre-deployment'` is currently not supported. |
| 21 | +- **action** *(required)* – Specifies the task to run. More details |
| 22 | + in the [next section](#job-actions). A job can: |
| 23 | + - Run a command using `execute`. |
| 24 | + - Make an HTTP request using `fetch`. |
| 25 | +- **retries** *(int, Optional)* – The maximum number of times the job |
| 26 | + should retry upon failure. |
| 27 | +- **timeout** *(duration, Optional)* – The maximum time a job is |
| 28 | + allowed to run before timing out (e.g., `'10m'` for 10 minutes). |
| 29 | +- **max_schedule_drift** *(duration, Optional)* – The maximum |
| 30 | + allowed delay before the job is considered late. (e.g., `'10m'` for |
| 31 | + 10 minutes) |
| 32 | + |
| 33 | + |
| 34 | +Fully working examples configuration can be found |
| 35 | +[here](#example-jobs-configuration). |
| 36 | + |
| 37 | + |
| 38 | +## Job Actions |
| 39 | + |
| 40 | +This section defines the `action` field in a job definition. There are |
| 41 | +two types of actions for a job: |
| 42 | + |
| 43 | +1. **Executing a command (`execute`)** |
| 44 | + |
| 45 | + Runs a CLI command with Optional environment variables. |
| 46 | + ```yaml |
| 47 | + action: |
| 48 | + execute: |
| 49 | + command: install-wp |
| 50 | + cli_args: |
| 51 | + - "--version" |
| 52 | + - "--help" |
| 53 | + env: |
| 54 | + LOG_LEVEL: DEBUG |
| 55 | + EXTRA_FLAGS: "--enable-feature-1 --enable-feature-2" |
| 56 | + ``` |
| 57 | + - `command` *(str, Optional)* – The command to execute. |
| 58 | + - `cli_args` *(List[str], Optional)* – Arguments for the command. |
| 59 | + - `env` *(map, Optional)* – Environment variables. |
| 60 | + |
| 61 | +2. **Making an HTTP request (`fetch`)** |
| 62 | + |
| 63 | + Sends an HTTP request to a specific endpoint. |
| 64 | + ```yaml |
| 65 | + action: |
| 66 | + fetch: |
| 67 | + path: /wp-cron.php |
| 68 | + timeout: '10m' |
| 69 | + headers: |
| 70 | + - name: Accept |
| 71 | + value: application/json |
| 72 | + - name: User-Agent |
| 73 | + value: Wasmer-CronJob |
| 74 | +
|
| 75 | + ``` |
| 76 | + - `path` *(str, required)* – The request URL path. |
| 77 | + - `method` *(str, Optional)* – HTTP method (defaults to `GET`). |
| 78 | + - `headers` *(list[object[name, value]], Optional)* – HTTP headers as |
| 79 | + yaml objects. The objects have two fields, `name` which is the |
| 80 | + header name, and `value` which is the value of header field. |
| 81 | + - `body` *(str, Optional)* – Request body. |
| 82 | + - `timeout` *(duration, Optional)* – Maximum time to wait for |
| 83 | + a response. |
| 84 | + |
| 85 | +## Example Jobs Configuration |
| 86 | + |
| 87 | +Below are examples of various job types based on their trigger and |
| 88 | +action. |
| 89 | + |
| 90 | + |
| 91 | +### Fetch job |
| 92 | + |
| 93 | +This job fetches the `/` path on it's app every 15 minutes. |
| 94 | +```yaml |
| 95 | +kind: wasmer.io/App.v0 |
| 96 | +package: wasmer/hello |
| 97 | +jobs: |
| 98 | + - name: ping-server-every-15-minutes |
| 99 | + trigger: '*/15 * * * *' |
| 100 | + action: |
| 101 | + fetch: |
| 102 | + path: / |
| 103 | + timeout: 30s |
| 104 | +``` |
| 105 | + |
| 106 | + |
| 107 | +### Execute job |
| 108 | + |
| 109 | +This job runs `php --version` every minute. |
| 110 | + |
| 111 | +```yaml |
| 112 | +kind: wasmer.io/App.v0 |
| 113 | +package: wasmer/wordpress |
| 114 | +jobs: |
| 115 | + - name: check-php-version |
| 116 | + trigger: '*/1 * * * *' |
| 117 | + execute: |
| 118 | + command: php |
| 119 | + cli_args: ["--version"] |
| 120 | +``` |
| 121 | + |
| 122 | +### Post deployment job |
| 123 | + |
| 124 | +This job fetches the `/warm-up-caches` path on it's app after |
| 125 | +each deployment. |
| 126 | + |
| 127 | +```yaml |
| 128 | +kind: wasmer.io/App.v0 |
| 129 | +package: wasmer/hello |
| 130 | +jobs: |
| 131 | + - name: ping-server-every-15-minutes |
| 132 | + trigger: post-deployment |
| 133 | + action: |
| 134 | + fetch: |
| 135 | + path: /warm-up-caches |
| 136 | + timeout: 30s |
| 137 | +``` |
| 138 | + |
| 139 | + |
| 140 | +## Use Cases |
| 141 | + |
| 142 | +- **Post-deployment setup:** The `installation` job ensures necessary |
| 143 | + setup commands run after deployment. |
| 144 | +- **Scheduled background tasks:** The `wp-cron` job triggers |
| 145 | + WordPress’s cron system every 15 minutes. |
0 commit comments