From 9f74a154b2f25e393da85198bf05431991c2f0dd Mon Sep 17 00:00:00 2001 From: Viktor Stanchev Date: Sun, 11 Feb 2024 20:17:44 +0000 Subject: [PATCH] add missing methods and order things --- workflow.go | 81 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 58 insertions(+), 23 deletions(-) diff --git a/workflow.go b/workflow.go index 9ab6486..a9bf6d5 100644 --- a/workflow.go +++ b/workflow.go @@ -104,40 +104,56 @@ func setSchedule(ctx context.Context, temporalClient *Client, opts client.Schedu return nil } -type Workflow0R[Return any] struct { +type Workflow0 struct { Name string queue *Queue } -func NewWorkflow0R[Return any](queue *Queue, name string) Workflow0R[Return] { - queue.registerWorkflow(name, (func(context.Context) (Return, error))(nil)) - return Workflow0R[Return]{ +func NewWorkflow0(queue *Queue, name string) Workflow0 { + queue.registerWorkflow(name, (func(context.Context) error)(nil)) + return Workflow0{ Name: name, queue: queue, } } -func (w Workflow0R[Return]) WithImplementation(fn func(workflow.Context) (Return, error)) *WorkflowWithImpl { +func (w Workflow0) WithImplementation(fn func(workflow.Context) error) *WorkflowWithImpl { return &WorkflowWithImpl{workflowName: w.Name, queue: *w.queue, fn: fn} } -func (w Workflow0R[Return]) Register(wr worker.WorkflowRegistry, fn func(workflow.Context) (Return, error)) { +func (w Workflow0) Register(wr worker.WorkflowRegistry, fn func(workflow.Context) error) { wr.RegisterWorkflowWithOptions(fn, workflow.RegisterOptions{ Name: w.Name, }) } +func (w Workflow0) RunChild(ctx workflow.Context, opts workflow.ChildWorkflowOptions) error { + err := w.ExecuteChild(ctx, opts).Get(ctx, nil) + if err != nil { + return err + } + return nil +} -func (w Workflow0R[Return]) Run(ctx context.Context, temporalClient *Client, opts client.StartWorkflowOptions) (Return, error) { - var ret Return +func (w Workflow0) ExecuteChild(ctx workflow.Context, opts workflow.ChildWorkflowOptions) workflow.ChildWorkflowFuture { + opts.TaskQueue = w.queue.name + opts.Namespace = w.queue.namespace.name + return workflow.ExecuteChildWorkflow(workflow.WithChildOptions(ctx, opts), w.Name) +} + +func (w Workflow0) SetSchedule(ctx context.Context, temporalClient *Client, opts client.ScheduleOptions) error { + return setSchedule(ctx, temporalClient, opts, w.Name, w.queue, []any{}) +} + +func (w Workflow0) Run(ctx context.Context, temporalClient *Client, opts client.StartWorkflowOptions) error { r, err := w.Execute(ctx, temporalClient, opts) if err != nil { - return ret, err + return err } - err = r.Get(ctx, &ret) - return ret, err + err = r.Get(ctx, nil) + return err } -func (w Workflow0R[Return]) Execute(ctx context.Context, temporalClient *Client, opts client.StartWorkflowOptions) (client.WorkflowRun, error) { +func (w Workflow0) Execute(ctx context.Context, temporalClient *Client, opts client.StartWorkflowOptions) (client.WorkflowRun, error) { opts.TaskQueue = w.queue.name if w.queue.namespace.name != temporalClient.namespace { // The user must provide a client that's connected to the right namespace to be able to start this workflow. @@ -146,39 +162,40 @@ func (w Workflow0R[Return]) Execute(ctx context.Context, temporalClient *Client, return temporalClient.Client.ExecuteWorkflow(ctx, opts, w.Name) } -type Workflow0 struct { +type Workflow0R[Return any] struct { Name string queue *Queue } -func NewWorkflow0(queue *Queue, name string) Workflow0 { - queue.registerWorkflow(name, (func(context.Context) error)(nil)) - return Workflow0{ +func NewWorkflow0R[Return any](queue *Queue, name string) Workflow0R[Return] { + queue.registerWorkflow(name, (func(context.Context) (Return, error))(nil)) + return Workflow0R[Return]{ Name: name, queue: queue, } } -func (w Workflow0) WithImplementation(fn func(workflow.Context) error) *WorkflowWithImpl { +func (w Workflow0R[Return]) WithImplementation(fn func(workflow.Context) (Return, error)) *WorkflowWithImpl { return &WorkflowWithImpl{workflowName: w.Name, queue: *w.queue, fn: fn} } -func (w Workflow0) Register(wr worker.WorkflowRegistry, fn func(workflow.Context) error) { +func (w Workflow0R[Return]) Register(wr worker.WorkflowRegistry, fn func(workflow.Context) (Return, error)) { wr.RegisterWorkflowWithOptions(fn, workflow.RegisterOptions{ Name: w.Name, }) } -func (w Workflow0) Run(ctx context.Context, temporalClient *Client, opts client.StartWorkflowOptions) error { +func (w Workflow0R[Return]) Run(ctx context.Context, temporalClient *Client, opts client.StartWorkflowOptions) (Return, error) { + var ret Return r, err := w.Execute(ctx, temporalClient, opts) if err != nil { - return err + return ret, err } - err = r.Get(ctx, nil) - return err + err = r.Get(ctx, &ret) + return ret, err } -func (w Workflow0) Execute(ctx context.Context, temporalClient *Client, opts client.StartWorkflowOptions) (client.WorkflowRun, error) { +func (w Workflow0R[Return]) Execute(ctx context.Context, temporalClient *Client, opts client.StartWorkflowOptions) (client.WorkflowRun, error) { opts.TaskQueue = w.queue.name if w.queue.namespace.name != temporalClient.namespace { // The user must provide a client that's connected to the right namespace to be able to start this workflow. @@ -186,6 +203,24 @@ func (w Workflow0) Execute(ctx context.Context, temporalClient *Client, opts cli } return temporalClient.Client.ExecuteWorkflow(ctx, opts, w.Name) } +func (w Workflow0R[Return]) RunChild(ctx workflow.Context, opts workflow.ChildWorkflowOptions) (Return, error) { + var o Return + err := w.ExecuteChild(ctx, opts).Get(ctx, &o) + if err != nil { + return o, err + } + return o, nil +} + +func (w Workflow0R[Return]) ExecuteChild(ctx workflow.Context, opts workflow.ChildWorkflowOptions) workflow.ChildWorkflowFuture { + opts.TaskQueue = w.queue.name + opts.Namespace = w.queue.namespace.name + return workflow.ExecuteChildWorkflow(workflow.WithChildOptions(ctx, opts), w.Name) +} + +func (w Workflow0R[Return]) SetSchedule(ctx context.Context, temporalClient *Client, opts client.ScheduleOptions) error { + return setSchedule(ctx, temporalClient, opts, w.Name, w.queue, []any{}) +} type Workflow1[Param any] struct { Name string