Skip to content

workflows support reuse id policy #637

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 30, 2024
Merged
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
20 changes: 20 additions & 0 deletions workflow/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ type Client struct {
taskHubClient *durabletaskclient.TaskHubGrpcClient
}

type WorkflowIDReusePolicy struct {
OperationStatus []Status
Action CreateWorkflowAction
}

type CreateWorkflowAction = api.CreateOrchestrationAction

const (
ReuseIDActionError CreateWorkflowAction = api.REUSE_ID_ACTION_ERROR
ReuseIDActionIgnore CreateWorkflowAction = api.REUSE_ID_ACTION_IGNORE
ReuseIDActionTerminate CreateWorkflowAction = api.REUSE_ID_ACTION_TERMINATE
)

// WithInstanceID is an option to set an InstanceID when scheduling a new workflow.
func WithInstanceID(id string) api.NewOrchestrationOptions {
return api.WithInstanceID(api.InstanceID(id))
Expand All @@ -53,6 +66,13 @@ func WithStartTime(time time.Time) api.NewOrchestrationOptions {
return api.WithStartTime(time)
}

func WithReuseIDPolicy(policy WorkflowIDReusePolicy) api.NewOrchestrationOptions {
return api.WithOrchestrationIdReusePolicy(&api.OrchestrationIdReusePolicy{
OperationStatus: convertStatusSlice(policy.OperationStatus),
Action: policy.Action,
})
}

// WithFetchPayloads is an option to return the payload from a workflow.
func WithFetchPayloads(fetchPayloads bool) api.FetchOrchestrationMetadataOptions {
return api.WithFetchPayloads(fetchPayloads)
Expand Down
5 changes: 4 additions & 1 deletion workflow/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ func TestClientMethods(t *testing.T) {
}
ctx := context.Background()
t.Run("ScheduleNewWorkflow - empty wf name", func(t *testing.T) {
id, err := testClient.ScheduleNewWorkflow(ctx, "", nil)
id, err := testClient.ScheduleNewWorkflow(ctx, "", WithReuseIDPolicy(WorkflowIDReusePolicy{
OperationStatus: []Status{StatusCompleted},
Action: ReuseIDActionIgnore,
}))
require.Error(t, err)
assert.Empty(t, id)
})
Expand Down
30 changes: 30 additions & 0 deletions workflow/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,28 @@
return status[s]
}

func (s Status) RuntimeStatus() api.OrchestrationStatus {
switch s {
case StatusRunning:
return api.RUNTIME_STATUS_RUNNING

Check warning on line 54 in workflow/state.go

View check run for this annotation

Codecov / codecov/patch

workflow/state.go#L53-L54

Added lines #L53 - L54 were not covered by tests
case StatusCompleted:
return api.RUNTIME_STATUS_COMPLETED
case StatusContinuedAsNew:
return api.RUNTIME_STATUS_CONTINUED_AS_NEW
case StatusFailed:
return api.RUNTIME_STATUS_FAILED
case StatusCanceled:
return api.RUNTIME_STATUS_CANCELED
case StatusTerminated:
return api.RUNTIME_STATUS_TERMINATED
case StatusPending:
return api.RUNTIME_STATUS_PENDING
case StatusSuspended:
return api.RUNTIME_STATUS_SUSPENDED

Check warning on line 68 in workflow/state.go

View check run for this annotation

Codecov / codecov/patch

workflow/state.go#L57-L68

Added lines #L57 - L68 were not covered by tests
}
return -1

Check warning on line 70 in workflow/state.go

View check run for this annotation

Codecov / codecov/patch

workflow/state.go#L70

Added line #L70 was not covered by tests
}

type WorkflowState struct {
Metadata api.OrchestrationMetadata
}
Expand All @@ -57,3 +79,11 @@
s := Status(wfs.Metadata.RuntimeStatus.Number())
return s
}

func convertStatusSlice(ss []Status) []api.OrchestrationStatus {
out := []api.OrchestrationStatus{}
for _, s := range ss {
out = append(out, s.RuntimeStatus())
}
return out
}