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