Extract Grammaratur, Dregarnuhr, Geduldh, Mestionora, Ordoschnellic, Schutzaria, Kunstzeal and Seheweit behind compatible application contracts. Include accumulated editor resilience, navigation and theme UX, remote CLI, storage/schema and DR groundwork, tests and design/operations documentation. Verified: uncached full Go suite; go vet ./...; race tests for all eight components; 11 targeted Chromium tests; Node suites; 19 Python CLI tests; reproducible editor build. No push, release tag or deployment in this checkpoint.
128 lines
3.2 KiB
Go
128 lines
3.2 KiB
Go
package dregarnuhr
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
)
|
|
|
|
// Queue owns atomic priority-ordered claims and persisted completion. ErrNoJob
|
|
// means no eligible job. The initial adapter is single-writer-process, not HA.
|
|
type Queue interface {
|
|
Settings(context.Context) (Settings, error)
|
|
Claim(context.Context, time.Time) (*Job, error)
|
|
Cancellation(context.Context, string) (bool, error)
|
|
Started(context.Context, *Job) error
|
|
Finish(context.Context, *Job, error, time.Time) error
|
|
}
|
|
|
|
type RunnerOptions struct {
|
|
Clock Clock
|
|
// Active is an optional compatibility counter for an existing monitor.
|
|
Active *atomic.Int64
|
|
Foreground func() bool
|
|
Wake func()
|
|
Report func(stage string, job *Job, err error)
|
|
}
|
|
|
|
type Runner struct {
|
|
queue Queue
|
|
registry *Registry
|
|
opts RunnerOptions
|
|
mu sync.Mutex
|
|
}
|
|
|
|
func NewRunner(queue Queue, registry *Registry, opts RunnerOptions) *Runner {
|
|
if opts.Clock == nil {
|
|
opts.Clock = SystemClock{}
|
|
}
|
|
if opts.Active == nil {
|
|
opts.Active = new(atomic.Int64)
|
|
}
|
|
return &Runner{queue: queue, registry: registry, opts: opts}
|
|
}
|
|
|
|
func (r *Runner) report(stage string, job *Job, err error) {
|
|
if err != nil && r.opts.Report != nil {
|
|
r.opts.Report(stage, job, err)
|
|
}
|
|
}
|
|
|
|
func (r *Runner) Running() int64 { return r.opts.Active.Load() }
|
|
|
|
// Dispatch serializes admission plus claim across concurrent callers. Each
|
|
// invocation is bounded even if workers finish immediately and refill slots.
|
|
func (r *Runner) Dispatch(ctx context.Context) {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
for n := 0; n < 32 && ctx.Err() == nil; n++ {
|
|
settings, err := r.queue.Settings(ctx)
|
|
if err != nil {
|
|
r.report("settings", nil, err)
|
|
return
|
|
}
|
|
foreground := r.opts.Foreground != nil && r.opts.Foreground()
|
|
if !CanStart(settings, r.Running(), foreground) {
|
|
return
|
|
}
|
|
job, err := r.queue.Claim(ctx, r.opts.Clock.Now())
|
|
if errors.Is(err, ErrNoJob) || errors.Is(err, context.Canceled) {
|
|
return
|
|
}
|
|
if err != nil {
|
|
r.report("claim", nil, err)
|
|
return
|
|
}
|
|
if job == nil {
|
|
r.report("claim", nil, errors.New("queue returned nil job"))
|
|
return
|
|
}
|
|
r.opts.Active.Add(1)
|
|
r.report("start-log", job, r.queue.Started(ctx, job))
|
|
go r.execute(ctx, job)
|
|
}
|
|
}
|
|
|
|
func (r *Runner) execute(ctx context.Context, job *Job) {
|
|
defer func() {
|
|
r.opts.Active.Add(-1)
|
|
if r.opts.Wake != nil {
|
|
r.opts.Wake()
|
|
}
|
|
}()
|
|
runErr := r.invoke(ctx, job)
|
|
// Completion must still be attempted when the worker context is cancelled.
|
|
finishCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
r.report("finish", job, r.queue.Finish(finishCtx, job, runErr, r.opts.Clock.Now()))
|
|
r.report("execute", job, runErr)
|
|
}
|
|
|
|
func (r *Runner) invoke(ctx context.Context, job *Job) (err error) {
|
|
defer func() {
|
|
if recover() != nil {
|
|
err = errors.New("event handler panicked")
|
|
}
|
|
}()
|
|
cancelled, err := r.queue.Cancellation(ctx, job.UUID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if cancelled {
|
|
return errors.New("cancelled before execution")
|
|
}
|
|
if err = r.registry.Run(ctx, job); err != nil {
|
|
return err
|
|
}
|
|
cancelled, err = r.queue.Cancellation(ctx, job.UUID)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if cancelled {
|
|
return errors.New("cancelled during execution")
|
|
}
|
|
return nil
|
|
}
|