add to how-to

Signed-off-by: Hannah Hunter <hannahhunter@microsoft.com>
This commit is contained in:
Hannah Hunter 2024-07-18 14:47:51 -04:00
parent 3a7e34a3be
commit e51f0c219a
1 changed files with 90 additions and 4 deletions

View File

@ -12,18 +12,104 @@ Now that you've learned what the [jobs building block]({{< ref jobs-overview.md
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 ".NET" "Go" >}}
{{% codetab %}}
<!--.net-->
## Set up the Scheduler service
{{% /codetab %}}
When you run `dapr init` in either self-hosted mode or on Kubernetes, the Dapr scheduler service is started.
{{% codetab %}}
<!--go-->
The Go SDK triggers the job called `daprc.Job` to schedule jobs. Job data is housed in a backup database (`"my-prod-db"`) and are called with `ScheduleJobAlpha1`. 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")
// ...
ctx := context.Background()
// 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 data
job := daprc.Job{
Name: "prod-db-backup",
Schedule: "@every 1s",
Repeats: 10,
Data: &anypb.Any{
Value: jobData,
},
}
// Create the client
client, err := daprc.NewClient()
//...
// Schedule job
err = client.ScheduleJobAlpha1(ctx, &job)
// ...
// Get job
resp, err := client.GetJobAlpha1(ctx, "prod-db-backup")
// ...
// Delete job
err = client.DeleteJobAlpha1(ctx, "prod-db-backup")
// ..
}
var jobCount = 0
```
{{% /codetab %}}
{{< /tabs >}}
## Run the Dapr sidecar
Run the Dapr sidecar alongside your application.
Once you've set up the Jobs API in your application, run the Dapr sidecar.
```bash
dapr run --app-id=jobs --app-port 50070 --app-protocol grpc --log-level debug -- go run main.go
dapr run --app-id=distributed-scheduler --metrics-port=9091 --scheduler-host-address=localhost:50006 --dapr-grpc-port 50001 --app-port 50070 --app-protocol grpc --log-level debug go run ./main.go
```
## Next steps