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
+51
-7
@@ -4,6 +4,8 @@ import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const jobCols = `id, kind, url, extractor, video_id, profile, cache_key, status, lane, priority,
|
||||
@@ -66,21 +68,63 @@ const laneRank = `CASE lane WHEN 'paid' THEN 0 ELSE 1 END`
|
||||
// queueOrder is the one ordering used for claiming and for positions.
|
||||
const queueOrder = laneRank + ` ASC, priority DESC, queued_at ASC, id ASC`
|
||||
|
||||
// NextQueuedJob returns the first queued job in the given lanes, locking it
|
||||
// against other claimers. It must run inside a transaction. On Postgres the
|
||||
// lock is FOR UPDATE SKIP LOCKED; on SQLite the immediate transaction
|
||||
// already serializes writers.
|
||||
func (s *Store) NextQueuedJob(ctx context.Context, lanes []string) (*Job, error) {
|
||||
// claimWindow is how many queued jobs a claim looks at to find one whose
|
||||
// required tags the worker has.
|
||||
const claimWindow = 25
|
||||
|
||||
// NextQueuedJob returns the first queued job in the given lanes that the
|
||||
// worker's tags qualify for, locking it against other claimers. It must
|
||||
// run inside a transaction. On Postgres the lock is FOR UPDATE SKIP
|
||||
// LOCKED; on SQLite the immediate transaction already serializes writers.
|
||||
//
|
||||
// A job with required tags is skipped by workers that lack one of them.
|
||||
// The claim looks at the head of the queue, so a job that needs a rare tag
|
||||
// waits its turn there without blocking the jobs behind it.
|
||||
func (s *Store) NextQueuedJob(ctx context.Context, lanes []string, tags []string) (*Job, error) {
|
||||
if !s.inTx {
|
||||
return nil, errors.New("db: NextQueuedJob needs a transaction")
|
||||
}
|
||||
ph, args := in(lanes)
|
||||
q := `SELECT ` + jobCols + ` FROM jobs WHERE status = 'queued' AND lane IN (` + ph + `)
|
||||
ORDER BY ` + queueOrder + ` LIMIT 1`
|
||||
ORDER BY ` + queueOrder + ` LIMIT ` + strconv.Itoa(claimWindow)
|
||||
if s.db.Dialect == Postgres {
|
||||
q += ` FOR UPDATE SKIP LOCKED`
|
||||
}
|
||||
return scanJob(s.row(ctx, q, args...))
|
||||
jobs, err := scanJobs(s.rows(ctx, q, args...))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, j := range jobs {
|
||||
if HasTags(tags, j.RequiredTags) {
|
||||
return j, nil
|
||||
}
|
||||
}
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
|
||||
// HasTags reports whether a worker's tags include every required tag.
|
||||
// required is a comma separated list; empty means "any worker".
|
||||
func HasTags(have []string, required string) bool {
|
||||
if required == "" {
|
||||
return true
|
||||
}
|
||||
for _, need := range strings.Split(required, ",") {
|
||||
need = strings.TrimSpace(need)
|
||||
if need == "" {
|
||||
continue
|
||||
}
|
||||
found := false
|
||||
for _, h := range have {
|
||||
if h == need {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// MarkJobRunning hands a queued job to a worker.
|
||||
|
||||
Reference in New Issue
Block a user