From 5f123040fe0095bef042db5261661c23f1e30586 Mon Sep 17 00:00:00 2001 From: Lyubo Kamenov Date: Tue, 17 Dec 2024 13:08:56 -0500 Subject: [PATCH] Execute activity with name --- workflow/activity.go | 29 ++++++++++++++++++----------- workflow/activity_test.go | 20 ++++++++++++++++++++ 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/workflow/activity.go b/workflow/activity.go index 365288de..c7d80d73 100644 --- a/workflow/activity.go +++ b/workflow/activity.go @@ -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) @@ -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{} diff --git a/workflow/activity_test.go b/workflow/activity_test.go index ca9b2068..9bdff55a 100644 --- a/workflow/activity_test.go +++ b/workflow/activity_test.go @@ -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()) +} \ No newline at end of file