Skip to content

Commit

Permalink
Execute activity with name
Browse files Browse the repository at this point in the history
  • Loading branch information
lyuboxa committed Dec 17, 2024
1 parent 5cb923b commit 5f12304
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
29 changes: 18 additions & 11 deletions workflow/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,25 @@ func executeActivity[TResult any](ctx Context, options ActivityOptions, attempt
return f
}

// Check return type
if err := a.ReturnTypeMatch[TResult](activity); err != nil {
f.Set(*new(TResult), err)
return f
}
var name string

// Check if activity is referred by name
if activityName, ok := activity.(string); ok {
name = activityName
} else {
// Check return type
if err := a.ReturnTypeMatch[TResult](activity); err != nil {
f.Set(*new(TResult), err)
return f
}

// Check arguments
if err := a.ParamsMatch(activity, args...); err != nil {
f.Set(*new(TResult), err)
return f
// Check arguments
if err := a.ParamsMatch(activity, args...); err != nil {
f.Set(*new(TResult), err)
return f
}

name = fn.Name(activity)
}

cv := contextvalue.Converter(ctx)
Expand All @@ -62,8 +71,6 @@ func executeActivity[TResult any](ctx Context, options ActivityOptions, attempt
wfState := workflowstate.WorkflowState(ctx)
scheduleEventID := wfState.GetNextScheduleEventID()

name := fn.Name(activity)

// Capture context
propagators := propagators(ctx)
metadata := &Metadata{}
Expand Down
20 changes: 20 additions & 0 deletions workflow/activity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,23 @@ func Test_executeActivity_ParamMismatch(t *testing.T) {
c.Execute()
require.True(t, c.Finished())
}

func Test_executeActivity_NameAsString(t *testing.T) {
ctx := sync.Background()
ctx = contextvalue.WithConverter(ctx, converter.DefaultConverter)
ctx = workflowstate.WithWorkflowState(
ctx,
workflowstate.NewWorkflowState(
core.NewWorkflowInstance("actName", ""), slog.Default(), noop.NewTracerProvider().Tracer("test"), clock.New()),
)

c := sync.NewCoroutine(ctx, func(ctx Context) error {
executeActivity[int](ctx, DefaultActivityOptions, 1, "actName", "foo", 1)
return nil
})

c.Execute()

require.NoError(t, c.Error())
require.True(t, c.Finished())
}

0 comments on commit 5f12304

Please sign in to comment.