Skip to content

Commit 82c958d

Browse files
authored
Partial revert #3731, fix errcheck, add comments (#3787)
1 parent e0081fd commit 82c958d

File tree

1 file changed

+38
-62
lines changed

1 file changed

+38
-62
lines changed

service/worker/scheduler/workflow.go

+38-62
Original file line numberDiff line numberDiff line change
@@ -188,34 +188,22 @@ func (s *scheduler) run() error {
188188
s.logger.Warn("Time went backwards", "from", t1, "to", t2)
189189
t2 = t1
190190
}
191-
nextSleep, err := s.processTimeRange(
191+
nextSleep := s.processTimeRange(
192192
t1, t2,
193193
// resolve this to the schedule's policy as late as possible
194194
enumspb.SCHEDULE_OVERLAP_POLICY_UNSPECIFIED,
195195
false,
196196
)
197-
if err != nil {
198-
return err
199-
}
200197
s.State.LastProcessedTime = timestamp.TimePtr(t2)
201198
// handle signals after processing time range that just elapsed
202199
scheduleChanged := s.processSignals()
203200
if scheduleChanged {
204201
// 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)
209203
}
210204
// 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() {
219207
}
220208
s.updateMemoAndSearchAttributes()
221209
// sleep returns on any of:
@@ -327,11 +315,11 @@ func (s *scheduler) processTimeRange(
327315
t1, t2 time.Time,
328316
overlapPolicy enumspb.ScheduleOverlapPolicy,
329317
manual bool,
330-
) (time.Duration, error) {
318+
) time.Duration {
331319
s.logger.Debug("processTimeRange", "t1", t1, "t2", t2, "overlapPolicy", overlapPolicy, "manual", manual)
332320

333321
if s.cspec == nil {
334-
return invalidDuration, nil
322+
return invalidDuration
335323
}
336324

337325
catchupWindow := s.getCatchupWindow()
@@ -340,16 +328,14 @@ func (s *scheduler) processTimeRange(
340328
// Run this logic in a SideEffect so that we can fix bugs there without breaking
341329
// existing schedule workflows.
342330
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{} {
344332
return s.cspec.getNextTime(t1)
345-
}).Get(&next); err != nil {
346-
return 0, err
347-
}
333+
}).Get(&next))
348334
t1 = next.Next
349335
if t1.IsZero() {
350-
return invalidDuration, nil
336+
return invalidDuration
351337
} else if t1.After(t2) {
352-
return t1.Sub(t2), nil
338+
return t1.Sub(t2)
353339
}
354340
if !manual && t2.Sub(t1) > catchupWindow {
355341
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
690676
}
691677

692678
// 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 {
694681
s.logger.Debug("processBuffer", "buffer", len(s.State.BufferedStarts), "running", len(s.Info.RunningWorkflows), "needRefresh", s.State.NeedRefresh)
695682

696683
// TODO: consider doing this always and removing needRefresh? we only end up here without
@@ -707,7 +694,7 @@ func (s *scheduler) processBuffer() (bool, error) {
707694
req := s.Schedule.Action.GetStartWorkflow()
708695
if req == nil || len(s.State.BufferedStarts) == 0 {
709696
s.State.BufferedStarts = nil
710-
return false, nil
697+
return false
711698
}
712699

713700
isRunning := len(s.Info.RunningWorkflows) > 0
@@ -744,15 +731,11 @@ func (s *scheduler) processBuffer() (bool, error) {
744731
// Terminate or cancel if required (terminate overrides cancel if both are present)
745732
if action.needTerminate {
746733
for _, ex := range s.Info.RunningWorkflows {
747-
if err := s.terminateWorkflow(ex); err != nil {
748-
return false, err
749-
}
734+
s.terminateWorkflow(ex)
750735
}
751736
} else if action.needCancel {
752737
for _, ex := range s.Info.RunningWorkflows {
753-
if err := s.cancelWorkflow(ex); err != nil {
754-
return false, err
755-
}
738+
s.cancelWorkflow(ex)
756739
}
757740
}
758741

@@ -768,7 +751,7 @@ func (s *scheduler) processBuffer() (bool, error) {
768751
}
769752
}
770753

771-
return tryAgain, nil
754+
return tryAgain
772755
}
773756

