Skip to content

Commit

Permalink
feat: add jobs/scheduling api (with validation override)
Browse files Browse the repository at this point in the history
Signed-off-by: mikeee <[email protected]>
  • Loading branch information
mikeee committed Jul 10, 2024
1 parent 33180dd commit fc49b68
Show file tree
Hide file tree
Showing 52 changed files with 779 additions and 818 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/fossa.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ jobs:
uses: actions/checkout@v4

- name: "Run FOSSA Scan"
uses: fossas/fossa-action@v1.1.0 # Use a specific version if locking is preferred
uses: fossas/fossa-action@v1.3.3 # Use a specific version if locking is preferred
with:
api-key: ${{ env.FOSSA_API_KEY }}

- name: "Run FOSSA Test"
uses: fossas/fossa-action@v1.1.0 # Use a specific version if locking is preferred
uses: fossas/fossa-action@v1.3.3 # Use a specific version if locking is preferred
with:
api-key: ${{ env.FOSSA_API_KEY }}
run-tests: true
4 changes: 3 additions & 1 deletion .github/workflows/test-on-push.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: recursive

- name: Setup
uses: actions/setup-go@v5
Expand All @@ -39,7 +41,7 @@ jobs:
uses: codecov/codecov-action@v4

- name: Lint
uses: golangci/golangci-lint-action@v3.3.1
uses: golangci/golangci-lint-action@v6
with:
version: ${{ env.GOLANGCILINT_VER }}
skip-cache: true
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test-tooling.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
run: make tidy

- name: Lint
uses: golangci/golangci-lint-action@v3
uses: golangci/golangci-lint-action@v6
with:
version: ${{ env.GOLANGCILINT_VER }}
working-directory: ./tools/check-lint-version
Expand Down
49 changes: 28 additions & 21 deletions .github/workflows/validate_examples.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ jobs:
CHECKOUT_REF: ${{ github.ref }}
outputs:
DAPR_INSTALL_URL: ${{ env.DAPR_INSTALL_URL }}
DAPR_CLI_VER: ${{ steps.outputs.outputs.DAPR_CLI_VER }}
DAPR_RUNTIME_VER: ${{ steps.outputs.outputs.DAPR_RUNTIME_VER }}
DAPR_CLI_VER: 1.14.0-rc.2
DAPR_CLI_REF: ${{ steps.outputs.outputs.DAPR_CLI_REF }}
DAPR_RUNTIME_VER: 1.14.0-rc.2
CHECKOUT_REPO: ${{ steps.outputs.outputs.CHECKOUT_REPO }}
CHECKOUT_REF: ${{ steps.outputs.outputs.CHECKOUT_REF }}
DAPR_REF: ${{ steps.outputs.outputs.DAPR_REF }}
GITHUB_SHA: ${{ steps.outputs.outputs.GITHUB_SHA }}
steps:
- name: Parse repository_dispatch payload
if: github.event_name == 'repository_dispatch'
Expand Down Expand Up @@ -79,9 +81,6 @@ jobs:
echo "DAPR_CLI_VER=$CLI_VERSION" >> $GITHUB_ENV
echo "Found $CLI_VERSION"
- name: Set up Dapr CLI
run: wget -q ${{ env.DAPR_INSTALL_URL }} -O - | /bin/bash -s ${{ env.DAPR_CLI_VER }}

