|
| 1 | +# Dapr Jobs |
| 2 | + |
| 3 | +In this quickstart, you'll schedule, get, and delete a job using Dapr's Job API. This API is responsible for scheduling and running jobs at a specific time or interval. |
| 4 | + |
| 5 | +Visit [this](https://v1-14.docs.dapr.io/developing-applications/building-blocks/jobs/) link for more information about Dapr and the Jobs API. |
| 6 | + |
| 7 | +> **Note:** This example leverages HTTP `requests` only. If you are looking for the example using the Dapr Client SDK (recommended) [click here](../sdk/). |
| 8 | +
|
| 9 | +This quickstart includes two apps: |
| 10 | + |
| 11 | +- `job-scheduler`, responsible for scheduling, retrieving and deleting jobs. |
| 12 | +- `job-service`, responsible for handling the triggered jobs. |
| 13 | + |
| 14 | +## Run the app with the template file |
| 15 | + |
| 16 | +This section shows how to run both applications at once using [multi-app run template files](https://docs.dapr.io/developing-applications/local-development/multi-app-dapr-run/multi-app-overview/) with `dapr run -f .`. This enables to you test the interactions between multiple applications and will `schedule`, `run`, `get`, and `delete` jobs within a single process. |
| 17 | + |
| 18 | +1. Install dependencies: |
| 19 | + |
| 20 | +<!-- STEP |
| 21 | +name: Install Node dependencies |
| 22 | +--> |
| 23 | + |
| 24 | +```bash |
| 25 | +cd ./job-service |
| 26 | +npm install |
| 27 | +cd .. |
| 28 | +cd ./job-scheduler |
| 29 | +npm install |
| 30 | +``` |
| 31 | + |
| 32 | +<!-- END_STEP --> |
| 33 | + |
| 34 | +2. Open a new terminal window and run the multi app run template: |
| 35 | + |
| 36 | +<!-- STEP |
| 37 | +name: Run multi app run template |
| 38 | +expected_stdout_lines: |
| 39 | + - '== APP - job-scheduler == Job Scheduled: R2-D2' |
| 40 | + - '== APP - job-scheduler == Job Scheduled: C-3PO' |
| 41 | + - '== APP - job-service == Received job request...' |
| 42 | + - '== APP - job-service == Starting droid: R2-D2' |
| 43 | + - '== APP - job-service == Executing maintenance job: Oil Change' |
| 44 | + - '== APP - job-service == Received job request...' |
| 45 | + - '== APP - job-service == Starting droid: C-3PO' |
| 46 | + - '== APP - job-service == Executing maintenance job: Limb Calibration' |
| 47 | +expected_stderr_lines: |
| 48 | +output_match_mode: substring |
| 49 | +match_order: none |
| 50 | +background: false |
| 51 | +sleep: 60 |
| 52 | +timeout_seconds: 120 |
| 53 | +--> |
| 54 | + |
| 55 | +```bash |
| 56 | +dapr run -f . |
| 57 | +``` |
| 58 | + |
| 59 | +The terminal console output should look similar to this, where: |
| 60 | + |
| 61 | +- The `R2-D2` job is being scheduled. |
| 62 | +- The `R2-D2` job is being retrieved. |
| 63 | +- The `C-3PO` job is being scheduled. |
| 64 | +- The `C-3PO` job is being retrieved. |
| 65 | +- The `R2-D2` job is being executed after 15 seconds. |
| 66 | +- The `C-3PO` job is being executed after 20 seconds. |
| 67 | + |
| 68 | +```text |
| 69 | +== APP - job-scheduler == Job Scheduled: R2-D2 |
| 70 | +== APP - job-scheduler == Job details: {"name":"R2-D2", "dueTime":"15s", "data":{"value":{"value":"R2-D2:Oil Change"}}} |
| 71 | +== APP - job-scheduler == Job Scheduled: C-3PO |
| 72 | +== APP - job-scheduler == Job details: {"name":"C-3PO", "dueTime":"20s", "data":{"value":{"value":"C-3PO:Limb Calibration"}}} |
| 73 | +== APP - job-service == Received job request... |
| 74 | +== APP - job-service == Starting droid: R2-D2 |
| 75 | +== APP - job-service == Executing maintenance job: Oil Change |
| 76 | +``` |
| 77 | + |
| 78 | +After 20 seconds, the terminal output should present the `C-3PO` job being processed: |
| 79 | + |
| 80 | +```text |
| 81 | +== APP - job-service == Received job request... |
| 82 | +== APP - job-service == Starting droid: C-3PO |
| 83 | +== APP - job-service == Executing maintenance job: Limb Calibration |
| 84 | +``` |
| 85 | + |
| 86 | +<!-- END_STEP --> |
| 87 | + |
| 88 | +## Run the Jobs APIs individually |
| 89 | + |
| 90 | +### Schedule Jobs |
| 91 | + |
| 92 | +1. Open a terminal and run the `job-service` app: |
| 93 | + |
| 94 | +```bash |
| 95 | +dapr run --app-id job-service --app-port 6200 --dapr-http-port 6280 -- go run . |
| 96 | +``` |
| 97 | + |
| 98 | +2. On a new terminal window, schedule the `R2-D2` Job using the Jobs API. |
| 99 | + |
| 100 | +```bash |
| 101 | +curl -X POST \ |
| 102 | + http://localhost:6280/v1.0-alpha1/jobs/R2D2 \ |
| 103 | + -H "Content-Type: application/json" \ |
| 104 | + -d '{ |
| 105 | + "data": { |
| 106 | + "value": "R2-D2:Oil Change" |
| 107 | + }, |
| 108 | + "dueTime": "2s" |
| 109 | + }' |
| 110 | +``` |
| 111 | + |
| 112 | +Back at the `job-service` app terminal window, the output should be: |
| 113 | + |
| 114 | +```text |
| 115 | +== APP - job-app == Received job request... |
| 116 | +== APP - job-app == Starting droid: R2-D2 |
| 117 | +== APP - job-app == Executing maintenance job: Oil Change |
| 118 | +``` |
| 119 | + |
| 120 | +3. On the same terminal window, schedule the `C-3PO` Job using the Jobs API. |
| 121 | + |
| 122 | +```bash |
| 123 | +curl -X POST \ |
| 124 | + http://localhost:6280/v1.0-alpha1/jobs/c-3po \ |
| 125 | + -H "Content-Type: application/json" \ |
| 126 | + -d '{ |
| 127 | + "data": { |
| 128 | + "value": "C-3PO:Limb Calibration" |
| 129 | + }, |
| 130 | + "dueTime": "30s" |
| 131 | + }' |
| 132 | +``` |
| 133 | + |
| 134 | +### Get a scheduled job |
| 135 | + |
| 136 | +1. On the same terminal window, run the command below to get the recently scheduled `C-3PO` job. |
| 137 | + |
| 138 | +```bash |
| 139 | +curl -X GET http://localhost:6280/v1.0-alpha1/jobs/c-3po -H "Content-Type: application/json" |
| 140 | +``` |
| 141 | + |
| 142 | +You should see the following: |
| 143 | + |
| 144 | +```text |
| 145 | +{"name":"C-3PO", "dueTime":"30s", "data":{"@type":"type.googleapis.com/google.protobuf.StringValue", "expression":"C-3PO:Limb Calibration"}} |
| 146 | +``` |
| 147 | + |
| 148 | +### Delete a scheduled job |
| 149 | + |
| 150 | +1. On the same terminal window, run the command below to deleted the recently scheduled `C-3PO` job. |
| 151 | + |
| 152 | +```bash |
| 153 | +curl -X DELETE http://localhost:6280/v1.0-alpha1/jobs/c-3po -H "Content-Type: application/json" |
| 154 | +``` |
| 155 | + |
| 156 | +2. Run the command below to attempt to retrieve the deleted job: |
| 157 | + |
| 158 | +```bash |
| 159 | +curl -X GET http://localhost:6280/v1.0-alpha1/jobs/c-3po -H "Content-Type: application/json" |
| 160 | +``` |
| 161 | + |
| 162 | +Back at the `job-service` app terminal window, the output should be: |
| 163 | + |
| 164 | +```text |
| 165 | +ERRO[0249] Error getting job c-3po due to: rpc error: code = Unknown desc = job not found: app||default||job-service||c-3po instance=diagrid.local scope=dapr.api type=log ver=1.14.0-rc.2 |
| 166 | +``` |
0 commit comments