Skip to content

Commit e3068a9

Browse files
cicoyleyaron2
andauthored
[Jobs API] Describe Triggered Job Handling Assumptions (#4376)
* add specific logic for what assumptions are made for triggered jobs for http, grpc, sdks Signed-off-by: Cassandra Coyle <[email protected]> * rm space Signed-off-by: Cassandra Coyle <[email protected]> * add a note about this applying to all programming languages to avoid confusion Signed-off-by: Cassandra Coyle <[email protected]> * Update howto-schedule-and-handle-triggered-jobs.md Signed-off-by: Yaron Schneider <[email protected]> --------- Signed-off-by: Cassandra Coyle <[email protected]> Signed-off-by: Yaron Schneider <[email protected]> Co-authored-by: Yaron Schneider <[email protected]>
1 parent ae6d065 commit e3068a9

File tree

1 file changed

+70
-1
lines changed

1 file changed

+70
-1
lines changed

daprdocs/content/en/developing-applications/building-blocks/jobs/howto-schedule-and-handle-triggered-jobs.md

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,75 @@ In this example, at trigger time, which is `@every 1s` according to the `Schedul
9494

9595
At the trigger time, the `prodDBBackupHandler` function is called, executing the desired business logic for this job at trigger time. For example:
9696

97+
#### HTTP
98+
99+
When you create a job using Dapr's Jobs API, Dapr will automatically assume there is an endpoint available at
100+
`/job/<job-name>`. For instance, if you schedule a job named `test`, Dapr expects your application to listen for job
101+
events at `/job/test`. Ensure your application has a handler set up for this endpoint to process the job when it is
102+
triggered. For example:
103+
104+
*Note: The following example is in Go but applies to any programming language.*
105+
106+
```go
107+
108+
func main() {
109+
...
110+
http.HandleFunc("/job/", handleJob)
111+
http.HandleFunc("/job/<job-name>", specificJob)
112+
...
113+
}
114+
115+
func specificJob(w http.ResponseWriter, r *http.Request) {
116+
// Handle specific triggered job
117+
}
118+
119+
func handleJob(w http.ResponseWriter, r *http.Request) {
120+
// Handle the triggered jobs
121+
}
122+
```
123+
124+
#### gRPC
125+
126+
When a job reaches its scheduled trigger time, the triggered job is sent back to the application via the following
127+
callback function:
128+
129+
*Note: The following example is in Go but applies to any programming language with gRPC support.*
130+
131+
```go
132+
import rtv1 "github.com/dapr/dapr/pkg/proto/runtime/v1"
133+
...
134+
func (s *JobService) OnJobEventAlpha1(ctx context.Context, in *rtv1.JobEventRequest) (*rtv1.JobEventResponse, error) {
135+
// Handle the triggered job
136+
}
137+
```
138+
139+
This function processes the triggered jobs within the context of your gRPC server. When you set up the server, ensure that
140+
you register the callback server, which will invoke this function when a job is triggered:
141+
142+
```go
143+
...
144+
js := &JobService{}
145+
rtv1.RegisterAppCallbackAlphaServer(server, js)
146+
```
147+
148+
In this setup, you have full control over how triggered jobs are received and processed, as they are routed directly
149+
through this gRPC method.
150+
151+
#### SDKs
152+
153+
For SDK users, handling triggered jobs is simpler. When a job is triggered, Dapr will automatically route the job to the
154+
event handler you set up during the server initialization. For example, in Go, you'd register the event handler like this:
155+
156+
```go
157+
...
158+
if err = server.AddJobEventHandler("prod-db-backup", prodDBBackupHandler); err != nil {
159+
log.Fatalf("failed to register job event handler: %v", err)
160+
}
161+
```
162+
163+
Dapr takes care of the underlying routing. When the job is triggered, your `prodDBBackupHandler` function is called with
164+
the triggered job data. Here’s an example of handling the triggered job:
165+
97166
```go
98167
// ...
99168

@@ -144,4 +213,4 @@ dapr run --app-id=distributed-scheduler \
144213
## Next steps
145214

146215
- [Learn more about the Scheduler control plane service]({{< ref "concepts/dapr-services/scheduler.md" >}})
147-
- [Jobs API reference]({{< ref jobs_api.md >}})
216+
- [Jobs API reference]({{< ref jobs_api.md >}})

0 commit comments

Comments
 (0)