@@ -188,34 +188,22 @@ func (s *scheduler) run() error {
188
188
s .logger .Warn ("Time went backwards" , "from" , t1 , "to" , t2 )
189
189
t2 = t1
190
190
}
191
- nextSleep , err := s .processTimeRange (
191
+ nextSleep := s .processTimeRange (
192
192
t1 , t2 ,
193
193
// resolve this to the schedule's policy as late as possible
194
194
enumspb .SCHEDULE_OVERLAP_POLICY_UNSPECIFIED ,
195
195
false ,
196
196
)
197
- if err != nil {
198
- return err
199
- }
200
197
s .State .LastProcessedTime = timestamp .TimePtr (t2 )
201
198
// handle signals after processing time range that just elapsed
202
199
scheduleChanged := s .processSignals ()
203
200
if scheduleChanged {
204
201
// need to calculate sleep again
205
- nextSleep , err = s .processTimeRange (t2 , t2 , enumspb .SCHEDULE_OVERLAP_POLICY_UNSPECIFIED , false )
206
- if err != nil {
207
- return err
208
- }
202
+ nextSleep = s .processTimeRange (t2 , t2 , enumspb .SCHEDULE_OVERLAP_POLICY_UNSPECIFIED , false )
209
203
}
210
204
// try starting workflows in the buffer
211
- for {
212
- b , err := s .processBuffer ()
213
- if err != nil {
214
- return err
215
- }
216
- if ! b {
217
- break
218
- }
205
+ //nolint:revive
206
+ for s .processBuffer () {
219
207
}
220
208
s .updateMemoAndSearchAttributes ()
221
209
// sleep returns on any of:
@@ -327,11 +315,11 @@ func (s *scheduler) processTimeRange(
327
315
t1 , t2 time.Time ,
328
316
overlapPolicy enumspb.ScheduleOverlapPolicy ,
329
317
manual bool ,
330
- ) ( time.Duration , error ) {
318
+ ) time.Duration {
331
319
s .logger .Debug ("processTimeRange" , "t1" , t1 , "t2" , t2 , "overlapPolicy" , overlapPolicy , "manual" , manual )
332
320
333
321
if s .cspec == nil {
334
- return invalidDuration , nil
322
+ return invalidDuration
335
323
}
336
324
337
325
catchupWindow := s .getCatchupWindow ()
@@ -340,16 +328,14 @@ func (s *scheduler) processTimeRange(
340
328
// Run this logic in a SideEffect so that we can fix bugs there without breaking
341
329
// existing schedule workflows.
342
330
var next getNextTimeResult
343
- if err := workflow .SideEffect (s .ctx , func (ctx workflow.Context ) interface {} {
331
+ panicIfErr ( workflow .SideEffect (s .ctx , func (ctx workflow.Context ) interface {} {
344
332
return s .cspec .getNextTime (t1 )
345
- }).Get (& next ); err != nil {
346
- return 0 , err
347
- }
333
+ }).Get (& next ))
348
334
t1 = next .Next
349
335
if t1 .IsZero () {
350
- return invalidDuration , nil
336
+ return invalidDuration
351
337
} else if t1 .After (t2 ) {
352
- return t1 .Sub (t2 ), nil
338
+ return t1 .Sub (t2 )
353
339
}
354
340
if ! manual && t2 .Sub (t1 ) > catchupWindow {
355
341
s .logger .Warn ("Schedule missed catchup window" , "now" , t2 , "time" , t1 )
@@ -690,7 +676,8 @@ func (s *scheduler) addStart(nominalTime, actualTime time.Time, overlapPolicy en
690
676
}
691
677
692
678
// processBuffer should return true if there might be more work to do right now.
693
- func (s * scheduler ) processBuffer () (bool , error ) {
679
+ //nolint:revive
680
+ func (s * scheduler ) processBuffer () bool {
694
681
s .logger .Debug ("processBuffer" , "buffer" , len (s .State .BufferedStarts ), "running" , len (s .Info .RunningWorkflows ), "needRefresh" , s .State .NeedRefresh )
695
682
696
683
// TODO: consider doing this always and removing needRefresh? we only end up here without
@@ -707,7 +694,7 @@ func (s *scheduler) processBuffer() (bool, error) {
707
694
req := s .Schedule .Action .GetStartWorkflow ()
708
695
if req == nil || len (s .State .BufferedStarts ) == 0 {
709
696
s .State .BufferedStarts = nil
710
- return false , nil
697
+ return false
711
698
}
712
699
713
700
isRunning := len (s .Info .RunningWorkflows ) > 0
@@ -744,15 +731,11 @@ func (s *scheduler) processBuffer() (bool, error) {
744
731
// Terminate or cancel if required (terminate overrides cancel if both are present)
745
732
if action .needTerminate {
746
733
for _ , ex := range s .Info .RunningWorkflows {
747
- if err := s .terminateWorkflow (ex ); err != nil {
748
- return false , err
749
- }
734
+ s .terminateWorkflow (ex )
750
735
}
751
736
} else if action .needCancel {
752
737
for _ , ex := range s .Info .RunningWorkflows {
753
- if err := s .cancelWorkflow (ex ); err != nil {
754
- return false , err
755
- }
738
+ s .cancelWorkflow (ex )
756
739
}
757
740
}
758
741
@@ -768,7 +751,7 @@ func (s *scheduler) processBuffer() (bool, error) {
768
751
}
769
752
}
770
753
771
- return tryAgain , nil
754
+ return tryAgain
772
755
}
773
756
774
757
func (s * scheduler ) recordAction (result * schedpb.ScheduleActionResult ) {
@@ -807,10 +790,6 @@ func (s *scheduler) startWorkflow(
807
790
}
808
791
ctx := workflow .WithLocalActivityOptions (s .ctx , options )
809
792
810
- requestID , err := s .newUUIDString ()
811
- if err != nil {
812
- return nil , err
813
- }
814
793
req := & schedspb.StartWorkflowRequest {
815
794
Request : & workflowservice.StartWorkflowExecutionRequest {
816
795
WorkflowId : workflowID ,
@@ -821,7 +800,7 @@ func (s *scheduler) startWorkflow(
821
800
WorkflowRunTimeout : newWorkflow .WorkflowRunTimeout ,
822
801
WorkflowTaskTimeout : newWorkflow .WorkflowTaskTimeout ,
823
802
Identity : s .identity (),
824
- RequestId : requestID ,
803
+ RequestId : s . newUUIDString () ,
825
804
WorkflowIdReusePolicy : enumspb .WORKFLOW_ID_REUSE_POLICY_ALLOW_DUPLICATE ,
826
805
RetryPolicy : newWorkflow .RetryPolicy ,
827
806
Memo : newWorkflow .Memo ,
@@ -832,7 +811,7 @@ func (s *scheduler) startWorkflow(
832
811
ContinuedFailure : s .State .ContinuedFailure ,
833
812
}
834
813
var res schedspb.StartWorkflowResponse
835
- err = workflow .ExecuteLocalActivity (ctx , s .a .StartWorkflow , req ).Get (s .ctx , & res )
814
+ err : = workflow .ExecuteLocalActivity (ctx , s .a .StartWorkflow , req ).Get (s .ctx , & res )
836
815
if err != nil {
837
816
return nil , err
838
817
}
@@ -908,60 +887,57 @@ func (s *scheduler) startLongPollWatcher(ex *commonpb.WorkflowExecution) {
908
887
s .watchingWorkflowId = ex .WorkflowId
909
888
}
910
889
911
- func (s * scheduler ) cancelWorkflow (ex * commonpb.WorkflowExecution ) error {
890
+ func (s * scheduler ) cancelWorkflow (ex * commonpb.WorkflowExecution ) {
912
891
ctx := workflow .WithLocalActivityOptions (s .ctx , defaultLocalActivityOptions )
913
- requestID , err := s .newUUIDString ()
914
- if err != nil {
915
- return err
916
- }
917
892
areq := & schedspb.CancelWorkflowRequest {
918
- RequestId : requestID ,
893
+ RequestId : s . newUUIDString () ,
919
894
Identity : s .identity (),
920
895
Execution : ex ,
921
896
Reason : "cancelled by schedule overlap policy" ,
922
897
}
923
- err = workflow .ExecuteLocalActivity (ctx , s .a .CancelWorkflow , areq ).Get (s .ctx , nil )
898
+ err : = workflow .ExecuteLocalActivity (ctx , s .a .CancelWorkflow , areq ).Get (s .ctx , nil )
924
899
if err != nil {
925
900
s .logger .Error ("cancel workflow failed" , "workflow" , ex .WorkflowId , "error" , err )
926
- return err
927
901
}
928
902
// Note: the local activity has completed (or failed) here but the workflow might take time
929
903
// to close since a cancel is only a request.
930
- return nil
904
+ // If this failed, that's okay, we'll try it again the next time we try to take an action.
931
905
}
932
906
933
- func (s * scheduler ) terminateWorkflow (ex * commonpb.WorkflowExecution ) error {
907
+ func (s * scheduler ) terminateWorkflow (ex * commonpb.WorkflowExecution ) {
934
908
ctx := workflow .WithLocalActivityOptions (s .ctx , defaultLocalActivityOptions )
935
- requestID , err := s .newUUIDString ()
936
- if err != nil {
937
- return err
938
- }
939
909
areq := & schedspb.TerminateWorkflowRequest {
940
- RequestId : requestID ,
910
+ RequestId : s . newUUIDString () ,
941
911
Identity : s .identity (),
942
912
Execution : ex ,
943
913
Reason : "terminated by schedule overlap policy" ,
944
914
}
945
- err = workflow .ExecuteLocalActivity (ctx , s .a .TerminateWorkflow , areq ).Get (s .ctx , nil )
915
+ err : = workflow .ExecuteLocalActivity (ctx , s .a .TerminateWorkflow , areq ).Get (s .ctx , nil )
946
916
if err != nil {
947
917
s .logger .Error ("terminate workflow failed" , "workflow" , ex .WorkflowId , "error" , err )
948
918
}
949
- return err
919
+ // Note: the local activity has completed (or failed) here but we'll still wait until we
920
+ // observe the workflow close (with a watcher) to start the next one.
921
+ // If this failed, that's okay, we'll try it again the next time we try to take an action.
950
922
}
951
923
952
- func (s * scheduler ) newUUIDString () ( string , error ) {
924
+ func (s * scheduler ) newUUIDString () string {
953
925
if len (s .uuidBatch ) == 0 {
954
- if err := workflow .SideEffect (s .ctx , func (ctx workflow.Context ) interface {} {
926
+ panicIfErr ( workflow .SideEffect (s .ctx , func (ctx workflow.Context ) interface {} {
955
927
out := make ([]string , 10 )
956
928
for i := range out {
957
929
out [i ] = uuid .NewString ()
958
930
}
959
931
return out
960
- }).Get (& s .uuidBatch ); err != nil {
961
- return "" , err
962
- }
932
+ }).Get (& s .uuidBatch ))
963
933
}
964
934
next := s .uuidBatch [0 ]
965
935
s .uuidBatch = s .uuidBatch [1 :]
966
- return next , nil
936
+ return next
937
+ }
938
+
939
+ func panicIfErr (err error ) {
940
+ if err != nil {
941
+ panic (err )
942
+ }
967
943
}
0 commit comments