|
| 1 | +# Dapr Jobs API (SDK) |
| 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://docs.dapr.io/developing-applications/building-blocks/jobs/) link for more information about Dapr and the Jobs API. |
| 6 | + |
| 7 | +> **Note:** This example leverages the Dotnet SDK. If you are looking for the example using only HTTP requests, [click here](../http/). |
| 8 | +
|
| 9 | +This quickstart includes two apps: |
| 10 | + |
| 11 | +- Jobs Scheduler, responsible for scheduling, retrieving and deleting jobs. |
| 12 | +- Jobs Service, responsible for handling the triggered jobs. |
| 13 | + |
| 14 | +## Run all apps with multi-app run 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. Build the apps: |
| 19 | + |
| 20 | +<!-- STEP |
| 21 | +name: Build dependencies for job-service |
| 22 | +sleep: 1 |
| 23 | +--> |
| 24 | + |
| 25 | +```bash |
| 26 | +cd ./job-service |
| 27 | +dotnet build |
| 28 | +``` |
| 29 | + |
| 30 | +<!-- END_STEP --> |
| 31 | + |
| 32 | +<!-- STEP |
| 33 | +name: Build dependencies for job-scheduler |
| 34 | +sleep: 1 |
| 35 | +--> |
| 36 | + |
| 37 | +```bash |
| 38 | +cd ./job-scheduler |
| 39 | +dotnet build |
| 40 | +``` |
| 41 | + |
| 42 | +<!-- END_STEP --> |
| 43 | + |
| 44 | +2. Run the multi app run template: |
| 45 | + |
| 46 | +<!-- STEP |
| 47 | +name: Run multi app run template |
| 48 | +expected_stdout_lines: |
| 49 | + - '== APP - job-service-sdk == Job Scheduled: R2-D2' |
| 50 | + - '== APP - job-service-sdk == Job Scheduled: C-3PO' |
| 51 | + - '== APP - job-service-sdk == Starting droid: R2-D2' |
| 52 | + - '== APP - job-service-sdk == Executing maintenance job: Oil Change' |
| 53 | + - '== APP - job-service-sdk == Starting droid: C-3PO' |
| 54 | + - '== APP - job-service-sdk == Executing maintenance job: Limb Calibration' |
| 55 | +expected_stderr_lines: |
| 56 | +output_match_mode: substring |
| 57 | +match_order: none |
| 58 | +background: true |
| 59 | +sleep: 60 |
| 60 | +timeout_seconds: 120 |
| 61 | +--> |
| 62 | + |
| 63 | +```bash |
| 64 | +dapr run -f . |
| 65 | +``` |
| 66 | + |
| 67 | +The terminal console output should look similar to this, where: |
| 68 | + |
| 69 | +- The `R2-D2` job is being scheduled. |
| 70 | +- The `R2-D2` job is being retrieved. |
| 71 | +- The `C-3PO` job is being scheduled. |
| 72 | +- The `C-3PO` job is being retrieved. |
| 73 | +- The `R2-D2` job is being executed after 15 seconds. |
| 74 | +- The `C-3PO` job is being executed after 20 seconds. |
| 75 | + |
| 76 | +```text |
| 77 | +== APP - job-scheduler-sdk == Scheduling job... |
| 78 | +== APP - job-service-sdk == Job Scheduled: R2-D2 |
| 79 | +== APP - job-scheduler-sdk == Job scheduled: {"name":"R2-D2","job":"Oil Change","dueTime":15} |
| 80 | +== APP - job-scheduler-sdk == Getting job: R2-D2 |
| 81 | +== APP - job-service-sdk == Getting job... |
| 82 | +== APP - job-scheduler-sdk == Job details: {"schedule":"@every 15s","repeatCount":1,"dueTime":null,"ttl":null,"payload":"ChtkYXByLmlvL3NjaGVkdWxlL2pvYnBheWxvYWQSJXsiZHJvaWQiOiJSMi1EMiIsInRhc2siOiJPaWwgQ2hhbmdlIn0="} |
| 83 | +== APP - job-scheduler-sdk == Scheduling job... |
| 84 | +== APP - job-service-sdk == Job Scheduled: C-3PO |
| 85 | +== APP - job-scheduler-sdk == Job scheduled: {"name":"C-3PO","job":"Limb Calibration","dueTime":20} |
| 86 | +== APP - job-scheduler-sdk == Getting job: C-3PO |
| 87 | +== APP - job-service-sdk == Getting job... |
| 88 | +== APP - job-scheduler-sdk == Job details: {"schedule":"@every 20s","repeatCount":1,"dueTime":null,"ttl":null,"payload":"ChtkYXByLmlvL3NjaGVkdWxlL2pvYnBheWxvYWQSK3siZHJvaWQiOiJDLTNQTyIsInRhc2siOiJMaW1iIENhbGlicmF0aW9uIn0="} |
| 89 | +== APP - job-service-sdk == Handling job... |
| 90 | +== APP - job-service-sdk == Starting droid: R2-D2 |
| 91 | +== APP - job-service-sdk == Executing maintenance job: Oil Change |
| 92 | +``` |
| 93 | + |
| 94 | +After 20 seconds, the terminal output should present the `C-3PO` job being processed: |
| 95 | + |
| 96 | +```text |
| 97 | +== APP - job-service-sdk == Handling job... |
| 98 | +== APP - job-service-sdk == Starting droid: C-3PO |
| 99 | +== APP - job-service-sdk == Executing maintenance job: Limb Calibration |
| 100 | +``` |
| 101 | + |
| 102 | +<!-- END_STEP --> |
| 103 | + |
| 104 | +3. Stop and clean up application processes. |
| 105 | + |
| 106 | +<!-- STEP |
| 107 | +name: Stop multi-app run |
| 108 | +--> |
| 109 | + |
| 110 | +```bash |
| 111 | +dapr stop -f . |
| 112 | +``` |
| 113 | + |
| 114 | +<!-- END_STEP --> |
| 115 | + |
| 116 | +## Run apps individually |
| 117 | + |
| 118 | +### Schedule Jobs |
| 119 | + |
| 120 | +1. Open a terminal and run the `job-service` app. Build the dependencies if you haven't already. |
| 121 | + |
| 122 | +```bash |
| 123 | +cd ./job-service |
| 124 | +dotnet build |
| 125 | +``` |
| 126 | + |
| 127 | +```bash |
| 128 | +dapr run --app-id job-service-sdk --app-port 6200 --dapr-http-port 6280 -- dotnet run |
| 129 | +``` |
| 130 | + |
| 131 | +2. In a new terminal window, schedule the `R2-D2` Job using the Jobs API. |
| 132 | + |
| 133 | +```bash |
| 134 | +curl -X POST \ |
| 135 | + http://localhost:6200/scheduleJob \ |
| 136 | + -H "Content-Type: application/json" \ |
| 137 | + -d '{ |
| 138 | + "name": "R2-D2", |
| 139 | + "job": "Oil Change", |
| 140 | + "dueTime": 2 |
| 141 | + }' |
| 142 | +``` |
| 143 | + |
| 144 | +In the `job-service` terminal window, the output should be: |
| 145 | + |
| 146 | +```text |
| 147 | +== APP - job-app == Received job request... |
| 148 | +== APP - job-app == Starting droid: R2-D2 |
| 149 | +== APP - job-app == Executing maintenance job: Oil Change |
| 150 | +``` |
| 151 | + |
| 152 | +3. On the same terminal window, schedule the `C-3PO` Job using the Jobs API. |
| 153 | + |
| 154 | +```bash |
| 155 | +curl -X POST \ |
| 156 | + http://localhost:6200/scheduleJob \ |
| 157 | + -H "Content-Type: application/json" \ |
| 158 | + -d '{ |
| 159 | + "name": "C-3PO", |
| 160 | + "job": "Limb Calibration", |
| 161 | + "dueTime": 30 |
| 162 | + }' |
| 163 | +``` |
| 164 | + |
| 165 | +### Get a scheduled job |
| 166 | + |
| 167 | +1. On the same terminal window, run the command below to get the recently scheduled `C-3PO` job. |
| 168 | + |
| 169 | +```bash |
| 170 | +curl -X GET http://localhost:6200/getJob/C-3PO -H "Content-Type: application/json" |
| 171 | +``` |
| 172 | + |
| 173 | +You should see the following: |
| 174 | + |
| 175 | +```text |
| 176 | +{"name":"c-3po", "dueTime":"30s", "data":{"@type":"type.googleapis.com/google.protobuf.Value", "value":{"Value":"C-3PO:Limb Calibration"}}} |
| 177 | +``` |
| 178 | + |
| 179 | +### Delete a scheduled job |
| 180 | + |
| 181 | +1. On the same terminal window, run the command below to deleted the recently scheduled `C-3PO` job. |
| 182 | + |
| 183 | +```bash |
| 184 | +curl -X DELETE http://localhost:6200/deleteJob/C-3PO -H "Content-Type: application/json" |
| 185 | +``` |
| 186 | + |
| 187 | +2. Run the command below to attempt to retrieve the deleted job: |
| 188 | + |
| 189 | +```bash |
| 190 | +curl -X GET http://localhost:6200/getJob/C-3PO -H "Content-Type: application/json" |
| 191 | +``` |
| 192 | + |
| 193 | +In the `job-service` terminal window, the output should be similar to the following: |
| 194 | + |
| 195 | +```text |
| 196 | +ERRO[0157] Error getting job C-3PO due to: rpc error: code = NotFound desc = job not found: C-3PO instance=local scope=dapr.api type=log ver=1.15.0 |
| 197 | +``` |
0 commit comments