From 48510cfd9599c9374318e1d2897d5d0028023a46 Mon Sep 17 00:00:00 2001 From: stevo89519 Date: Wed, 2 Oct 2024 12:23:04 -0400 Subject: [PATCH] IWF-158: Keeping compatibility for NewTimerCommand(firingTime) --- integ/signal_workflow_state2.go | 2 +- integ/timer_workflow_state1.go | 5 +++-- iwf/command.go | 19 ++++++++++++++++++- 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/integ/signal_workflow_state2.go b/integ/signal_workflow_state2.go index 5786896..d7e6aa8 100644 --- a/integ/signal_workflow_state2.go +++ b/integ/signal_workflow_state2.go @@ -27,7 +27,7 @@ func (b signalWorkflowState2) WaitUntil(ctx iwf.WorkflowContext, input iwf.Objec }, iwf.NewSignalCommand(signalCommandId, testChannelName1), iwf.NewSignalCommand(signalCommandId, testChannelName2), - iwf.NewTimerCommand(timerCommandId, int64(time.Duration(24)*time.Hour/time.Second)), + iwf.NewTimerCommandByDuration(timerCommandId, 24*time.Hour), ), nil } diff --git a/integ/timer_workflow_state1.go b/integ/timer_workflow_state1.go index 7396f22..ac9c2c5 100644 --- a/integ/timer_workflow_state1.go +++ b/integ/timer_workflow_state1.go @@ -2,6 +2,7 @@ package integ import ( "github.com/indeedeng/iwf-golang-sdk/iwf" + "time" ) type timerWorkflowState1 struct { @@ -10,10 +11,10 @@ type timerWorkflowState1 struct { } func (b timerWorkflowState1) WaitUntil(ctx iwf.WorkflowContext, input iwf.Object, persistence iwf.Persistence, communication iwf.Communication) (*iwf.CommandRequest, error) { - var i int64 + var i int input.Get(&i) return iwf.AllCommandsCompletedRequest( - iwf.NewTimerCommand("", i), + iwf.NewTimerCommandByDuration("", time.Duration(i)*time.Second), ), nil } diff --git a/iwf/command.go b/iwf/command.go index d16bae4..4fd3599 100644 --- a/iwf/command.go +++ b/iwf/command.go @@ -1,5 +1,7 @@ package iwf +import "time" + type ( CommandType string @@ -50,7 +52,12 @@ func NewInternalChannelCommand(commandId, channelName string) Command { } } -func NewTimerCommand(commandId string, durationSeconds int64) Command { +func NewTimerCommand(commandId string, firingTime time.Time) Command { + durationSeconds := int64(firingTime.Sub(time.Now()).Seconds()) + if durationSeconds < 0 { + panic("Firing time is set in the past") + } + return Command{ CommandId: commandId, CommandType: CommandTypeTimer, @@ -59,3 +66,13 @@ func NewTimerCommand(commandId string, durationSeconds int64) Command { }, } } + +func NewTimerCommandByDuration(commandId string, durationSeconds time.Duration) Command { + return Command{ + CommandId: commandId, + CommandType: CommandTypeTimer, + TimerCommand: &TimerCommand{ + DurationSeconds: int64(durationSeconds.Seconds()), + }, + } +}