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
+105
View File
@@ -248,3 +248,108 @@ func TestDotEnvDoesNotOverrideRealEnv(t *testing.T) {
t.Errorf("a missing .env is fine, got %v", err)
}
}
func TestSiteTagsAndRequiredTags(t *testing.T) {
m, err := ParseSiteTags(" YouTube.com = residential ; example.com=eu, residential ;; x.com=residential")
if err != nil {
t.Fatal(err)
}
cfg := Default()
cfg.SiteTags = m
tests := map[string]string{
"youtube.com": "residential",
"music.youtube.com": "residential", // a rule covers subdomains
"example.com": "eu,residential",
"cdn.eu.example.com": "eu,residential",
"notyoutube.com": "",
"com": "",
"": "",
"vimeo.com": "",
"YOUTUBE.COM": "residential",
}
for site, want := range tests {
if got := cfg.RequiredTags(site); got != want {
t.Errorf("RequiredTags(%q) = %q, want %q", site, got, want)
}
}
// Rules of a host and of its parent add up.
cfg.SiteTags["eu.example.com"] = []string{"fast"}
if got := cfg.RequiredTags("cdn.eu.example.com"); got != "eu,fast,residential" {
t.Errorf("combined rules = %q", got)
}
if _, err := ParseSiteTags("no-equals-sign"); err == nil {
t.Error("a malformed SITE_TAGS must be rejected")
}
if got := Default().RequiredTags("youtube.com"); got != "" {
t.Errorf("without rules nothing is required, got %q", got)
}
}
func TestProbeModeAndScaleOutValidation(t *testing.T) {
cfg := Default()
cfg.Telegram.Token = "x"
if !cfg.ProbeLocally() {
t.Fatal("with the embedded worker, the core probes by itself")
}
cfg.Worker.Embedded = false
if cfg.ProbeLocally() {
t.Fatal("without the embedded worker, probes go to remote workers")
}
cfg.ProbeMode = "local"
if !cfg.ProbeLocally() {
t.Fatal("PROBE_MODE=local forces local probing")
}
cfg.ProbeMode = "sometimes"
if err := cfg.Validate(); err == nil {
t.Fatal("an unknown PROBE_MODE must be rejected")
}
cfg.ProbeMode = "auto"
cfg.WorkerTokens = []string{"eu-1:short"}
if err := cfg.Validate(); err == nil {
t.Fatal("a short worker token must be rejected")
}
cfg.WorkerTokens = []string{"eu-1:dlw_0123456789abcdef"}
if err := cfg.Validate(); err != nil {
t.Fatalf("a good worker token entry: %v", err)
}
cfg.Storage.Backend = "s3"
if err := cfg.Validate(); err == nil {
t.Fatal("s3 without endpoint, bucket and keys must be rejected")
}
cfg.Storage.S3 = S3{Endpoint: "https://s3.example.com", Bucket: "b", AccessKey: "k", SecretKey: "s"}
if err := cfg.Validate(); err != nil {
t.Fatalf("complete s3 settings: %v", err)
}
}
func TestWorkerConfig(t *testing.T) {
cfg := DefaultWorker()
if err := cfg.Validate(); err == nil {
t.Fatal("a worker needs CORE_URL and WORKER_TOKEN")
}
cfg.CoreURL, cfg.Token = "bot.example.com", "dlw_x"
if err := cfg.Validate(); err == nil {
t.Fatal("CORE_URL needs a scheme")
}
cfg.CoreURL = "https://bot.example.com"
if err := cfg.Validate(); err != nil {
t.Fatalf("a complete worker config: %v", err)
}
if cfg.Heartbeat.D() != 10*time.Second || cfg.Concurrency != 2 || cfg.ProbeSlots != 2 {
t.Fatalf("worker defaults changed: %+v", cfg)
}
env := map[string]string{
"CORE_URL": "https://core.example.com/", "WORKER_TOKEN": "dlw_abc", "WORKER_TAGS": "residential, eu",
"YTDLP_EXTRA_ARGS": "--limit-rate 1M", "WORKER_HEALTH_LISTEN": "off", "YTDLP_AUTO_UPDATE": "0",
}
w := DefaultWorker()
if err := applyEnv(reflect.ValueOf(&w).Elem(), lookup(env)); err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(w.Tags, []string{"residential", "eu"}) || !reflect.DeepEqual(w.ExtraArgs, []string{"--limit-rate", "1M"}) ||
w.AutoUpdate.D() != 0 {
t.Fatalf("worker env not applied: %+v", w)
}
}