Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion workflow/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
13 changes: 13 additions & 0 deletions workflow/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
19 changes: 19 additions & 0 deletions workflow/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Loading