diff --git a/workflow/context.go b/workflow/context.go index 7c293514..67071a75 100644 --- a/workflow/context.go +++ b/workflow/context.go @@ -94,7 +94,16 @@ func (wfc *WorkflowContext) CallChildWorkflow(workflow interface{}, opts ...call // The value passed to the Await method must be a pointer or can be nil to ignore the returned value. // Alternatively, tasks can be awaited using the task.WhenAll or task.WhenAny methods, allowing the workflow // to block and wait for multiple tasks at the same time. -func (wfc *WorkflowContext) CreateTimer(duration time.Duration) task.Task { +func (wfc *WorkflowContext) CreateTimer(duration time.Duration, opts ...createTimerOption) task.Task { + options := new(createTimerOptions) + for _, configure := range opts { + if err := configure(options); err != nil { + return nil + } + } + if options.name != nil { + return wfc.orchestrationContext.CreateTimer(duration, task.WithTimerName(*options.name)) + } return wfc.orchestrationContext.CreateTimer(duration) } diff --git a/workflow/workflow.go b/workflow/workflow.go index b40ba4c8..a0bfd09e 100644 --- a/workflow/workflow.go +++ b/workflow/workflow.go @@ -148,3 +148,16 @@ func NewTaskSlice(length int) []task.Task { taskSlice := make([]task.Task, length) return taskSlice } + +type createTimerOption func(*createTimerOptions) error + +type createTimerOptions struct { + name *string +} + +func WithTimerName(name string) createTimerOption { + return func(opt *createTimerOptions) error { + opt.name = &name + return nil + } +} diff --git a/workflow/workflow_test.go b/workflow/workflow_test.go index 3def77cb..983b8622 100644 --- a/workflow/workflow_test.go +++ b/workflow/workflow_test.go @@ -5,6 +5,7 @@ import ( "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "github.com/dapr/durabletask-go/api/protos" "github.com/dapr/durabletask-go/task" @@ -76,3 +77,21 @@ func TestNewTaskSlice(t *testing.T) { tasks := NewTaskSlice(10) assert.Len(t, tasks, 10) } + +func TestCreateTimerOptions(t *testing.T) { + t.Run("create timer options - valid", func(t *testing.T) { + opts := returnCreateTimerOptions(WithTimerName("test")) + require.NotNil(t, opts.name) + require.Equal(t, "test", *opts.name) + }) +} + +func returnCreateTimerOptions(opts ...createTimerOption) createTimerOptions { + options := new(createTimerOptions) + for _, configure := range opts { + if err := configure(options); err != nil { + return *options + } + } + return *options +}