-
Notifications
You must be signed in to change notification settings - Fork 734
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Jobs API docs, pt 3 #4243
Merged
Merged
Jobs API docs, pt 3 #4243
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
4b172d5
add screenshots with scheduler service
hhunter-ms 2dad961
Merge branch 'v1.14' into issue_3915-3
hhunter-ms dbaf646
update image to show new link
hhunter-ms 64fb51f
Merge branch 'issue_3915-3' of https://github.com/hhunter-ms/docs int…
hhunter-ms 3a7e34a
Merge branch 'v1.14' into issue_3915-3
hhunter-ms e51f0c2
add to how-to
hhunter-ms 51ebfe4
Merge branch 'v1.14' into issue_3915-3
hhunter-ms 19c7ca9
tweak howto-schedule-jobs && add howto-handle-triggered-jobs
cicoyle 38cdb1a
review cassies additions
hhunter-ms c8fd729
rm separate howto and add the handling of triggered jobs to the initi…
cicoyle 6b8dcaf
rm unneccessary howto
cicoyle c7f303f
pull in cassie change and edit
hhunter-ms 70264c5
delete file
hhunter-ms 33f80b6
add code tabs for dapr run
hhunter-ms File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
156 changes: 156 additions & 0 deletions
156
...g-applications/building-blocks/jobs/howto-schedule-and-handle-triggered-jobs.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
--- | ||
type: docs | ||
title: "How-To: Schedule and handle triggered jobs" | ||
linkTitle: "How-To: Schedule and handle triggered jobs" | ||
weight: 2000 | ||
description: "Learn how to use the jobs API to schedule and handle triggered jobs" | ||
--- | ||
|
||
Now that you've learned what the [jobs building block]({{< ref jobs-overview.md >}}) provides, let's look at an example of how to use the API. The code example below describes an application that schedules jobs for a database backup application and handles them at trigger time, also known as the time the job was sent back to the application because it reached it's dueTime. | ||
|
||
<!-- | ||
Include a diagram or image, if possible. | ||
--> | ||
|
||
## Start the Scheduler service | ||
|
||
When you [run `dapr init` in either self-hosted mode or on Kubernetes]({{< ref install-dapr-selfhost.md >}}), the Dapr Scheduler service is started. | ||
|
||
## Set up the Jobs API | ||
|
||
In your code, set up and schedule jobs within your application. | ||
|
||
{{< tabs "Go" >}} | ||
|
||
{{% codetab %}} | ||
|
||
<!--go--> | ||
|
||
The following Go SDK code sample schedules the job named `prod-db-backup`. Job data is housed in a backup database (`"my-prod-db"`) and is scheduled with `ScheduleJobAlpha1`, | ||
providing the job `data` which is the backup `task` name and `metadata` including the location and database name. The job is scheduled with a `schedule` set and the amount of `repeats` | ||
desired, meaning there is a max amount of times the job should be triggered and sent back to the app. At trigger time, so `@every 1s` according to the `schedule`, this job will be | ||
triggered and sent back to the application up to the max repeats set which is `10`. At the trigger time, the `prodDBBackupHandler` function is called executing the | ||
desired business logic for this job at trigger time. For example: | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
//... | ||
|
||
daprc "github.com/dapr/go-sdk/client" | ||
"github.com/dapr/go-sdk/examples/dist-scheduler/api" | ||
"github.com/dapr/go-sdk/service/common" | ||
daprs "github.com/dapr/go-sdk/service/grpc" | ||
) | ||
|
||
func main() { | ||
// Initialize the server | ||
server, err := daprs.NewService(":50070") | ||
// ... | ||
|
||
if err = server.AddJobEventHandler("prod-db-backup", prodDBBackupHandler); err != nil { | ||
log.Fatalf("failed to register job event handler: %v", err) | ||
} | ||
|
||
log.Println("starting server") | ||
go func() { | ||
if err = server.Start(); err != nil { | ||
log.Fatalf("failed to start server: %v", err) | ||
} | ||
}() | ||
// ... | ||
|
||
// Set up backup location | ||
jobData, err := json.Marshal(&api.DBBackup{ | ||
Task: "db-backup", | ||
Metadata: api.Metadata{ | ||
DBName: "my-prod-db", | ||
BackupLocation: "/backup-dir", | ||
}, | ||
}, | ||
) | ||
// ... | ||
|
||
// Set up the job | ||
job := daprc.Job{ | ||
Name: "prod-db-backup", | ||
Schedule: "@every 1s", | ||
Repeats: 10, | ||
Data: &anypb.Any{ | ||
Value: jobData, | ||
}, | ||
} | ||
|
||
// Create the client | ||
client, err := daprc.NewClient() | ||
// ... | ||
|
||
defer client.Close() | ||
|
||
// Schedule job | ||
err = client.ScheduleJobAlpha1(ctx, &job) | ||
// ... | ||
fmt.Println("schedule job - success") | ||
|
||
// Get job | ||
resp, err := client.GetJobAlpha1(ctx, "prod-db-backup") | ||
// ... | ||
fmt.Printf("get job - resp: %v\n", resp) // parse | ||
|
||
// Let test run and then cleanup the job | ||
time.Sleep(20 * time.Second) | ||
// Delete job | ||
err = client.DeleteJobAlpha1(ctx, "prod-db-backup") | ||
// ... | ||
|
||
if err = server.Stop(); err != nil { | ||
log.Fatalf("failed to stop server: %v\n", err) | ||
} | ||
} | ||
|
||
var jobCount = 0 | ||
|
||
// at job trigger time this function is called | ||
func prodDBBackupHandler(ctx context.Context, job *common.JobEvent) error { | ||
var jobData common.Job | ||
if err := json.Unmarshal(job.Data, &jobData); err != nil { | ||
// ... | ||
} | ||
decodedPayload, err := base64.StdEncoding.DecodeString(jobData.Value) | ||
// ... | ||
|
||
var jobPayload api.DBBackup | ||
if err := json.Unmarshal(decodedPayload, &jobPayload); err != nil { | ||
// ... | ||
} | ||
fmt.Printf("job %d received:\n type: %v \n typeurl: %v\n value: %v\n extracted payload: %v\n", jobCount, job.JobType, jobData.TypeURL, jobData.Value, jobPayload) | ||
jobCount++ | ||
return nil | ||
} | ||
|
||
``` | ||
|
||
{{% /codetab %}} | ||
|
||
|
||
{{< /tabs >}} | ||
|
||
## Run the Dapr sidecar | ||
|
||
Once you've set up the Jobs API in your application, in a terminal window run the Dapr sidecar with the following command. | ||
|
||
```bash | ||
hhunter-ms marked this conversation as resolved.
Show resolved
Hide resolved
|
||
dapr run --app-id=distributed-scheduler \ | ||
--metrics-port=9091 \ | ||
--dapr-grpc-port 50001 \ | ||
--app-port 50070 \ | ||
--app-protocol grpc \ | ||
--log-level debug \ | ||
go run ./main.go | ||
``` | ||
|
||
## Next steps | ||
|
||
- [Learn more about the Scheduler control plane service]({{< ref "concepts/dapr-services/scheduler.md" >}}) | ||
- [Jobs API reference]({{< ref jobs_api.md >}}) |
32 changes: 0 additions & 32 deletions
32
.../content/en/developing-applications/building-blocks/jobs/howto-schedule-jobs.md
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+1.85 KB
(110%)
daprdocs/static/images/install-dapr-selfhost/docker-containers.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The spacing is showing up weird here, but looks fine in my IDE
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it looks fine when I build locally too!