From 7d2cacdb6a9088395ff38b73ae7f9563b69a4820 Mon Sep 17 00:00:00 2001 From: Cassandra Coyle Date: Thu, 10 Oct 2024 09:47:41 -0400 Subject: [PATCH 1/4] add specific logic for what assumptions are made for triggered jobs for http, grpc, sdks Signed-off-by: Cassandra Coyle --- ...owto-schedule-and-handle-triggered-jobs.md | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/daprdocs/content/en/developing-applications/building-blocks/jobs/howto-schedule-and-handle-triggered-jobs.md b/daprdocs/content/en/developing-applications/building-blocks/jobs/howto-schedule-and-handle-triggered-jobs.md index 622e019b10a..0606900f28f 100644 --- a/daprdocs/content/en/developing-applications/building-blocks/jobs/howto-schedule-and-handle-triggered-jobs.md +++ b/daprdocs/content/en/developing-applications/building-blocks/jobs/howto-schedule-and-handle-triggered-jobs.md @@ -94,6 +94,75 @@ In this example, at trigger time, which is `@every 1s` according to the `Schedul At the trigger time, the `prodDBBackupHandler` function is called, executing the desired business logic for this job at trigger time. For example: +### Triggered Job Handling Assumptions + +#### HTTP + +When you create a job using Dapr's Jobs API, Dapr will automatically assume there is an endpoint available at +`/job/`. For instance, if you schedule a job named `test`, Dapr expects your application to listen for job +events at `/job/test`. Ensure your application has a handler set up for this endpoint to process the job when it is +triggered. For example: + +```go + +func main() { + ... + http.HandleFunc("/job/", handleJob) + http.HandleFunc("/job/", specificJob) + ... +} + +func specificJob(w http.ResponseWriter, r *http.Request) { + // Handle specific triggered job +} + +func handleJob(w http.ResponseWriter, r *http.Request) { + // Handle the triggered jobs +} + + +``` + +#### gRPC + +When a job reaches its scheduled trigger time, the triggered job is sent back to the application via the following +callback function: + +```go +import rtv1 "github.com/dapr/dapr/pkg/proto/runtime/v1" +... +func (s *JobService) OnJobEventAlpha1(ctx context.Context, in *rtv1.JobEventRequest) (*rtv1.JobEventResponse, error) { + // Handle the triggered job +} +``` + +This function processes the triggered jobs within the context of your gRPC server. When you set up the server, ensure that +you register the callback server, which will invoke this function when a job is triggered: + +```go +... +js := &JobService{} +rtv1.RegisterAppCallbackAlphaServer(server, js) +``` + +In this setup, you have full control over how triggered jobs are received and processed, as they are routed directly +through this gRPC method. + +#### SDKs + +For SDK users, handling triggered jobs is simpler. When a job is triggered, Dapr will automatically route the job to the +event handler you set up during the server initialization. For example, in Go, you'd register the event handler like this: + +```go +... +if err = server.AddJobEventHandler("prod-db-backup", prodDBBackupHandler); err != nil { + log.Fatalf("failed to register job event handler: %v", err) +} +``` + +Dapr takes care of the underlying routing. When the job is triggered, your `prodDBBackupHandler` function is called with +the triggered job data. Here’s an example of handling the triggered job: + ```go // ... From 9e1fb5c239d8acd13e724fa0bbef9f5467c70483 Mon Sep 17 00:00:00 2001 From: Cassandra Coyle Date: Thu, 10 Oct 2024 09:49:31 -0400 Subject: [PATCH 2/4] rm space Signed-off-by: Cassandra Coyle --- .../jobs/howto-schedule-and-handle-triggered-jobs.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/daprdocs/content/en/developing-applications/building-blocks/jobs/howto-schedule-and-handle-triggered-jobs.md b/daprdocs/content/en/developing-applications/building-blocks/jobs/howto-schedule-and-handle-triggered-jobs.md index 0606900f28f..2e5681d5c0f 100644 --- a/daprdocs/content/en/developing-applications/building-blocks/jobs/howto-schedule-and-handle-triggered-jobs.md +++ b/daprdocs/content/en/developing-applications/building-blocks/jobs/howto-schedule-and-handle-triggered-jobs.md @@ -119,8 +119,6 @@ func specificJob(w http.ResponseWriter, r *http.Request) { func handleJob(w http.ResponseWriter, r *http.Request) { // Handle the triggered jobs } - - ``` #### gRPC From 3e804b3ff6226d005dbc4a86c80d8893f3148901 Mon Sep 17 00:00:00 2001 From: Cassandra Coyle Date: Thu, 10 Oct 2024 12:32:09 -0400 Subject: [PATCH 3/4] add a note about this applying to all programming languages to avoid confusion Signed-off-by: Cassandra Coyle --- .../jobs/howto-schedule-and-handle-triggered-jobs.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/daprdocs/content/en/developing-applications/building-blocks/jobs/howto-schedule-and-handle-triggered-jobs.md b/daprdocs/content/en/developing-applications/building-blocks/jobs/howto-schedule-and-handle-triggered-jobs.md index 2e5681d5c0f..ed4a30e0c13 100644 --- a/daprdocs/content/en/developing-applications/building-blocks/jobs/howto-schedule-and-handle-triggered-jobs.md +++ b/daprdocs/content/en/developing-applications/building-blocks/jobs/howto-schedule-and-handle-triggered-jobs.md @@ -96,6 +96,10 @@ At the trigger time, the `prodDBBackupHandler` function is called, executing the ### Triggered Job Handling Assumptions +Note: The following information applies to **all programming languages**, not just the Go examples provided in this +documentation. As of Dapr release v1.14, the Jobs API is only supported in the Go SDK, but support for additional SDKs +and languages will be added in future releases. + #### HTTP When you create a job using Dapr's Jobs API, Dapr will automatically assume there is an endpoint available at From 4141075e741e70b0d9e43fcbc9e8287a4989ef02 Mon Sep 17 00:00:00 2001 From: Yaron Schneider Date: Thu, 10 Oct 2024 14:55:25 -0700 Subject: [PATCH 4/4] Update howto-schedule-and-handle-triggered-jobs.md Signed-off-by: Yaron Schneider --- .../jobs/howto-schedule-and-handle-triggered-jobs.md | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/daprdocs/content/en/developing-applications/building-blocks/jobs/howto-schedule-and-handle-triggered-jobs.md b/daprdocs/content/en/developing-applications/building-blocks/jobs/howto-schedule-and-handle-triggered-jobs.md index ed4a30e0c13..0a5dba3b10c 100644 --- a/daprdocs/content/en/developing-applications/building-blocks/jobs/howto-schedule-and-handle-triggered-jobs.md +++ b/daprdocs/content/en/developing-applications/building-blocks/jobs/howto-schedule-and-handle-triggered-jobs.md @@ -94,12 +94,6 @@ In this example, at trigger time, which is `@every 1s` according to the `Schedul At the trigger time, the `prodDBBackupHandler` function is called, executing the desired business logic for this job at trigger time. For example: -### Triggered Job Handling Assumptions - -Note: The following information applies to **all programming languages**, not just the Go examples provided in this -documentation. As of Dapr release v1.14, the Jobs API is only supported in the Go SDK, but support for additional SDKs -and languages will be added in future releases. - #### HTTP When you create a job using Dapr's Jobs API, Dapr will automatically assume there is an endpoint available at @@ -107,6 +101,8 @@ When you create a job using Dapr's Jobs API, Dapr will automatically assume ther events at `/job/test`. Ensure your application has a handler set up for this endpoint to process the job when it is triggered. For example: +*Note: The following example is in Go but applies to any programming language.* + ```go func main() { @@ -130,6 +126,8 @@ func handleJob(w http.ResponseWriter, r *http.Request) { When a job reaches its scheduled trigger time, the triggered job is sent back to the application via the following callback function: +*Note: The following example is in Go but applies to any programming language with gRPC support.* + ```go import rtv1 "github.com/dapr/dapr/pkg/proto/runtime/v1" ... @@ -215,4 +213,4 @@ dapr run --app-id=distributed-scheduler \ ## Next steps - [Learn more about the Scheduler control plane service]({{< ref "concepts/dapr-services/scheduler.md" >}}) -- [Jobs API reference]({{< ref jobs_api.md >}}) \ No newline at end of file +- [Jobs API reference]({{< ref jobs_api.md >}})