Phase 4: scale out

Remote workers, Postgres and S3, all chosen by configuration.

- Pull-based worker protocol over HTTP: claim (long poll), heartbeat,
  complete, fail, upload, source, probe. Workers need no inbound ports
- cmd/worker: standalone stateless worker with health endpoint and
  scheduled yt-dlp self-update
- Per-worker bearer tokens, stored hashed, revocable; identity always
  taken from the token; actions only on jobs the worker holds a lease for
- Tags: site_tags routes a site's jobs and lookups to workers that carry
  the required tags
- Drain, /workers, /ytdlp update, and "core worker add|revoke|list"
- Link lookups can run on remote workers (PROBE_MODE), so the core needs
  no yt-dlp and no residential address
- Postgres backend with pgx, same schema and queries; per-user advisory
  lock and job row lock where SQLite's serial writes were relied on
- The whole test suite runs on SQLite and on Postgres
- S3 storage backend with presigned worker uploads, sharing a conformance
  suite with the local backend
- Dockerfile with core, worker and faketelegram targets, docker-compose
  with optional Postgres and local Bot API server, systemd units
- deploy/acceptance/kill-worker.sh: kills a worker mid-job and checks
  that the other one delivers the file
- Fixes found on the way: /cancel could let the next playlist item start;
  a refund note could be missing when the sweep refunded first

Docker was not available on the build machine: the compose files were
validated with "docker compose config" only. The acceptance scenario was
run with real processes.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Michilis
2026-11-18 09:27:11 +00:00
co-authored by Claude Fable 5.2
parent 9c9d641eda
commit 7d4893bc3f
51 changed files with 4732 additions and 108 deletions
+25 -4
View File
@@ -167,20 +167,26 @@ type Service struct {
sched *sched.Scheduler
billing *billing.Billing
cache *cache.Cache
runner ytdlp.Runner
prober Prober
log *slog.Logger
probes chan struct{}
now func() time.Time
}
// Prober looks up the metadata of a link. It is yt-dlp on the core, or the
// broker that hands lookups to remote workers.
type Prober interface {
Probe(ctx context.Context, url string) (*ytdlp.Info, error)
}
// New creates the service.
func New(cfg config.Config, store *db.Store, sch *sched.Scheduler, bill *billing.Billing, ch *cache.Cache, runner ytdlp.Runner, log *slog.Logger) *Service {
func New(cfg config.Config, store *db.Store, sch *sched.Scheduler, bill *billing.Billing, ch *cache.Cache, prober Prober, log *slog.Logger) *Service {
n := cfg.Worker.ProbeConcurrency
if n < 1 {
n = 1
}
return &Service{
cfg: cfg, store: store, sched: sch, billing: bill, cache: ch, runner: runner, log: log,
cfg: cfg, store: store, sched: sch, billing: bill, cache: ch, prober: prober, log: log,
probes: make(chan struct{}, n), now: time.Now,
}
}
@@ -218,7 +224,7 @@ func (s *Service) ProbeAny(ctx context.Context, url string) (*Probed, error) {
case <-ctx.Done():
return nil, ctx.Err()
}
info, err := s.runner.Probe(ctx, url)
info, err := s.prober.Probe(ctx, url)
if err != nil {
var ye *ytdlp.Error
if errors.As(err, &ye) {
@@ -496,6 +502,9 @@ func (s *Service) startOnce(ctx context.Context, who Who, p StartParams) (*db.Jo
QuotaDay: day,
CacheKey: cache.JobKey(r.extractor, r.videoID, string(p.Profile)),
Cacheable: r.cacheable,
// Sites that refuse some networks are routed to workers that carry
// the tags the owner configured for them.
RequiredTags: s.cfg.RequiredTags(p.Info.Site),
}
if p.OneOff != nil {
// Prepaid by invoice. Remember the amount and the payer's wallet so
@@ -622,6 +631,18 @@ func (s *Service) Release(ctx context.Context, deliveryID string) (refunded int6
return refunded, err
}
// Refunded returns the sats that were given back for a delivery, whoever
// did it and whenever. Several paths can release a delivery (the cancel
// handler, the failure handler, the periodic sweep) and only one of them
// wins; the message to the user should mention the refund regardless.
func (s *Service) Refunded(ctx context.Context, deliveryID string) int64 {
d, err := s.store.GetDelivery(ctx, deliveryID)
if err != nil || !d.Released || d.Status == db.DeliverySent || d.WalletID == "" {
return 0
}
return d.ChargeSats
}
// RecordDelivered adds the delivered bytes to the requester's daily volume.
func (s *Service) RecordDelivered(ctx context.Context, d *db.Delivery, bytes int64) error {
day := d.QuotaDay