-
Notifications
You must be signed in to change notification settings - Fork 175
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement distributed scheduler building block (#562)
* feat: add jobs/scheduling api (with validation override) Signed-off-by: mikeee <[email protected]> * chore: fix deps Signed-off-by: mikeee <[email protected]> * fix: use cli fix Signed-off-by: mikeee <[email protected]> * fix: ci artifact path set for cli build Signed-off-by: mikeee <[email protected]> * chore: remove sidecar step Signed-off-by: mikeee <[email protected]> * chore: revert changes to other examples Signed-off-by: mikeee <[email protected]> --------- Signed-off-by: mikeee <[email protected]> Signed-off-by: Mike Nguyen <[email protected]>
- Loading branch information
Showing
19 changed files
with
488 additions
and
33 deletions.
There are no files selected for viewing
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
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
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,91 @@ | ||
/* | ||
Copyright 2021 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 client | ||
|
||
import ( | ||
"context" | ||
"log" | ||
|
||
"google.golang.org/protobuf/types/known/anypb" | ||
|
||
pb "github.com/dapr/dapr/pkg/proto/runtime/v1" | ||
) | ||
|
||
type Job struct { | ||
Name string | ||
Schedule string | ||
Repeats uint32 // Optional | ||
DueTime string // Optional | ||
TTL string // Optional | ||
Data *anypb.Any | ||
} | ||
|
||
// ScheduleJobAlpha1 raises and schedules a job. | ||
func (c *GRPCClient) ScheduleJobAlpha1(ctx context.Context, job *Job) error { | ||
// TODO: Assert job fields are defined: Name, Schedule, Data | ||
jobRequest := &pb.Job{ | ||
Name: job.Name, | ||
Schedule: &job.Schedule, | ||
Data: job.Data, | ||
} | ||
|
||
if job.Schedule != "" { | ||
jobRequest.Schedule = &job.Schedule | ||
} | ||
|
||
if job.Repeats != 0 { | ||
jobRequest.Repeats = &job.Repeats | ||
} | ||
|
||
if job.DueTime != "" { | ||
jobRequest.DueTime = &job.DueTime | ||
} | ||
|
||
if job.TTL != "" { | ||
jobRequest.Ttl = &job.TTL | ||
} | ||
_, err := c.protoClient.ScheduleJobAlpha1(ctx, &pb.ScheduleJobRequest{ | ||
Job: jobRequest, | ||
}) | ||
return err | ||
} | ||
|
||
// GetJobAlpha1 retrieves a scheduled job. | ||
func (c *GRPCClient) GetJobAlpha1(ctx context.Context, name string) (*Job, error) { | ||
// TODO: Name validation | ||
resp, err := c.protoClient.GetJobAlpha1(ctx, &pb.GetJobRequest{ | ||
Name: name, | ||
}) | ||
log.Println(resp) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &Job{ | ||
Name: resp.GetJob().GetName(), | ||
Schedule: resp.GetJob().GetSchedule(), | ||
Repeats: resp.GetJob().GetRepeats(), | ||
DueTime: resp.GetJob().GetDueTime(), | ||
TTL: resp.GetJob().GetTtl(), | ||
Data: resp.GetJob().GetData(), | ||
}, nil | ||
} | ||
|
||
// DeleteJobAlpha1 deletes a scheduled job. | ||
func (c *GRPCClient) DeleteJobAlpha1(ctx context.Context, name string) error { | ||
// TODO: Name validation | ||
_, err := c.protoClient.DeleteJobAlpha1(ctx, &pb.DeleteJobRequest{ | ||
Name: name, | ||
}) | ||
return err | ||
} |
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,58 @@ | ||
/* | ||
Copyright 2021 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 client | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"google.golang.org/protobuf/types/known/anypb" | ||
) | ||
|
||
func TestSchedulingAlpha1(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
t.Run("schedule job - valid", func(t *testing.T) { | ||
err := testClient.ScheduleJobAlpha1(ctx, &Job{ | ||
Name: "test", | ||
Schedule: "test", | ||
Data: &anypb.Any{}, | ||
}) | ||
|
||
require.NoError(t, err) | ||
}) | ||
|
||
t.Run("get job - valid", func(t *testing.T) { | ||
expected := &Job{ | ||
Name: "name", | ||
Schedule: "@every 10s", | ||
Repeats: 4, | ||
DueTime: "10s", | ||
TTL: "10s", | ||
Data: nil, | ||
} | ||
|
||
resp, err := testClient.GetJobAlpha1(ctx, "name") | ||
require.NoError(t, err) | ||
assert.Equal(t, expected, resp) | ||
}) | ||
|
||
t.Run("delete job - valid", func(t *testing.T) { | ||
err := testClient.DeleteJobAlpha1(ctx, "name") | ||
|
||
require.NoError(t, err) | ||
}) | ||
} |
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,43 @@ | ||
# Dapr Distributed Scheduler Example with go-sdk | ||
|
||
## Steps | ||
|
||
### Prepare | ||
|
||
- Dapr installed (v1.14 or higher) | ||
|
||
### Run Distributed Scheduling Example | ||
|
||
<!-- STEP | ||
name: Run Distributed Scheduling Example | ||
output_match_mode: substring | ||
expected_stdout_lines: | ||
- 'Scheduler stream connected' | ||
- 'schedulejob - success' | ||
- 'job 0 received' | ||
- 'extracted payload: {db-backup {my-prod-db /backup-dir}}' | ||
- 'job 1 received' | ||
- 'extracted payload: {db-backup {my-prod-db /backup-dir}}' | ||
- 'job 2 received' | ||
- 'extracted payload: {db-backup {my-prod-db /backup-dir}}' | ||
- 'getjob - resp: &{prod-db-backup @every 1s 10 value:"{\"task\":\"db-backup\",\"metadata\":{\"db_name\":\"my-prod-db\",\"backup_location\":\"/backup-dir\"}}"}' | ||
- 'deletejob - success' | ||
background: true | ||
sleep: 30 | ||
--> | ||
|
||
```bash | ||
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 | ||
|
||
``` | ||
|
||
<!-- END_STEP --> |
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,11 @@ | ||
package api | ||
|
||
type Metadata struct { | ||
DBName string `json:"db_name"` | ||
BackupLocation string `json:"backup_location"` | ||
} | ||
|
||
type DBBackup struct { | ||
Task string `json:"task"` | ||
Metadata Metadata `json:"metadata"` | ||
} |
Oops, something went wrong.