- name: Checkout Dapr CLI repo to override dapr command.
uses: actions/checkout@v4
if: env.DAPR_CLI_REF != ''
Expand All @@ -104,23 +103,24 @@ jobs:
cd cli
make
mkdir -p $HOME/artifacts/$GITHUB_SHA/
sudo cp dist/linux_amd64/release/dapr $HOME/artifacts/$GITHUB_SHA/dapr
sudo cp dist/linux_amd64/release/dapr ~/artifacts/$GITHUB_SHA/dapr
echo "DAPR_CLI_REF=$DAPR_CLI_REF" >> $GITHUB_ENV
- name: Build daprd and placement with referenced commit.
- name: Build dapr
if: env.DAPR_REF != ''
run: |
cd dapr_runtime
make
mkdir -p $HOME/artifacts/$GITHUB_SHA/
cp dist/linux_amd64/release/daprd $HOME/artifacts/$GITHUB_SHA/daprd
cp dist/linux_amd64/release/placement $HOME/artifacts/$GITHUB_SHA/placement
echo "artifactPath=~/artifacts/$GITHUB_SHA/" >> $GITHUB_ENV
cp ./dist/linux_amd64/release/* ~/artifacts/$GITHUB_SHA/
- name: Upload dapr-artifacts
uses: actions/upload-artifact@v4
if: env.DAPR_REF != '' || env.DAPR_CLI_REF != ''
with:
name: dapr-artifacts
path: $HOME/artifacts/$GITHUB_SHA/
path: ${{ env.artifactPath }}
if-no-files-found: error
retention-days: 1
compression-level: 0
Expand All @@ -130,10 +130,12 @@ jobs:
run: |
echo "DAPR_INSTALL_URL=$DAPR_INSTALL_URL"
echo "DAPR_CLI_VER=$DAPR_CLI_VER" >> "$GITHUB_OUTPUT"
echo "DAPR_CLI_REF=$DAPR_CLI_REF" >> "$GITHUB_OUTPUT"
echo "DAPR_RUNTIME_VER=$DAPR_RUNTIME_VER" >> "$GITHUB_OUTPUT"
echo "CHECKOUT_REPO=$CHECKOUT_REPO" >> "$GITHUB_OUTPUT"
echo "CHECKOUT_REF=$CHECKOUT_REF" >> "$GITHUB_OUTPUT"
echo "DAPR_REF=$DAPR_REF" >> "$GITHUB_OUTPUT"
echo "GITHUB_SHA=$GITHUB_SHA" >> "$GITHUB_OUTPUT"
validate-example:
needs: setup
Expand All @@ -146,10 +148,11 @@ jobs:
DAPR_INSTALL_URL: ${{ needs.setup.outputs.DAPR_INSTALL_URL }}
DAPR_CLI_VER: ${{ needs.setup.outputs.DAPR_CLI_VER }}
DAPR_RUNTIME_VER: ${{ needs.setup.outputs.DAPR_RUNTIME_VER }}
DAPR_CLI_REF: ${{ github.event.inputs.daprcli_commit }}
DAPR_REF: ${{ github.event.inputs.daprdapr_commit }}
DAPR_CLI_REF: ${{ needs.setup.outputs.DAPR_CLI_REF }}
DAPR_REF: ${{ needs.setup.outputs.DAPR_REF }}
CHECKOUT_REPO: ${{ needs.setup.outputs.CHECKOUT_REPO }}
CHECKOUT_REF: ${{ needs.setup.outputs.CHECKOUT_REF }}
GITHUB_SHA: ${{ needs.setup.outputs.GITHUB_SHA }}

strategy:
fail-fast: false
Expand All @@ -159,6 +162,7 @@ jobs:
"actor",
"configuration",
"crypto",
"dist-scheduler",
"grpc-service",
"hello-world",
"pubsub",
Expand All @@ -184,7 +188,11 @@ jobs:
uses: actions/download-artifact@v4
with:
name: dapr-artifacts
path: $HOME/artifacts/$GITHUB_SHA/
path: ~/artifacts/${{ env.GITHUB_SHA }}/

- name: Display artifacts downloaded
if: env.DAPR_CLI_REF != '' || env.DAPR_REF != ''
run: ls ~/artifacts/$GITHUB_SHA/

- name: Set up Go
id: setup-go
Expand All @@ -193,25 +201,24 @@ jobs:
go-version-file: "go.mod"

- name: Set up Dapr CLI
if: env.DAPR_CLI_VER != ''
run: wget -q ${{ env.DAPR_INSTALL_URL }} -O - | /bin/bash -s ${{ env.DAPR_CLI_VER }}

- name: Override dapr cli with referenced commit.
if: env.DAPR_CLI_REF != ''
run: |
sudo cp $HOME/artifacts/$GITHUB_SHA/dapr /usr/local/bin/dapr
- name: Initialize Dapr runtime ${{ env.DAPR_RUNTIME_VER }}
run: |
dapr uninstall --all
dapr init --runtime-version ${{ env.DAPR_RUNTIME_VER }}
- name: Override daprd and placement service with referenced commit.
- name: Override dapr cli with referenced commit.
if: env.DAPR_CLI_REF != ''
run: |
sudo cp $HOME/artifacts/$GITHUB_SHA/dapr /usr/local/bin/dapr
- name: Override daprd with referenced commit.
if: env.DAPR_REF != ''
run: |
mkdir -p $HOME/.dapr/bin/
cp $HOME/artifacts/$GITHUB_SHA/daprd $HOME/.dapr/bin/daprd
docker stop dapr_placement
$HOME/artifacts/$GITHUB_SHA/placement --healthz-port 9091 &
- name: Set up Python ${{ env.PYTHON_VER }}
uses: actions/setup-python@v5
Expand Down
15 changes: 12 additions & 3 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,15 @@ type Client interface {
// RaiseEventWorkflowBeta1 raises an event for a workflow.
RaiseEventWorkflowBeta1(ctx context.Context, req *RaiseEventWorkflowRequest) error

// ScheduleJobAlpha1 creates and schedules a job.
ScheduleJobAlpha1(ctx context.Context, req *Job) error

// GetJobAlpha1 returns a scheduled job.
GetJobAlpha1(ctx context.Context, name string) (*Job, error)

// DeleteJobAlpha1 deletes a scheduled job.
DeleteJobAlpha1(ctx context.Context, name string) error

// GrpcClient returns the base grpc client if grpc is used and nil otherwise
GrpcClient() pb.DaprClient

Expand Down Expand Up @@ -325,7 +334,7 @@ func NewClientWithAddressContext(ctx context.Context, address string) (client Cl

opts := []grpc.DialOption{
grpc.WithUserAgent(userAgent()),
grpc.WithBlock(),
grpc.WithBlock(), //nolint:staticcheck
authTokenUnaryInterceptor(at),
authTokenStreamInterceptor(at),
}
Expand All @@ -337,7 +346,7 @@ func NewClientWithAddressContext(ctx context.Context, address string) (client Cl
}

ctx, cancel := context.WithTimeout(ctx, time.Duration(timeoutSeconds)*time.Second)
conn, err := grpc.DialContext(
conn, err := grpc.DialContext( //nolint:staticcheck
ctx,
parsedAddress.Target,
opts...,
Expand Down Expand Up @@ -373,7 +382,7 @@ func NewClientWithSocket(socket string) (client Client, err error) {
at := &authToken{}
logger.Printf("dapr client initializing for: %s", socket)
addr := "unix://" + socket
conn, err := grpc.Dial(
conn, err := grpc.Dial( //nolint:staticcheck
addr,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithUserAgent(userAgent()),
Expand Down
29 changes: 28 additions & 1 deletion client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func getTestClient(ctx context.Context) (client Client, closer func()) {
d := grpc.WithContextDialer(func(context.Context, string) (net.Conn, error) {
return l.Dial()
})

//nolint:staticcheck
c, err := grpc.DialContext(ctx, "", d, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
logger.Fatalf("failed to dial test context: %v", err)
Expand Down Expand Up @@ -557,6 +557,33 @@ func (s *testDaprServer) RaiseEventWorkflowBeta1(ctx context.Context, in *pb.Rai
return &emptypb.Empty{}, nil
}

func (s *testDaprServer) ScheduleJobAlpha1(ctx context.Context, in *pb.ScheduleJobRequest) (*pb.ScheduleJobResponse, error) {
return &pb.ScheduleJobResponse{}, nil
}

func (s *testDaprServer) GetJobAlpha1(ctx context.Context, in *pb.GetJobRequest) (*pb.GetJobResponse, error) {
var (
schedule = "@every 10s"
dueTime = "10s"
repeats uint32 = 4
ttl = "10s"
)
return &pb.GetJobResponse{
Job: &pb.Job{
Name: "name",
Schedule: &schedule,
Repeats: &repeats,
DueTime: &dueTime,
Ttl: &ttl,
Data: nil,
},
}, nil
}

func (s *testDaprServer) DeleteJobAlpha1(ctx context.Context, in *pb.DeleteJobRequest) (*pb.DeleteJobResponse, error) {
return &pb.DeleteJobResponse{}, nil
}

func TestGrpcClient(t *testing.T) {
protoClient := pb.NewDaprClient(nil)
client := &GRPCClient{protoClient: protoClient}
Expand Down
91 changes: 91 additions & 0 deletions client/scheduling.go
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
}
Loading

0 comments on commit fc49b68

Please sign in to comment.