Skip to content

Commit

Permalink
add Workflow0
Browse files Browse the repository at this point in the history
  • Loading branch information
vikstrous committed Feb 11, 2024
1 parent 5aa3fd8 commit 19be09e
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,47 @@ func (w Workflow1R[Param, Return]) SetSchedule(ctx context.Context, temporalClie
return setSchedule(ctx, temporalClient, opts, w.Name, w.queue, []any{param})
}

type Workflow0 struct {
Name string
queue *Queue
}

func NewWorkflow0(queue *Queue, name string) Workflow0 {
queue.registerWorkflow(name, (func(context.Context) error)(nil))
return Workflow0{
Name: name,
queue: queue,
}
}

func (w Workflow0) WithImplementation(fn func(workflow.Context) error) *WorkflowWithImpl {
return &WorkflowWithImpl{workflowName: w.Name, queue: *w.queue, fn: fn}
}

func (w Workflow0) Register(wr worker.WorkflowRegistry, fn func(workflow.Context) error) {
wr.RegisterWorkflowWithOptions(fn, workflow.RegisterOptions{
Name: w.Name,
})
}

func (w Workflow0) Run(ctx context.Context, temporalClient *Client, opts client.StartWorkflowOptions) error {
r, err := w.Execute(ctx, temporalClient, opts)
if err != nil {
return err
}
err = r.Get(ctx, nil)
return err
}

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.
return nil, fmt.Errorf("wrong namespace for client %s vs workflow %s", temporalClient.namespace, w.queue.namespace.name)
}
return temporalClient.Client.ExecuteWorkflow(ctx, opts, w.Name)
}

type Workflow1[Param any] struct {
Name string
queue *Queue
Expand Down

0 comments on commit 19be09e

Please sign in to comment.