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 ". 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 }