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:
co-authored by
Claude Fable 5.2
parent
9c9d641eda
commit
7d4893bc3f
@@ -0,0 +1,105 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"reflect"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// WorkerConfig is the configuration of the standalone worker binary. A
|
||||
// worker knows the core's URL and its own token, and nothing else about
|
||||
// the system: no bot token, no database, no payment keys.
|
||||
type WorkerConfig struct {
|
||||
// CoreURL is the base URL of the core, for example
|
||||
// https://bot.example.com.
|
||||
CoreURL string `env:"CORE_URL"`
|
||||
// Token is this worker's bearer token, created on the core with
|
||||
// "core worker add <name>".
|
||||
Token string `env:"WORKER_TOKEN"`
|
||||
// Tags describe what this worker can do or where it runs, for example
|
||||
// "residential,eu". Jobs for sites that require a tag only go to
|
||||
// workers that have it.
|
||||
Tags []string `env:"WORKER_TAGS"`
|
||||
Concurrency int `env:"WORKER_CONCURRENCY"`
|
||||
// TempDir is scratch space. Each worker process needs its own.
|
||||
TempDir string `env:"WORKER_TEMP_DIR"`
|
||||
YtDlpPath string `env:"YTDLP_PATH"`
|
||||
FfmpegPath string `env:"FFMPEG_PATH"`
|
||||
// ExtraArgs are appended to every yt-dlp call, for example
|
||||
// "--proxy socks5://127.0.0.1:1080".
|
||||
ExtraArgs []string `env:"YTDLP_EXTRA_ARGS"`
|
||||
DownloadTimeout Duration `env:"DOWNLOAD_TIMEOUT"`
|
||||
ProbeTimeout Duration `env:"PROBE_TIMEOUT"`
|
||||
// ProbeSlots is how many metadata lookups run next to the downloads.
|
||||
ProbeSlots int `env:"WORKER_PROBE_SLOTS"`
|
||||
// Heartbeat must be well under the core's QUEUE_LEASE.
|
||||
Heartbeat Duration `env:"WORKER_HEARTBEAT"`
|
||||
// AutoUpdate is how often yt-dlp updates itself. 0 turns it off.
|
||||
AutoUpdate Duration `env:"YTDLP_AUTO_UPDATE"`
|
||||
// HealthListen is the address of the health endpoint, or "off". It is
|
||||
// the only port a worker opens and it is not needed by the core. Several
|
||||
// workers on one machine need different ports.
|
||||
HealthListen string `env:"WORKER_HEALTH_LISTEN"`
|
||||
Log Log
|
||||
}
|
||||
|
||||
// DefaultWorker returns the worker defaults.
|
||||
func DefaultWorker() WorkerConfig {
|
||||
return WorkerConfig{
|
||||
Concurrency: 2,
|
||||
TempDir: "data/worker-tmp",
|
||||
YtDlpPath: "yt-dlp",
|
||||
FfmpegPath: "ffmpeg",
|
||||
DownloadTimeout: Duration(30 * time.Minute),
|
||||
ProbeTimeout: Duration(45 * time.Second),
|
||||
ProbeSlots: 2,
|
||||
Heartbeat: Duration(10 * time.Second),
|
||||
AutoUpdate: Duration(24 * time.Hour),
|
||||
HealthListen: ":8081",
|
||||
Log: Log{Level: "info", Format: "json"},
|
||||
}
|
||||
}
|
||||
|
||||
// LoadWorker reads the worker configuration from the environment, after
|
||||
// loading a .env file from the working directory if there is one.
|
||||
func LoadWorker() (WorkerConfig, error) {
|
||||
cfg := DefaultWorker()
|
||||
if err := loadDotEnv(".env"); err != nil {
|
||||
return cfg, err
|
||||
}
|
||||
if err := applyEnv(reflect.ValueOf(&cfg).Elem(), os.LookupEnv); err != nil {
|
||||
return cfg, err
|
||||
}
|
||||
cfg.CoreURL = strings.TrimRight(cfg.CoreURL, "/")
|
||||
// An empty variable keeps the default, so "off" is how to say no.
|
||||
switch strings.ToLower(cfg.HealthListen) {
|
||||
case "off", "none", "false", "-":
|
||||
cfg.HealthListen = ""
|
||||
}
|
||||
return cfg, cfg.Validate()
|
||||
}
|
||||
|
||||
// Validate reports configuration mistakes.
|
||||
func (c WorkerConfig) Validate() error {
|
||||
var errs []string
|
||||
if c.CoreURL == "" {
|
||||
errs = append(errs, "CORE_URL is required")
|
||||
} else if !strings.HasPrefix(c.CoreURL, "http://") && !strings.HasPrefix(c.CoreURL, "https://") {
|
||||
errs = append(errs, "CORE_URL must start with http:// or https://")
|
||||
}
|
||||
if c.Token == "" {
|
||||
errs = append(errs, "WORKER_TOKEN is required")
|
||||
}
|
||||
if c.Concurrency < 1 {
|
||||
errs = append(errs, "WORKER_CONCURRENCY must be at least 1")
|
||||
}
|
||||
if c.Heartbeat.D() <= 0 {
|
||||
errs = append(errs, "WORKER_HEARTBEAT must be positive")
|
||||
}
|
||||
if len(errs) > 0 {
|
||||
return errors.New("config: " + strings.Join(errs, "; "))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user