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,140 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
// Worker is a registered download worker.
|
||||
type Worker struct {
|
||||
ID string
|
||||
TokenHash string
|
||||
Tags string // comma separated, as last advertised
|
||||
Capacity int
|
||||
Draining bool
|
||||
Revoked bool
|
||||
Version string // yt-dlp version, as last reported
|
||||
LastSeenAt int64
|
||||
CreatedAt int64
|
||||
}
|
||||
|
||||
const workerCols = `id, token_hash, tags, capacity, draining, revoked, version, last_seen_at, created_at`
|
||||
|
||||
func scanWorker(sc interface{ Scan(...any) error }) (*Worker, error) {
|
||||
var w Worker
|
||||
err := sc.Scan(&w.ID, &w.TokenHash, &w.Tags, &w.Capacity, &w.Draining, &w.Revoked, &w.Version,
|
||||
&w.LastSeenAt, &w.CreatedAt)
|
||||
if err != nil {
|
||||
return nil, notFound(err)
|
||||
}
|
||||
return &w, nil
|
||||
}
|
||||
|
||||
// UpsertWorker registers a worker or replaces its token. Replacing the
|
||||
// token also lifts a revocation: issuing a new token is how a worker is
|
||||
// brought back.
|
||||
func (s *Store) UpsertWorker(ctx context.Context, id, tokenHash string) error {
|
||||
_, err := s.exec(ctx, `INSERT INTO workers (id, token_hash, created_at) VALUES (?, ?, ?)
|
||||
ON CONFLICT (id) DO UPDATE SET token_hash = excluded.token_hash, revoked = ?`,
|
||||
id, tokenHash, NowMilli(), false)
|
||||
if isUniqueViolation(err) {
|
||||
return ErrDuplicate
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// WorkerByToken finds the worker a token hash belongs to.
|
||||
func (s *Store) WorkerByToken(ctx context.Context, tokenHash string) (*Worker, error) {
|
||||
return scanWorker(s.row(ctx, `SELECT `+workerCols+` FROM workers WHERE token_hash = ?`, tokenHash))
|
||||
}
|
||||
|
||||
// GetWorker loads a worker by ID.
|
||||
func (s *Store) GetWorker(ctx context.Context, id string) (*Worker, error) {
|
||||
return scanWorker(s.row(ctx, `SELECT `+workerCols+` FROM workers WHERE id = ?`, id))
|
||||
}
|
||||
|
||||
// ListWorkers returns all workers, most recently seen first.
|
||||
func (s *Store) ListWorkers(ctx context.Context) ([]*Worker, error) {
|
||||
rows, err := s.rows(ctx, `SELECT `+workerCols+` FROM workers ORDER BY last_seen_at DESC, id`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []*Worker
|
||||
for rows.Next() {
|
||||
w, err := scanWorker(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, w)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// TouchWorker records what a worker advertised in its latest call.
|
||||
func (s *Store) TouchWorker(ctx context.Context, id, tags string, capacity int, version string, now int64) error {
|
||||
_, err := s.exec(ctx, `UPDATE workers SET tags = ?, capacity = ?,
|
||||
version = CASE WHEN ? <> '' THEN ? ELSE version END, last_seen_at = ? WHERE id = ?`,
|
||||
tags, capacity, version, version, now, id)
|
||||
return err
|
||||
}
|
||||
|
||||
// SetWorkerDraining turns draining on or off and reports whether the
|
||||
// worker exists.
|
||||
func (s *Store) SetWorkerDraining(ctx context.Context, id string, draining bool) (bool, error) {
|
||||
n, err := s.exec(ctx, `UPDATE workers SET draining = ? WHERE id = ?`, draining, id)
|
||||
return n == 1, err
|
||||
}
|
||||
|
||||
// RevokeWorker makes a worker's token useless and reports whether the
|
||||
// worker exists.
|
||||
func (s *Store) RevokeWorker(ctx context.Context, id string) (bool, error) {
|
||||
n, err := s.exec(ctx, `UPDATE workers SET revoked = ? WHERE id = ?`, true, id)
|
||||
return n == 1, err
|
||||
}
|
||||
|
||||
// RunningByWorker counts running jobs per worker.
|
||||
func (s *Store) RunningByWorker(ctx context.Context) (map[string]int, error) {
|
||||
rows, err := s.rows(ctx, `SELECT worker_id, COUNT(*) FROM jobs WHERE status = 'running' GROUP BY worker_id`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
out := map[string]int{}
|
||||
for rows.Next() {
|
||||
var id string
|
||||
var n int
|
||||
if err := rows.Scan(&id, &n); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out[id] = n
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// QueueDepth counts queued jobs per lane and returns the age in
|
||||
// milliseconds of the oldest one.
|
||||
func (s *Store) QueueDepth(ctx context.Context, now int64) (paid, free int, oldestMS int64, err error) {
|
||||
rows, err := s.rows(ctx, `SELECT lane, COUNT(*), MIN(queued_at) FROM jobs WHERE status = 'queued' GROUP BY lane`)
|
||||
if err != nil {
|
||||
return 0, 0, 0, err
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var lane string
|
||||
var n int
|
||||
var oldest sql.NullInt64
|
||||
if err := rows.Scan(&lane, &n, &oldest); err != nil {
|
||||
return 0, 0, 0, err
|
||||
}
|
||||
if lane == LanePaid {
|
||||
paid = n
|
||||
} else {
|
||||
free += n
|
||||
}
|
||||
if oldest.Valid && now-oldest.Int64 > oldestMS {
|
||||
oldestMS = now - oldest.Int64
|
||||
}
|
||||
}
|
||||
return paid, free, oldestMS, rows.Err()
|
||||
}
|
||||
Reference in New Issue
Block a user