@@ -15,11 +15,10 @@ package client
1515
1616import (
1717 "context"
18- "log "
18+ "errors "
1919 "time"
2020
2121 "google.golang.org/protobuf/types/known/anypb"
22- "google.golang.org/protobuf/types/known/durationpb"
2322
2423 commonpb "github.com/dapr/dapr/pkg/proto/common/v1"
2524 runtimepb "github.com/dapr/dapr/pkg/proto/runtime/v1"
@@ -35,18 +34,18 @@ type JobFailurePolicyConstant struct {
3534}
3635
3736func (f * JobFailurePolicyConstant ) GetPBFailurePolicy () * commonpb.JobFailurePolicy {
38- policy := & commonpb.JobFailurePolicy {
39- Policy : & commonpb.JobFailurePolicy_Constant {
40- Constant : & commonpb.JobFailurePolicyConstant {},
41- },
42- }
37+ constantfp := & commonpb.JobFailurePolicyConstant {}
4338 if f .maxRetries != nil {
44- policy . Policy .( * commonpb. JobFailurePolicy_Constant ). Constant .MaxRetries = f .maxRetries
39+ constantfp .MaxRetries = f .maxRetries
4540 }
4641 if f .interval != nil {
47- policy .Policy .(* commonpb.JobFailurePolicy_Constant ).Constant .Interval = & durationpb.Duration {Seconds : int64 (f .interval .Seconds ())}
42+ constantfp .Interval = toProtoDuration (* f .interval )
43+ }
44+ return & commonpb.JobFailurePolicy {
45+ Policy : & commonpb.JobFailurePolicy_Constant {
46+ Constant : constantfp ,
47+ },
4848 }
49- return policy
5049}
5150
5251type JobFailurePolicyDrop struct {
@@ -73,36 +72,30 @@ func NewFailurePolicyDrop() FailurePolicy {
7372
7473type Job struct {
7574 Name string
76- Schedule string // Optional
77- Repeats uint32 // Optional
78- DueTime string // Optional
79- TTL string // Optional
75+ Schedule * string
76+ Repeats * uint32
77+ DueTime * string
78+ TTL * string
8079 Data * anypb.Any
8180 FailurePolicy FailurePolicy
8281}
8382
8483// ScheduleJobAlpha1 raises and schedules a job.
8584func (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
85+ if job .Name == "" {
86+ return errors .New ("job name is required" )
9487 }
95-
96- if job .Repeats != 0 {
97- jobRequest .Repeats = & job .Repeats
88+ if job .Data == nil {
89+ return errors .New ("job data is required" )
9890 }
9991
100- if job .DueTime != "" {
101- jobRequest .DueTime = & job .DueTime
102- }
103-
104- if job .TTL != "" {
105- jobRequest .Ttl = & job .TTL
92+ jobRequest := & runtimepb.Job {
93+ Name : job .Name ,
94+ Data : job .Data ,
95+ Schedule : job .Schedule ,
96+ Repeats : job .Repeats ,
97+ DueTime : job .DueTime ,
98+ Ttl : job .TTL ,
10699 }
107100
108101 if job .FailurePolicy != nil {
@@ -116,11 +109,13 @@ func (c *GRPCClient) ScheduleJobAlpha1(ctx context.Context, job *Job) error {
116109
117110// GetJobAlpha1 retrieves a scheduled job.
118111func (c * GRPCClient ) GetJobAlpha1 (ctx context.Context , name string ) (* Job , error ) {
119- // TODO: Name validation
112+ if name == "" {
113+ return nil , errors .New ("job name is required" )
114+ }
115+
120116 resp , err := c .protoClient .GetJobAlpha1 (ctx , & runtimepb.GetJobRequest {
121117 Name : name ,
122118 })
123- log .Println (resp )
124119 if err != nil {
125120 return nil , err
126121 }
@@ -139,18 +134,21 @@ func (c *GRPCClient) GetJobAlpha1(ctx context.Context, name string) (*Job, error
139134
140135 return & Job {
141136 Name : resp .GetJob ().GetName (),
142- Schedule : resp .GetJob ().GetSchedule () ,
143- Repeats : resp .GetJob ().GetRepeats () ,
144- DueTime : resp .GetJob ().GetDueTime () ,
145- TTL : resp .GetJob ().GetTtl () ,
137+ Schedule : resp .GetJob ().Schedule ,
138+ Repeats : resp .GetJob ().Repeats ,
139+ DueTime : resp .GetJob ().DueTime ,
140+ TTL : resp .GetJob ().Ttl ,
146141 Data : resp .GetJob ().GetData (),
147142 FailurePolicy : failurePolicy ,
148143 }, nil
149144}
150145
151146// DeleteJobAlpha1 deletes a scheduled job.
152147func (c * GRPCClient ) DeleteJobAlpha1 (ctx context.Context , name string ) error {
153- // TODO: Name validation
148+ if name == "" {
149+ return errors .New ("job name is required" )
150+ }
151+
154152 _ , err := c .protoClient .DeleteJobAlpha1 (ctx , & runtimepb.DeleteJobRequest {
155153 Name : name ,
156154 })
0 commit comments