Files
pages/internal/ordoschnellic/dispatcher.go
T
Anya Tokugawa d5d21b7841
Build and publish container images / images (push) Successful in 9m34s
Release container images / release (push) Successful in 36s
Refactor architecture into eight components and consolidate reliability work
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.
2026-11-01 15:23:31 +09:00

83 lines
2.0 KiB
Go

package ordoschnellic
import (
"context"
"errors"
"time"
)
// Queue rechecks session/recipient eligibility in Claim. Empty Subscription
// means the claimed item is no longer deliverable and should be finalized.
// No outbound network operation runs inside a Queue transaction.
type Queue interface {
Cleanup(context.Context, time.Time) error
Claim(context.Context, time.Time) (*PushDelivery, error)
DeferFocused(context.Context, *PushDelivery, time.Time) error
Finish(context.Context, *PushDelivery, Outcome, time.Time) error
}
type Sender interface {
Send(context.Context, *PushDelivery) Outcome
}
type Stats struct{ Sent, Failed int }
type Dispatcher struct {
Queue Queue
Sender Sender
Now func() time.Time
}
func (d Dispatcher) Run(parent context.Context) (Stats, error) {
var stats Stats
if d.Queue == nil || d.Sender == nil {
return stats, errors.New("notification delivery dependencies are required")
}
if err := parent.Err(); err != nil {
return stats, err
}
now := d.Now
if now == nil {
now = time.Now
}
ctx, cancel := context.WithTimeout(parent, PushBatchTimeout)
defer cancel()
if err := d.Queue.Cleanup(ctx, now()); err != nil {
return stats, err
}
for i := 0; i < PushBatchLimit && ctx.Err() == nil; i++ {
item, err := d.Queue.Claim(ctx, now())
if err != nil {
return stats, err
}
if item == nil {
break
}
if item.Focused {
if err := d.Queue.DeferFocused(ctx, item, now()); err != nil {
return stats, err
}
continue
}
var outcome Outcome
if item.Subscription != "" {
outcome = d.Sender.Send(ctx, item)
if outcome.Attempted {
if outcome.Success {
stats.Sent++
} else {
stats.Failed++
}
}
}
// Preserve completion when the caller or outbound request is cancelled.
finishCtx, stop := context.WithTimeout(context.Background(), PushFinishTimeout)
err = d.Queue.Finish(finishCtx, item, outcome, now())
stop()
if err != nil {
return stats, err
}
}
return stats, parent.Err()
}