|
| 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 | + "errors" |
| 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 | + "github.com/dapr/kit/ptr" |
| 27 | +) |
| 28 | + |
| 29 | +type FailurePolicy interface { |
| 30 | + GetPBFailurePolicy() *commonpb.JobFailurePolicy |
| 31 | +} |
| 32 | + |
| 33 | +type JobFailurePolicyConstant struct { |
| 34 | + MaxRetries *uint32 |
| 35 | + Interval *time.Duration |
| 36 | +} |
| 37 | + |
| 38 | +func (f *JobFailurePolicyConstant) GetPBFailurePolicy() *commonpb.JobFailurePolicy { |
| 39 | + constantfp := &commonpb.JobFailurePolicyConstant{} |
| 40 | + if f.MaxRetries != nil { |
| 41 | + constantfp.MaxRetries = f.MaxRetries |
| 42 | + } |
| 43 | + if f.Interval != nil { |
| 44 | + constantfp.Interval = durationpb.New(*f.Interval) |
| 45 | + } |
| 46 | + return &commonpb.JobFailurePolicy{ |
| 47 | + Policy: &commonpb.JobFailurePolicy_Constant{ |
| 48 | + Constant: constantfp, |
| 49 | + }, |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +type JobFailurePolicyDrop struct{} |
| 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 | +type Job struct { |
| 64 | + Name string |
| 65 | + Schedule *string |
| 66 | + Repeats *uint32 |
| 67 | + DueTime *string |
| 68 | + TTL *string |
| 69 | + Data *anypb.Any |
| 70 | + FailurePolicy FailurePolicy |
| 71 | +} |
| 72 | + |
| 73 | +type JobOption func(*Job) |
| 74 | + |
| 75 | +func NewJob(name string, opts ...JobOption) *Job { |
| 76 | + job := &Job{ |
| 77 | + Name: name, |
| 78 | + } |
| 79 | + for _, opt := range opts { |
| 80 | + opt(job) |
| 81 | + } |
| 82 | + return job |
| 83 | +} |
| 84 | + |
| 85 | +func WithJobSchedule(schedule string) JobOption { |
| 86 | + return func(job *Job) { |
| 87 | + job.Schedule = &schedule |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func WithJobRepeats(repeats uint32) JobOption { |
| 92 | + return func(job *Job) { |
| 93 | + job.Repeats = &repeats |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func WithJobDueTime(dueTime string) JobOption { |
| 98 | + return func(job *Job) { |
| 99 | + job.DueTime = &dueTime |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func WithJobTTL(ttl string) JobOption { |
| 104 | + return func(job *Job) { |
| 105 | + job.TTL = &ttl |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func WithJobData(data *anypb.Any) JobOption { |
| 110 | + return func(job *Job) { |
| 111 | + job.Data = data |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +func WithJobConstantFailurePolicy() JobOption { |
| 116 | + return func(job *Job) { |
| 117 | + job.FailurePolicy = &JobFailurePolicyConstant{} |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +func WithJobConstantFailurePolicyMaxRetries(maxRetries uint32) JobOption { |
| 122 | + return func(job *Job) { |
| 123 | + if job.FailurePolicy == nil { |
| 124 | + job.FailurePolicy = &JobFailurePolicyConstant{} |
| 125 | + } |
| 126 | + if constantPolicy, ok := job.FailurePolicy.(*JobFailurePolicyConstant); ok { |
| 127 | + constantPolicy.MaxRetries = &maxRetries |
| 128 | + } else { |
| 129 | + job.FailurePolicy = &JobFailurePolicyConstant{ |
| 130 | + MaxRetries: &maxRetries, |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +func WithJobConstantFailurePolicyInterval(interval time.Duration) JobOption { |
| 137 | + return func(job *Job) { |
| 138 | + if job.FailurePolicy == nil { |
| 139 | + job.FailurePolicy = &JobFailurePolicyConstant{} |
| 140 | + } |
| 141 | + if constantPolicy, ok := job.FailurePolicy.(*JobFailurePolicyConstant); ok { |
| 142 | + constantPolicy.Interval = &interval |
| 143 | + } else { |
| 144 | + job.FailurePolicy = &JobFailurePolicyConstant{ |
| 145 | + Interval: &interval, |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +func WithJobDropFailurePolicy() JobOption { |
| 152 | + return func(job *Job) { |
| 153 | + job.FailurePolicy = &JobFailurePolicyDrop{} |
| 154 | + } |
| 155 | +} |
| 156 | + |
| 157 | +// ScheduleJobAlpha1 raises and schedules a job. |
| 158 | +func (c *GRPCClient) ScheduleJobAlpha1(ctx context.Context, job *Job) error { |
| 159 | + if job.Name == "" { |
| 160 | + return errors.New("job name is required") |
| 161 | + } |
| 162 | + if job.Data == nil { |
| 163 | + return errors.New("job data is required") |
| 164 | + } |
| 165 | + |
| 166 | + jobRequest := &runtimepb.Job{ |
| 167 | + Name: job.Name, |
| 168 | + Data: job.Data, |
| 169 | + Schedule: job.Schedule, |
| 170 | + Repeats: job.Repeats, |
| 171 | + DueTime: job.DueTime, |
| 172 | + Ttl: job.TTL, |
| 173 | + } |
| 174 | + |
| 175 | + if job.FailurePolicy != nil { |
| 176 | + jobRequest.FailurePolicy = job.FailurePolicy.GetPBFailurePolicy() |
| 177 | + } |
| 178 | + _, err := c.protoClient.ScheduleJobAlpha1(ctx, &runtimepb.ScheduleJobRequest{ |
| 179 | + Job: jobRequest, |
| 180 | + }) |
| 181 | + return err |
| 182 | +} |
| 183 | + |
| 184 | +// GetJobAlpha1 retrieves a scheduled job. |
| 185 | +func (c *GRPCClient) GetJobAlpha1(ctx context.Context, name string) (*Job, error) { |
| 186 | + if name == "" { |
| 187 | + return nil, errors.New("job name is required") |
| 188 | + } |
| 189 | + |
| 190 | + resp, err := c.protoClient.GetJobAlpha1(ctx, &runtimepb.GetJobRequest{ |
| 191 | + Name: name, |
| 192 | + }) |
| 193 | + if err != nil { |
| 194 | + return nil, err |
| 195 | + } |
| 196 | + |
| 197 | + var failurePolicy FailurePolicy |
| 198 | + switch policy := resp.GetJob().GetFailurePolicy().GetPolicy().(type) { |
| 199 | + case *commonpb.JobFailurePolicy_Constant: |
| 200 | + interval := time.Duration(policy.Constant.GetInterval().GetSeconds()) * time.Second |
| 201 | + failurePolicy = &JobFailurePolicyConstant{ |
| 202 | + MaxRetries: ptr.Of(policy.Constant.GetMaxRetries()), |
| 203 | + Interval: &interval, |
| 204 | + } |
| 205 | + case *commonpb.JobFailurePolicy_Drop: |
| 206 | + failurePolicy = &JobFailurePolicyDrop{} |
| 207 | + } |
| 208 | + |
| 209 | + return &Job{ |
| 210 | + Name: resp.GetJob().GetName(), |
| 211 | + Schedule: ptr.Of(resp.GetJob().GetSchedule()), |
| 212 | + Repeats: ptr.Of(resp.GetJob().GetRepeats()), |
| 213 | + DueTime: ptr.Of(resp.GetJob().GetDueTime()), |
| 214 | + TTL: ptr.Of(resp.GetJob().GetTtl()), |
| 215 | + Data: resp.GetJob().GetData(), |
| 216 | + FailurePolicy: failurePolicy, |
| 217 | + }, nil |
| 218 | +} |
| 219 | + |
| 220 | +// DeleteJobAlpha1 deletes a scheduled job. |
| 221 | +func (c *GRPCClient) DeleteJobAlpha1(ctx context.Context, name string) error { |
| 222 | + if name == "" { |
| 223 | + return errors.New("job name is required") |
| 224 | + } |
| 225 | + |
| 226 | + _, err := c.protoClient.DeleteJobAlpha1(ctx, &runtimepb.DeleteJobRequest{ |
| 227 | + Name: name, |
| 228 | + }) |
| 229 | + return err |
| 230 | +} |
0 commit comments