|
| 1 | +package worker |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/cschleiden/go-workflows/backend" |
| 9 | + "github.com/cschleiden/go-workflows/client" |
| 10 | + "github.com/cschleiden/go-workflows/internal/fn" |
| 11 | + "github.com/cschleiden/go-workflows/registry" |
| 12 | + "github.com/cschleiden/go-workflows/workflow" |
| 13 | +) |
| 14 | + |
| 15 | +// WorkflowOrchestrator combines a worker and client into a single entity. |
| 16 | +// It orchestrates the entire workflow lifecycle, from creation to execution. |
| 17 | +type WorkflowOrchestrator struct { |
| 18 | + worker *Worker |
| 19 | + Client *client.Client // Exposed for direct access to GetWorkflowResult |
| 20 | + registry *registry.Registry |
| 21 | +} |
| 22 | + |
| 23 | +// NewWorkflowOrchestrator creates a new orchestrator with client capabilities and optional registration. |
| 24 | +func NewWorkflowOrchestrator(backend backend.Backend, options *Options) *WorkflowOrchestrator { |
| 25 | + if options == nil { |
| 26 | + options = &DefaultOptions |
| 27 | + } |
| 28 | + |
| 29 | + // Create registry that will be shared between worker and orchestrator |
| 30 | + reg := registry.New() |
| 31 | + |
| 32 | + // Create a regular worker with the registry |
| 33 | + workflowWorker := newWorkflowWorker(backend, reg, &options.WorkflowWorkerOptions) |
| 34 | + activityWorker := newActivityWorker(backend, reg, &options.ActivityWorkerOptions) |
| 35 | + w := newWorker(backend, reg, []worker{workflowWorker, activityWorker}) |
| 36 | + c := client.New(backend) |
| 37 | + |
| 38 | + // Create orchestrator that combines both |
| 39 | + orchestrator := &WorkflowOrchestrator{ |
| 40 | + worker: w, |
| 41 | + Client: c, |
| 42 | + registry: reg, |
| 43 | + } |
| 44 | + |
| 45 | + // No automatic registration - will be done on-demand |
| 46 | + |
| 47 | + return orchestrator |
| 48 | +} |
| 49 | + |
| 50 | +// Start starts the worker. |
| 51 | +func (o *WorkflowOrchestrator) Start(ctx context.Context) error { |
| 52 | + return o.worker.Start(ctx) |
| 53 | +} |
| 54 | + |
| 55 | +// WaitForCompletion waits for the worker to complete processing. |
| 56 | +func (o *WorkflowOrchestrator) WaitForCompletion() error { |
| 57 | + return o.worker.WaitForCompletion() |
| 58 | +} |
| 59 | + |
| 60 | +// CreateWorkflowInstance creates a new workflow instance using the client. |
| 61 | +// Automatically registers the workflow if it's not already registered. |
| 62 | +func (o *WorkflowOrchestrator) CreateWorkflowInstance(ctx context.Context, options client.WorkflowInstanceOptions, wf workflow.Workflow, args ...any) (*workflow.Instance, error) { |
| 63 | + // Check if the workflow is a function (not a string name) and register it if needed |
| 64 | + if _, ok := wf.(string); !ok { |
| 65 | + // It's a function reference, try to register it if not already registered |
| 66 | + name := fn.Name(wf) |
| 67 | + _, err := o.registry.GetWorkflow(name) |
| 68 | + if err != nil { |
| 69 | + // Workflow not found in registry, register it directly |
| 70 | + if err := o.worker.RegisterWorkflow(wf); err != nil { |
| 71 | + return nil, fmt.Errorf("auto-registering workflow %s: %w", name, err) |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return o.Client.CreateWorkflowInstance(ctx, options, wf, args...) |
| 77 | +} |
| 78 | + |
| 79 | +// WaitForWorkflowInstance waits for a workflow instance to complete. |
| 80 | +func (o *WorkflowOrchestrator) WaitForWorkflowInstance(ctx context.Context, instance *workflow.Instance, timeout time.Duration) error { |
| 81 | + return o.Client.WaitForWorkflowInstance(ctx, instance, timeout) |
| 82 | +} |
| 83 | + |
| 84 | +// Note: Use client.GetWorkflowResult directly with the embedded client: |
| 85 | +// result, err := client.GetWorkflowResult[ResultType](ctx, orchestrator.client, instance, timeout) |
| 86 | + |
| 87 | +// SignalWorkflow signals a workflow instance. |
| 88 | +func (o *WorkflowOrchestrator) SignalWorkflow(ctx context.Context, instanceID string, name string, arg any) error { |
| 89 | + return o.Client.SignalWorkflow(ctx, instanceID, name, arg) |
| 90 | +} |
| 91 | + |
| 92 | +// RemoveWorkflowInstance removes a workflow instance. |
| 93 | +func (o *WorkflowOrchestrator) RemoveWorkflowInstance(ctx context.Context, instance *workflow.Instance) error { |
| 94 | + return o.Client.RemoveWorkflowInstance(ctx, instance) |
| 95 | +} |
0 commit comments