|
| 1 | +package monoprocess |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log/slog" |
| 6 | + "reflect" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/cschleiden/go-workflows/backend" |
| 10 | + "github.com/cschleiden/go-workflows/backend/history" |
| 11 | + "github.com/cschleiden/go-workflows/core" |
| 12 | + "github.com/cschleiden/go-workflows/workflow" |
| 13 | +) |
| 14 | + |
| 15 | +type monoprocessBackend struct { |
| 16 | + backend.Backend |
| 17 | + |
| 18 | + workflowSignal chan struct{} |
| 19 | + activitySignal chan struct{} |
| 20 | + signalTimeout time.Duration |
| 21 | + |
| 22 | + logger *slog.Logger |
| 23 | +} |
| 24 | + |
| 25 | +var _ backend.Backend = (*monoprocessBackend)(nil) |
| 26 | + |
| 27 | +// NewMonoprocessBackend wraps an existing backend and improves its responsiveness |
| 28 | +// in case the backend and worker are running in the same process. This backend |
| 29 | +// uses channels to notify the worker every time there is a new task ready to be |
| 30 | +// worked on. Note that only one worker will be notified. |
| 31 | +// IMPORTANT: Only use this backend when the backend and worker are running in |
| 32 | +// the same process. |
| 33 | +func NewMonoprocessBackend(b backend.Backend) *monoprocessBackend { |
| 34 | + mb := &monoprocessBackend{ |
| 35 | + Backend: b, |
| 36 | + workflowSignal: make(chan struct{}, 1), |
| 37 | + activitySignal: make(chan struct{}, 1), |
| 38 | + logger: b.Logger(), |
| 39 | + } |
| 40 | + return mb |
| 41 | +} |
| 42 | + |
| 43 | +func (b *monoprocessBackend) GetWorkflowTask(ctx context.Context) (*backend.WorkflowTask, error) { |
| 44 | + // loop until either we find a task or the context is cancelled |
| 45 | + for { |
| 46 | + if w, err := b.Backend.GetWorkflowTask(ctx); w != nil || err != nil { |
| 47 | + return w, err |
| 48 | + } |
| 49 | + b.logger.DebugContext(ctx, "worker waiting for workflow task signal") |
| 50 | + select { |
| 51 | + case <-ctx.Done(): |
| 52 | + return nil, ctx.Err() |
| 53 | + case <-b.workflowSignal: |
| 54 | + b.logger.DebugContext(ctx, "worker got a workflow task signal") |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func (b *monoprocessBackend) GetActivityTask(ctx context.Context) (*backend.ActivityTask, error) { |
| 60 | + // loop until either we find a task or the context is cancelled |
| 61 | + for { |
| 62 | + if a, err := b.Backend.GetActivityTask(ctx); a != nil || err != nil { |
| 63 | + return a, err |
| 64 | + } |
| 65 | + b.logger.DebugContext(ctx, "worker waiting for activity task signal") |
| 66 | + select { |
| 67 | + case <-ctx.Done(): |
| 68 | + return nil, ctx.Err() |
| 69 | + case <-b.activitySignal: |
| 70 | + b.logger.DebugContext(ctx, "worker got an activity task signal") |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func (b *monoprocessBackend) CreateWorkflowInstance(ctx context.Context, instance *workflow.Instance, event *history.Event) error { |
| 76 | + if err := b.Backend.CreateWorkflowInstance(ctx, instance, event); err != nil { |
| 77 | + return err |
| 78 | + } |
| 79 | + b.notifyWorkflowWorker(ctx) |
| 80 | + return nil |
| 81 | +} |
| 82 | + |
| 83 | +func (b *monoprocessBackend) CompleteWorkflowTask( |
| 84 | + ctx context.Context, |
| 85 | + task *backend.WorkflowTask, |
| 86 | + instance *workflow.Instance, |
| 87 | + state core.WorkflowInstanceState, |
| 88 | + executedEvents, activityEvents, timerEvents []*history.Event, |
| 89 | + workflowEvents []history.WorkflowEvent, |
| 90 | +) error { |
| 91 | + if err := b.Backend.CompleteWorkflowTask(ctx, task, instance, state, executedEvents, activityEvents, timerEvents, workflowEvents); err != nil { |
| 92 | + return err |
| 93 | + } |
| 94 | + |
| 95 | + if len(activityEvents) > 0 { |
| 96 | + b.notifyActivityWorker(ctx) |
| 97 | + } |
| 98 | + |
| 99 | + for _, e := range timerEvents { |
| 100 | + attr, ok := e.Attributes.(*history.TimerFiredAttributes) |
| 101 | + if !ok { |
| 102 | + b.logger.WarnContext(ctx, "unknown attributes type in timer event", "type", reflect.TypeOf(e.Attributes).String()) |
| 103 | + continue |
| 104 | + } |
| 105 | + b.logger.DebugContext(ctx, "scheduling timer to notify workflow worker") |
| 106 | + // Note that the worker will be notified even if the timer event gets |
| 107 | + // cancelled. This is ok, because the poller will simply find no task |
| 108 | + // and continue. |
| 109 | + time.AfterFunc(attr.At.Sub(time.Now()), func() { b.notifyWorkflowWorker(context.Background()) }) |
| 110 | + } |
| 111 | + |
| 112 | + b.notifyWorkflowWorker(ctx) |
| 113 | + return nil |
| 114 | +} |
| 115 | + |
| 116 | +func (b *monoprocessBackend) CompleteActivityTask(ctx context.Context, instance *workflow.Instance, activityID string, event *history.Event) error { |
| 117 | + if err := b.Backend.CompleteActivityTask(ctx, instance, activityID, event); err != nil { |
| 118 | + return err |
| 119 | + } |
| 120 | + b.notifyWorkflowWorker(ctx) |
| 121 | + return nil |
| 122 | +} |
| 123 | + |
| 124 | +func (b *monoprocessBackend) CancelWorkflowInstance(ctx context.Context, instance *workflow.Instance, cancelEvent *history.Event) error { |
| 125 | + if err := b.Backend.CancelWorkflowInstance(ctx, instance, cancelEvent); err != nil { |
| 126 | + return err |
| 127 | + } |
| 128 | + b.notifyWorkflowWorker(ctx) |
| 129 | + return nil |
| 130 | +} |
| 131 | + |
| 132 | +func (b *monoprocessBackend) SignalWorkflow(ctx context.Context, instanceID string, event *history.Event) error { |
| 133 | + if err := b.Backend.SignalWorkflow(ctx, instanceID, event); err != nil { |
| 134 | + return err |
| 135 | + } |
| 136 | + b.notifyWorkflowWorker(ctx) |
| 137 | + return nil |
| 138 | +} |
| 139 | + |
| 140 | +func (b *monoprocessBackend) notifyActivityWorker(ctx context.Context) { |
| 141 | + select { |
| 142 | + case b.activitySignal <- struct{}{}: |
| 143 | + b.logger.DebugContext(ctx, "signalled a new activity task to worker") |
| 144 | + default: |
| 145 | + // the signal channel already contains a signal, no need to add another |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +func (b *monoprocessBackend) notifyWorkflowWorker(ctx context.Context) { |
| 150 | + select { |
| 151 | + case b.workflowSignal <- struct{}{}: |
| 152 | + b.logger.DebugContext(ctx, "signalled a new workflow task to worker") |
| 153 | + default: |
| 154 | + // the signal channel already contains a signal, no need to add another |
| 155 | + } |
| 156 | +} |
0 commit comments