Files
MichilisandClaude Fable 5.2 7d4893bc3f 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>
2026-11-18 09:27:11 +00:00

60 lines
1.5 KiB
Go

// Command core is the bot gateway: it talks to Telegram, owns the queue and
// the database, serves the HTTP API and, by default, runs an embedded
// download worker.
//
// core run the bot
// core worker add <name> register a remote worker and print its token
// core worker revoke <name> revoke a worker's token
// core worker list list workers
package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/noderunners/dlbot/internal/app"
"github.com/noderunners/dlbot/internal/config"
)
func main() {
configPath := flag.String("config", "", "path to a TOML config file (optional, env vars override it)")
flag.Parse()
cfg, err := config.Load(*configPath)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(2)
}
log := app.NewLogger(cfg.Log, app.Secrets(cfg)...)
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
// "core worker ..." manages worker tokens and exits.
if args := flag.Args(); len(args) > 0 {
if args[0] != "worker" {
fmt.Fprintf(os.Stderr, "unknown command %q\n", args[0])
os.Exit(2)
}
if err := app.WorkerAdmin(ctx, cfg, args[1:], os.Stdout); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
return
}
a, err := app.New(ctx, app.Options{Config: cfg, Log: log})
if err != nil {
log.Error("startup failed", "err", err)
os.Exit(1)
}
if err := a.Run(ctx); err != nil {
log.Error("core exited with error", "err", err)
os.Exit(1)
}
}