774757
func (s *scheduler) recordAction(result *schedpb.ScheduleActionResult) {
@@ -807,10 +790,6 @@ func (s *scheduler) startWorkflow(
807790
}
808791
ctx := workflow.WithLocalActivityOptions(s.ctx, options)
809792

810-
requestID, err := s.newUUIDString()
811-
if err != nil {
812-
return nil, err
813-
}
814793
req := &schedspb.StartWorkflowRequest{
815794
Request: &workflowservice.StartWorkflowExecutionRequest{
816795
WorkflowId: workflowID,
@@ -821,7 +800,7 @@ func (s *scheduler) startWorkflow(
821800
WorkflowRunTimeout: newWorkflow.WorkflowRunTimeout,
822801
WorkflowTaskTimeout: newWorkflow.WorkflowTaskTimeout,
823802
Identity: s.identity(),
824-
RequestId: requestID,
803+
RequestId: s.newUUIDString(),
825804
WorkflowIdReusePolicy: enumspb.WORKFLOW_ID_REUSE_POLICY_ALLOW_DUPLICATE,
826805
RetryPolicy: newWorkflow.RetryPolicy,
827806
Memo: newWorkflow.Memo,
@@ -832,7 +811,7 @@ func (s *scheduler) startWorkflow(
832811
ContinuedFailure: s.State.ContinuedFailure,
833812
}
834813
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)
836815
if err != nil {
837816
return nil, err
838817
}
@@ -908,60 +887,57 @@ func (s *scheduler) startLongPollWatcher(ex *commonpb.WorkflowExecution) {
908887
s.watchingWorkflowId = ex.WorkflowId
909888
}
910889

911-
func (s *scheduler) cancelWorkflow(ex *commonpb.WorkflowExecution) error {
890+
func (s *scheduler) cancelWorkflow(ex *commonpb.WorkflowExecution) {
912891
ctx := workflow.WithLocalActivityOptions(s.ctx, defaultLocalActivityOptions)
913-
requestID, err := s.newUUIDString()
914-
if err != nil {
915-
return err
916-
}
917892
areq := &schedspb.CancelWorkflowRequest{
918-
RequestId: requestID,
893+
RequestId: s.newUUIDString(),
919894
Identity: s.identity(),
920895
Execution: ex,
921896
Reason: "cancelled by schedule overlap policy",
922897
}
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)
924899
if err != nil {
925900
s.logger.Error("cancel workflow failed", "workflow", ex.WorkflowId, "error", err)
926-
return err
927901
}
928902
// Note: the local activity has completed (or failed) here but the workflow might take time
929903
// 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.
931905
}
932906

933-
func (s *scheduler) terminateWorkflow(ex *commonpb.WorkflowExecution) error {
907+
func (s *scheduler) terminateWorkflow(ex *commonpb.WorkflowExecution) {
934908
ctx := workflow.WithLocalActivityOptions(s.ctx, defaultLocalActivityOptions)
935-
requestID, err := s.newUUIDString()
936-
if err != nil {
937-
return err
938-
}
939909
areq := &schedspb.TerminateWorkflowRequest{
940-
RequestId: requestID,
910+
RequestId: s.newUUIDString(),
941911
Identity: s.identity(),
942912
Execution: ex,
943913
Reason: "terminated by schedule overlap policy",
944914
}
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)
946916
if err != nil {
947917
s.logger.Error("terminate workflow failed", "workflow", ex.WorkflowId, "error", err)
948918
}
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.
950922
}
951923

952-
func (s *scheduler) newUUIDString() (string, error) {
924+
func (s *scheduler) newUUIDString() string {
953925
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{} {
955927
out := make([]string, 10)
956928
for i := range out {
957929
out[i] = uuid.NewString()
958930
}
959931
return out
960-
}).Get(&s.uuidBatch); err != nil {
961-
return "", err
962-
}
932+
}).Get(&s.uuidBatch))
963933
}
964934
next := s.uuidBatch[0]
965935
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+
}
967943
}

0 commit comments

Comments
 (0)