| 
 | 1 | +/*  | 
 | 2 | +Copyright 2021 The Dapr Authors  | 
 | 3 | +Licensed under the Apache License, Version 2.0 (the "License");  | 
 | 4 | +you may not use this file except in compliance with the License.  | 
 | 5 | +You may obtain a copy of the License at  | 
 | 6 | +    http://www.apache.org/licenses/LICENSE-2.0  | 
 | 7 | +Unless required by applicable law or agreed to in writing, software  | 
 | 8 | +distributed under the License is distributed on an "AS IS" BASIS,  | 
 | 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  | 
 | 10 | +See the License for the specific language governing permissions and  | 
 | 11 | +limitations under the License.  | 
 | 12 | +*/  | 
 | 13 | + | 
 | 14 | +package client  | 
 | 15 | + | 
 | 16 | +import (  | 
 | 17 | +	"context"  | 
 | 18 | +	"log"  | 
 | 19 | +	"time"  | 
 | 20 | + | 
 | 21 | +	"google.golang.org/protobuf/types/known/anypb"  | 
 | 22 | +	"google.golang.org/protobuf/types/known/durationpb"  | 
 | 23 | + | 
 | 24 | +	commonpb "github.com/dapr/dapr/pkg/proto/common/v1"  | 
 | 25 | +	runtimepb "github.com/dapr/dapr/pkg/proto/runtime/v1"  | 
 | 26 | +)  | 
 | 27 | + | 
 | 28 | +type FailurePolicy interface {  | 
 | 29 | +	GetPBFailurePolicy() *commonpb.JobFailurePolicy  | 
 | 30 | +}  | 
 | 31 | + | 
 | 32 | +type JobFailurePolicyConstant struct {  | 
 | 33 | +	maxRetries *uint32  | 
 | 34 | +	interval   *time.Duration  | 
 | 35 | +}  | 
 | 36 | + | 
 | 37 | +func (f *JobFailurePolicyConstant) GetPBFailurePolicy() *commonpb.JobFailurePolicy {  | 
 | 38 | +	policy := &commonpb.JobFailurePolicy{  | 
 | 39 | +		Policy: &commonpb.JobFailurePolicy_Constant{  | 
 | 40 | +			Constant: &commonpb.JobFailurePolicyConstant{},  | 
 | 41 | +		},  | 
 | 42 | +	}  | 
 | 43 | +	if f.maxRetries != nil {  | 
 | 44 | +		policy.Policy.(*commonpb.JobFailurePolicy_Constant).Constant.MaxRetries = f.maxRetries  | 
 | 45 | +	}  | 
 | 46 | +	if f.interval != nil {  | 
 | 47 | +		policy.Policy.(*commonpb.JobFailurePolicy_Constant).Constant.Interval = &durationpb.Duration{Seconds: int64(f.interval.Seconds())}  | 
 | 48 | +	}  | 
 | 49 | +	return policy  | 
 | 50 | +}  | 
 | 51 | + | 
 | 52 | +type JobFailurePolicyDrop struct {  | 
 | 53 | +}  | 
 | 54 | + | 
 | 55 | +func (f *JobFailurePolicyDrop) GetPBFailurePolicy() *commonpb.JobFailurePolicy {  | 
 | 56 | +	return &commonpb.JobFailurePolicy{  | 
 | 57 | +		Policy: &commonpb.JobFailurePolicy_Drop{  | 
 | 58 | +			Drop: &commonpb.JobFailurePolicyDrop{},  | 
 | 59 | +		},  | 
 | 60 | +	}  | 
 | 61 | +}  | 
 | 62 | + | 
 | 63 | +func NewFailurePolicyConstant(maxRetries *uint32, interval *time.Duration) FailurePolicy {  | 
 | 64 | +	return &JobFailurePolicyConstant{  | 
 | 65 | +		maxRetries: maxRetries,  | 
 | 66 | +		interval:   interval,  | 
 | 67 | +	}  | 
 | 68 | +}  | 
 | 69 | + | 
 | 70 | +func NewFailurePolicyDrop() FailurePolicy {  | 
 | 71 | +	return &JobFailurePolicyDrop{}  | 
 | 72 | +}  | 
 | 73 | + | 
 | 74 | +type Job struct {  | 
 | 75 | +	Name          string  | 
 | 76 | +	Schedule      string // Optional  | 
 | 77 | +	Repeats       uint32 // Optional  | 
 | 78 | +	DueTime       string // Optional  | 
 | 79 | +	TTL           string // Optional  | 
 | 80 | +	Data          *anypb.Any  | 
 | 81 | +	FailurePolicy FailurePolicy  | 
 | 82 | +}  | 
 | 83 | + | 
 | 84 | +// ScheduleJobAlpha1 raises and schedules a job.  | 
 | 85 | +func (c *GRPCClient) ScheduleJobAlpha1(ctx context.Context, job *Job) error {  | 
 | 86 | +	// TODO: Assert job fields are defined: Name, Data  | 
 | 87 | +	jobRequest := &runtimepb.Job{  | 
 | 88 | +		Name: job.Name,  | 
 | 89 | +		Data: job.Data,  | 
 | 90 | +	}  | 
 | 91 | + | 
 | 92 | +	if job.Schedule != "" {  | 
 | 93 | +		jobRequest.Schedule = &job.Schedule  | 
 | 94 | +	}  | 
 | 95 | + | 
 | 96 | +	if job.Repeats != 0 {  | 
 | 97 | +		jobRequest.Repeats = &job.Repeats  | 
 | 98 | +	}  | 
 | 99 | + | 
 | 100 | +	if job.DueTime != "" {  | 
 | 101 | +		jobRequest.DueTime = &job.DueTime  | 
 | 102 | +	}  | 
 | 103 | + | 
 | 104 | +	if job.TTL != "" {  | 
 | 105 | +		jobRequest.Ttl = &job.TTL  | 
 | 106 | +	}  | 
 | 107 | + | 
 | 108 | +	if job.FailurePolicy != nil {  | 
 | 109 | +		jobRequest.FailurePolicy = job.FailurePolicy.GetPBFailurePolicy()  | 
 | 110 | +	}  | 
 | 111 | +	_, err := c.protoClient.ScheduleJobAlpha1(ctx, &runtimepb.ScheduleJobRequest{  | 
 | 112 | +		Job: jobRequest,  | 
 | 113 | +	})  | 
 | 114 | +	return err  | 
 | 115 | +}  | 
 | 116 | + | 
 | 117 | +// GetJobAlpha1 retrieves a scheduled job.  | 
 | 118 | +func (c *GRPCClient) GetJobAlpha1(ctx context.Context, name string) (*Job, error) {  | 
 | 119 | +	// TODO: Name validation  | 
 | 120 | +	resp, err := c.protoClient.GetJobAlpha1(ctx, &runtimepb.GetJobRequest{  | 
 | 121 | +		Name: name,  | 
 | 122 | +	})  | 
 | 123 | +	log.Println(resp)  | 
 | 124 | +	if err != nil {  | 
 | 125 | +		return nil, err  | 
 | 126 | +	}  | 
 | 127 | + | 
 | 128 | +	var failurePolicy FailurePolicy  | 
 | 129 | +	switch policy := resp.GetJob().GetFailurePolicy().Policy.(type) {  | 
 | 130 | +	case *commonpb.JobFailurePolicy_Constant:  | 
 | 131 | +		interval := time.Duration(policy.Constant.Interval.GetSeconds()) * time.Second  | 
 | 132 | +		failurePolicy = &JobFailurePolicyConstant{  | 
 | 133 | +			maxRetries: policy.Constant.MaxRetries,  | 
 | 134 | +			interval:   &interval,  | 
 | 135 | +		}  | 
 | 136 | +	case *commonpb.JobFailurePolicy_Drop:  | 
 | 137 | +		failurePolicy = &JobFailurePolicyDrop{}  | 
 | 138 | +	}  | 
 | 139 | + | 
 | 140 | +	return &Job{  | 
 | 141 | +		Name:          resp.GetJob().GetName(),  | 
 | 142 | +		Schedule:      resp.GetJob().GetSchedule(),  | 
 | 143 | +		Repeats:       resp.GetJob().GetRepeats(),  | 
 | 144 | +		DueTime:       resp.GetJob().GetDueTime(),  | 
 | 145 | +		TTL:           resp.GetJob().GetTtl(),  | 
 | 146 | +		Data:          resp.GetJob().GetData(),  | 
 | 147 | +		FailurePolicy: failurePolicy,  | 
 | 148 | +	}, nil  | 
 | 149 | +}  | 
 | 150 | + | 
 | 151 | +// DeleteJobAlpha1 deletes a scheduled job.  | 
 | 152 | +func (c *GRPCClient) DeleteJobAlpha1(ctx context.Context, name string) error {  | 
 | 153 | +	// TODO: Name validation  | 
 | 154 | +	_, err := c.protoClient.DeleteJobAlpha1(ctx, &runtimepb.DeleteJobRequest{  | 
 | 155 | +		Name: name,  | 
 | 156 | +	})  | 
 | 157 | +	return err  | 
 | 158 | +}  | 
0 commit comments