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
+55
-3
@@ -22,6 +22,8 @@ import (
|
||||
"github.com/noderunners/dlbot/internal/db"
|
||||
"github.com/noderunners/dlbot/internal/ffmpeg"
|
||||
"github.com/noderunners/dlbot/internal/i18n"
|
||||
"github.com/noderunners/dlbot/internal/links"
|
||||
"github.com/noderunners/dlbot/internal/probe"
|
||||
"github.com/noderunners/dlbot/internal/sched"
|
||||
"github.com/noderunners/dlbot/internal/service"
|
||||
"github.com/noderunners/dlbot/internal/storage"
|
||||
@@ -53,6 +55,7 @@ type App struct {
|
||||
Sched *sched.Scheduler
|
||||
Billing *billing.Billing
|
||||
Cache *cache.Cache
|
||||
Broker *probe.Broker
|
||||
Service *service.Service
|
||||
Bot *bot.Bot
|
||||
HTTP *api.Server
|
||||
@@ -113,10 +116,34 @@ func Redactor(secrets ...string) func(groups []string, a slog.Attr) slog.Attr {
|
||||
|
||||
// Secrets lists the configured values that must never appear in logs.
|
||||
func Secrets(cfg config.Config) []string {
|
||||
return []string{
|
||||
out := []string{
|
||||
cfg.Telegram.Token, cfg.Telegram.WebhookSecret,
|
||||
cfg.LNbits.InvoiceKey, cfg.LNbits.WebhookSecret,
|
||||
cfg.Storage.S3.SecretKey,
|
||||
}
|
||||
// Static worker tokens are secrets too.
|
||||
for _, pair := range cfg.WorkerTokens {
|
||||
if _, token, ok := strings.Cut(pair, ":"); ok {
|
||||
out = append(out, token)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// registerStaticWorkers makes sure the workers named in WORKER_TOKENS
|
||||
// exist with those tokens. Only token hashes are stored.
|
||||
func registerStaticWorkers(ctx context.Context, store *db.Store, pairs []string, log *slog.Logger) error {
|
||||
for _, pair := range pairs {
|
||||
name, token, ok := strings.Cut(pair, ":")
|
||||
if !ok || name == "" || token == "" {
|
||||
continue
|
||||
}
|
||||
if err := store.UpsertWorker(ctx, name, api.HashWorkerToken(token)); err != nil {
|
||||
return fmt.Errorf("app: register worker %s: %w", name, err)
|
||||
}
|
||||
log.Info("worker registered from config", "worker_id", name)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// New opens the database and storage and builds every component. Nothing
|
||||
@@ -147,8 +174,18 @@ func New(ctx context.Context, opts Options) (*App, error) {
|
||||
if a.Storage, err = storage.NewLocal(cfg.Storage.LocalDir); err != nil {
|
||||
return fail(err)
|
||||
}
|
||||
case "s3":
|
||||
s3 := cfg.Storage.S3
|
||||
a.Storage, err = storage.NewS3(ctx, storage.S3Config{
|
||||
Endpoint: s3.Endpoint, Region: s3.Region, Bucket: s3.Bucket, AccessKey: s3.AccessKey,
|
||||
SecretKey: s3.SecretKey, UseSSL: s3.UseSSL, PathStyle: s3.PathStyle, Prefix: s3.Prefix,
|
||||
Presign: s3.Presign, PresignEndpoint: s3.PresignEndpoint,
|
||||
})
|
||||
if err != nil {
|
||||
return fail(err)
|
||||
}
|
||||
default:
|
||||
return fail(fmt.Errorf("app: storage backend %q is not available in this version", cfg.Storage.Backend))
|
||||
return fail(fmt.Errorf("app: unknown storage backend %q", cfg.Storage.Backend))
|
||||
}
|
||||
|
||||
tr, err := i18n.Load(locales.FS, log)
|
||||
@@ -193,7 +230,18 @@ func New(ctx context.Context, opts Options) (*App, error) {
|
||||
MetaTTL: cfg.Cache.MetaTTL.D(),
|
||||
Derive: cfg.Cache.Derive,
|
||||
}, a.tgBot.ID(), a.Store, a.Storage, log)
|
||||
a.Service = service.New(cfg, a.Store, a.Sched, a.Billing, a.Cache, a.runner, log)
|
||||
// Metadata lookups run on the core, or go to remote workers when the
|
||||
// core has no yt-dlp of its own or sits on a network that sites refuse.
|
||||
a.Broker = probe.NewBroker(cfg.Worker.ProbeTimeout.D(),
|
||||
func(url string) string { return cfg.RequiredTags(links.Site(url)) }, a.Sched.NotifyWork)
|
||||
var prober service.Prober = a.Broker
|
||||
if cfg.ProbeLocally() {
|
||||
prober = a.runner
|
||||
}
|
||||
a.Service = service.New(cfg, a.Store, a.Sched, a.Billing, a.Cache, prober, log)
|
||||
if err := registerStaticWorkers(ctx, a.Store, cfg.WorkerTokens, log); err != nil {
|
||||
return fail(err)
|
||||
}
|
||||
a.Bot, err = bot.New(ctx, bot.Deps{
|
||||
API: a.tgBot,
|
||||
Config: cfg,
|
||||
@@ -218,10 +266,14 @@ func New(ctx context.Context, opts Options) (*App, error) {
|
||||
a.HTTP.RegisterPayments(api.Payments{
|
||||
Billing: a.Billing, Store: a.Store, I18n: tr, Secret: cfg.LNbits.WebhookSecret,
|
||||
})
|
||||
a.HTTP.RegisterWorkerAPI(&api.WorkerAPI{
|
||||
Store: a.Store, Sched: a.Sched, Storage: a.Storage, Broker: a.Broker, Log: log,
|
||||
})
|
||||
|
||||
if cfg.Worker.Embedded {
|
||||
a.worker = worker.New(worker.Config{
|
||||
ID: "embedded",
|
||||
Tags: cfg.Worker.Tags,
|
||||
Concurrency: cfg.Worker.Concurrency,
|
||||
TempDir: cfg.Worker.TempDir,
|
||||
Heartbeat: cfg.Queue.Heartbeat.D(),
|
||||
|
||||
Reference in New Issue
Block a user