Skip to content
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 SDK tests #1074

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions jobs/go/http/job-scheduler/job-scheduler.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
/*
Copyright 2024 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

/*
dapr run --app-id job-scheduler --app-port 6300 --dapr-http-port 6380 -- go run .
*/

import (
"fmt"
"io"
Expand Down
6 changes: 5 additions & 1 deletion jobs/go/http/job-service/job-service.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2021 The Dapr Authors
Copyright 2024 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Expand All @@ -13,6 +13,10 @@ limitations under the License.

package main

/*
dapr run --app-id job-service --app-port 6200 --dapr-http-port 6280 -- go run .
*/

import (
"encoding/base64"
"encoding/json"
Expand Down
12 changes: 7 additions & 5 deletions jobs/go/sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ Open a new terminal window and run the multi app run template:
<!-- STEP
name: Run multi app run template (disabled)
expected_stdout_lines:
- '== APP - job-service == Starting droid: R2-D2'
- '== APP - job-service == Executing maintenance job: Oil Change'
- '== APP - job-service == Starting droid: C-3PO'
- '== APP - job-service == Executing maintenance job: Memory Wipe'
expected_stderr_lines:
output_match_mode: substring
match_order: none
background: true
sleep: 15
timeout_seconds: 30
sleep: 120
timeout_seconds: 180
-->
<!-- END_STEP -->

```bash
dapr run -f .
Expand All @@ -44,7 +47,6 @@ The terminal console output should look similar to this, where:
- The `R2-D2` job is being executed after 5 seconds.
- The `R2-D2` job is being executed after 10 seconds.


```text
== APP - job-service == dapr client initializing for: 127.0.0.1:6481
== APP - job-service == Registered job handler for: R2-D2
Expand Down Expand Up @@ -81,7 +83,7 @@ After 10 seconds, the terminal output should present the `C3-PO` job being proce
dapr stop -f .
```


<!-- END_STEP -->

## Run the Jobs APIs individually

Expand Down
19 changes: 16 additions & 3 deletions jobs/go/sdk/job-scheduler/job-scheduler.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2021 The Dapr Authors
Copyright 2024 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Expand All @@ -13,6 +13,10 @@ limitations under the License.

package main

/*
dapr run --app-id job-scheduler --app-port 6500 -- go run .
*/

import (
"context"
"encoding/json"
Expand All @@ -36,8 +40,6 @@ type App struct {
var app App

func main() {
// Waiting 15 seconds for the job-service to start
time.Sleep(15 * time.Second)

droidJobs := []DroidJob{
{Name: "R2-D2", Job: "Oil Change", DueTime: "5s"},
Expand All @@ -56,6 +58,17 @@ func main() {
daprClient: daprClient,
}

// Wait for job-service to be up and running
for {
_, err = daprClient.InvokeMethod(context.Background(), "job-service", "healthz", "GET")
if err != nil {
fmt.Println("job-service not available, retrying...")
} else {
break
}
time.Sleep(2 * time.Second) // Wait for 2 seconds before retrying
}

// Schedule R2-D2 job
err = schedule(droidJobs[0])
if err != nil {
Expand Down
20 changes: 18 additions & 2 deletions jobs/go/sdk/job-service/job-service.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2021 The Dapr Authors
Copyright 2024 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
Expand All @@ -14,7 +14,7 @@ limitations under the License.
package main

/*
dapr run --app-id maintenance-scheduler --app-port 5200 --dapr-http-port 5280 --dapr-grpc-port 5281 --scheduler-host-address=127.0.0.1:50006 -- go run .
dapr run --app-id job-service --app-port 6400 --dapr-grpc-port 6481 --app-protocol grpc -- go run .
*/

import (
Expand Down Expand Up @@ -89,6 +89,11 @@ func main() {
log.Fatalf("error adding invocation handler: %v", err)
}

// add helathz handler
if err := server.AddServiceInvocationHandler("healthz", healthz); err != nil {
log.Fatalf("error adding invocation handler: %v", err)
}

// Register job event handler for all jobs
for _, jobName := range jobNames {
if err := server.AddJobEventHandler(jobName, handleJob); err != nil {
Expand Down Expand Up @@ -199,6 +204,17 @@ func deleteJob(ctx context.Context, in *common.InvocationEvent) (out *common.Con
return out, err
}

// Healthz handler
func healthz(ctx context.Context, in *common.InvocationEvent) (out *common.Content, err error) {
out = &common.Content{
Data: in.Data,
ContentType: in.ContentType,
DataTypeURL: in.DataTypeURL,
}

return out, err
}

// Handler that handles job events
func handleJob(ctx context.Context, job *common.JobEvent) error {
var jobData common.Job
Expand Down
Loading