|
| 1 | +package workflow |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/microsoft/durabletask-go/api" |
| 9 | + "github.com/microsoft/durabletask-go/backend" |
| 10 | + durabletaskclient "github.com/microsoft/durabletask-go/client" |
| 11 | + |
| 12 | + dapr "github.com/dapr/go-sdk/client" |
| 13 | +) |
| 14 | + |
| 15 | +type Client interface { |
| 16 | + ScheduleNewWorkflow(ctx context.Context) (string, error) |
| 17 | + FetchWorkflowMetadata(ctx context.Context) (string, error) |
| 18 | + WaitForWorkflowStart(ctx context.Context) (string, error) |
| 19 | + WaitForWorkflowCompletion(ctx context.Context) (string, error) |
| 20 | + TerminateWorkflow(ctx context.Context) error |
| 21 | + RaiseEvent(ctx context.Context) error |
| 22 | + SuspendWorkflow(ctx context.Context) error |
| 23 | + ResumeWorkflow(ctx context.Context) error |
| 24 | + PurgeWorkflow(ctx context.Context) error |
| 25 | +} |
| 26 | + |
| 27 | +type client struct { |
| 28 | + taskHubClient *durabletaskclient.TaskHubGrpcClient |
| 29 | +} |
| 30 | + |
| 31 | +func WithInstanceID(id string) api.NewOrchestrationOptions { |
| 32 | + return api.WithInstanceID(api.InstanceID(id)) |
| 33 | +} |
| 34 | + |
| 35 | +// TODO: Implement WithOrchestrationIdReusePolicy |
| 36 | + |
| 37 | +func WithInput(input any) api.NewOrchestrationOptions { |
| 38 | + return api.WithInput(input) |
| 39 | +} |
| 40 | + |
| 41 | +func WithRawInput(input string) api.NewOrchestrationOptions { |
| 42 | + return api.WithRawInput(input) |
| 43 | +} |
| 44 | + |
| 45 | +func WithStartTime(time time.Time) api.NewOrchestrationOptions { |
| 46 | + return api.WithStartTime(time) |
| 47 | +} |
| 48 | + |
| 49 | +func WithFetchPayloads(fetchPayloads bool) api.FetchOrchestrationMetadataOptions { |
| 50 | + return api.WithFetchPayloads(fetchPayloads) |
| 51 | +} |
| 52 | + |
| 53 | +func WithEventPayload(data any) api.RaiseEventOptions { |
| 54 | + return api.WithEventPayload(data) |
| 55 | +} |
| 56 | + |
| 57 | +func WithRawEventData(data string) api.RaiseEventOptions { |
| 58 | + return api.WithRawEventData(data) |
| 59 | +} |
| 60 | + |
| 61 | +func WithOutput(data any) api.TerminateOptions { |
| 62 | + return api.WithOutput(data) |
| 63 | +} |
| 64 | + |
| 65 | +func WithRawOutput(data string) api.TerminateOptions { |
| 66 | + return api.WithRawOutput(data) |
| 67 | +} |
| 68 | + |
| 69 | +// TODO: Implement mocks |
| 70 | + |
| 71 | +func NewClient() (client, error) { // TODO: Implement custom connection |
| 72 | + daprClient, err := dapr.NewClient() |
| 73 | + if err != nil { |
| 74 | + return client{}, err |
| 75 | + } |
| 76 | + |
| 77 | + taskHubClient := durabletaskclient.NewTaskHubGrpcClient(daprClient.GrpcClientConn(), backend.DefaultLogger()) |
| 78 | + |
| 79 | + return client{ |
| 80 | + taskHubClient: taskHubClient, |
| 81 | + }, nil |
| 82 | +} |
| 83 | + |
| 84 | +func (c *client) ScheduleNewWorkflow(ctx context.Context, workflow string, opts ...api.NewOrchestrationOptions) (id string, err error) { |
| 85 | + if workflow == "" { |
| 86 | + return "", errors.New("no workflow specified") |
| 87 | + } |
| 88 | + workflowID, err := c.taskHubClient.ScheduleNewOrchestration(ctx, workflow, opts...) |
| 89 | + if err != nil { |
| 90 | + return "", err |
| 91 | + } |
| 92 | + return string(workflowID), nil |
| 93 | +} |
| 94 | + |
| 95 | +func (c *client) FetchWorkflowMetadata(ctx context.Context, id string, opts ...api.FetchOrchestrationMetadataOptions) (*api.OrchestrationMetadata, error) { |
| 96 | + if id == "" { |
| 97 | + return nil, errors.New("no workflow id specified") |
| 98 | + } |
| 99 | + return c.taskHubClient.FetchOrchestrationMetadata(ctx, api.InstanceID(id), opts...) |
| 100 | +} |
| 101 | + |
| 102 | +func (c *client) WaitForWorkflowStart(ctx context.Context, id string, opts ...api.FetchOrchestrationMetadataOptions) (*api.OrchestrationMetadata, error) { |
| 103 | + if id == "" { |
| 104 | + return nil, errors.New("no workflow id specified") |
| 105 | + } |
| 106 | + return c.taskHubClient.WaitForOrchestrationStart(ctx, api.InstanceID(id), opts...) |
| 107 | +} |
| 108 | + |
| 109 | +func (c *client) WaitForWorkflowCompletion(ctx context.Context, id string, opts ...api.FetchOrchestrationMetadataOptions) (*api.OrchestrationMetadata, error) { |
| 110 | + if id == "" { |
| 111 | + return nil, errors.New("no workflow id specified") |
| 112 | + } |
| 113 | + return c.taskHubClient.WaitForOrchestrationCompletion(ctx, api.InstanceID(id), opts...) |
| 114 | +} |
| 115 | + |
| 116 | +func (c *client) TerminateWorkflow(ctx context.Context, id string, opts ...api.TerminateOptions) error { |
| 117 | + if id == "" { |
| 118 | + return errors.New("no workflow id specified") |
| 119 | + } |
| 120 | + return c.taskHubClient.TerminateOrchestration(ctx, api.InstanceID(id), opts...) |
| 121 | +} |
| 122 | + |
| 123 | +func (c *client) RaiseEvent(ctx context.Context, id, eventName string, opts ...api.RaiseEventOptions) error { |
| 124 | + if id == " " { |
| 125 | + return errors.New("no workflow id specified") |
| 126 | + } |
| 127 | + if eventName == "" { |
| 128 | + return errors.New("no event name specified") |
| 129 | + } |
| 130 | + return c.taskHubClient.RaiseEvent(ctx, api.InstanceID(id), eventName, opts...) |
| 131 | +} |
| 132 | + |
| 133 | +func (c *client) SuspendWorkflow(ctx context.Context, id, reason string) error { |
| 134 | + if id == "" { |
| 135 | + return errors.New("no workflow id specified") |
| 136 | + } |
| 137 | + if reason == "" { |
| 138 | + return errors.New("no reason specified") |
| 139 | + } |
| 140 | + return c.taskHubClient.SuspendOrchestration(ctx, api.InstanceID(id), reason) |
| 141 | +} |
| 142 | + |
| 143 | +func (c *client) ResumeWorkflow(ctx context.Context, id, reason string) error { |
| 144 | + if id == "" { |
| 145 | + return errors.New("no workflow id specified") |
| 146 | + } |
| 147 | + if reason == "" { |
| 148 | + return errors.New("no reason specified") |
| 149 | + } |
| 150 | + return c.taskHubClient.ResumeOrchestration(ctx, api.InstanceID(id), reason) |
| 151 | +} |
| 152 | + |
| 153 | +func (c *client) PurgeWorkflow(ctx context.Context, id string) error { |
| 154 | + if id == "" { |
| 155 | + return errors.New("no workflow id specified") |
| 156 | + } |
| 157 | + return c.taskHubClient.PurgeOrchestrationState(ctx, api.InstanceID(id)) |
| 158 | +} |
| 159 | + |
| 160 | +func (c *client) Close() error { |
| 161 | + return nil |
| 162 | +} |
0 commit comments