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
@@ -16,6 +16,7 @@ import (
|
||||
"github.com/noderunners/dlbot/internal/ids"
|
||||
"github.com/noderunners/dlbot/internal/media"
|
||||
"github.com/noderunners/dlbot/internal/service"
|
||||
"github.com/noderunners/dlbot/internal/workerproto"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -176,10 +177,19 @@ func (b *Bot) startPlaylistItem(ctx context.Context, item *db.Request) bool {
|
||||
return false // no free slot yet: stay in line
|
||||
}
|
||||
}
|
||||
began := time.Now()
|
||||
took, err := b.store.TransitionRequest(ctx, item.ID, db.RequestWaiting, db.RequestChosen)
|
||||
if err != nil || !took {
|
||||
return false
|
||||
}
|
||||
// cancelledSince reports a /cancel that arrived while this item was
|
||||
// being prepared. The item was no longer "waiting", so the command
|
||||
// could not see it.
|
||||
cancelledSince := func() bool {
|
||||
b.statusMu.Lock()
|
||||
defer b.statusMu.Unlock()
|
||||
return b.cancelledAt[item.UserID].After(began)
|
||||
}
|
||||
lang := item.Lang
|
||||
fail := func(title, reason string) bool {
|
||||
if _, err := b.store.TransitionRequest(context.WithoutCancel(ctx), item.ID, db.RequestChosen, db.RequestFailed); err != nil {
|
||||
@@ -204,6 +214,10 @@ func (b *Bot) startPlaylistItem(ctx context.Context, item *db.Request) bool {
|
||||
case probed.Info == nil:
|
||||
return fail(item.URL, b.tr.T(lang, "error.playlist_nested", nil))
|
||||
}
|
||||
if cancelledSince() {
|
||||
_, _ = b.store.TransitionRequest(context.WithoutCancel(ctx), item.ID, db.RequestChosen, db.RequestCancelled)
|
||||
return false
|
||||
}
|
||||
info := probed.Info
|
||||
if err := b.store.SetRequestInfo(ctx, item.ID, info.JSON()); err != nil {
|
||||
b.log.Error("save playlist item info", "err", err)
|
||||
@@ -451,3 +465,88 @@ func (b *Bot) cmdCache(ctx context.Context, m *telego.Message, act actor) {
|
||||
"Since": strconv.FormatInt(st.Hits+st.Misses, 10),
|
||||
}), nil)
|
||||
}
|
||||
|
||||
// --- owner: workers ----------------------------------------------------------
|
||||
|
||||
func (b *Bot) cmdWorkers(ctx context.Context, m *telego.Message, act actor) {
|
||||
lang := act.lang
|
||||
ws, err := b.store.ListWorkers(ctx)
|
||||
if err != nil {
|
||||
b.internalError(ctx, m.Chat.ID, act, err)
|
||||
return
|
||||
}
|
||||
running, _ := b.store.RunningByWorker(ctx)
|
||||
var sb strings.Builder
|
||||
sb.WriteString(b.tr.HTML(lang, "owner.workers_header", i18n.Args{
|
||||
"Slots": b.sched.Capacity(), "Count": len(ws),
|
||||
}))
|
||||
if b.cfg.Worker.Embedded {
|
||||
sb.WriteString("\n")
|
||||
sb.WriteString(b.tr.HTML(lang, "owner.worker_embedded", i18n.Args{
|
||||
"Capacity": b.cfg.Worker.Concurrency, "Running": running["embedded"],
|
||||
}))
|
||||
}
|
||||
now := time.Now()
|
||||
for _, w := range ws {
|
||||
state := "owner.state_active"
|
||||
switch {
|
||||
case w.Revoked:
|
||||
state = "owner.state_revoked"
|
||||
case w.Draining:
|
||||
state = "owner.state_draining"
|
||||
case w.LastSeenAt == 0 || now.Sub(time.UnixMilli(w.LastSeenAt)) > 2*time.Minute:
|
||||
state = "owner.state_offline"
|
||||
}
|
||||
seen := b.tr.T(lang, "owner.never", nil)
|
||||
if w.LastSeenAt > 0 {
|
||||
seen = b.tr.Span(lang, now.Sub(time.UnixMilli(w.LastSeenAt)))
|
||||
}
|
||||
tags := w.Tags
|
||||
if tags == "" {
|
||||
tags = "-"
|
||||
}
|
||||
sb.WriteString("\n")
|
||||
sb.WriteString(b.tr.HTML(lang, "owner.worker_line", i18n.Args{
|
||||
"Name": w.ID, "State": b.tr.T(lang, state, nil), "Running": running[w.ID], "Capacity": w.Capacity,
|
||||
"Seen": seen, "Tags": tags, "Version": w.Version,
|
||||
}))
|
||||
}
|
||||
_, _ = b.send(ctx, m.Chat.ID, 0, sb.String(), nil)
|
||||
}
|
||||
|
||||
// cmdDrain handles /drain <worker> and /drain <worker> off. A draining
|
||||
// worker finishes its jobs and is given no new ones.
|
||||
func (b *Bot) cmdDrain(ctx context.Context, m *telego.Message, act actor) {
|
||||
lang := act.lang
|
||||
fields := strings.Fields(commandArgs(m))
|
||||
if len(fields) == 0 || len(fields) > 2 || (len(fields) == 2 && fields[1] != "off" && fields[1] != "on") {
|
||||
_, _ = b.send(ctx, m.Chat.ID, 0, b.tr.HTML(lang, "owner.drain_usage", nil), nil)
|
||||
return
|
||||
}
|
||||
drain := len(fields) == 1 || fields[1] == "on"
|
||||
ok, err := b.store.SetWorkerDraining(ctx, fields[0], drain)
|
||||
if err != nil {
|
||||
b.internalError(ctx, m.Chat.ID, act, err)
|
||||
return
|
||||
}
|
||||
key := "owner.drain_on"
|
||||
switch {
|
||||
case !ok:
|
||||
key = "owner.worker_unknown"
|
||||
case !drain:
|
||||
key = "owner.drain_off"
|
||||
}
|
||||
_, _ = b.send(ctx, m.Chat.ID, 0, b.tr.HTML(lang, key, i18n.Args{"Name": fields[0]}), nil)
|
||||
}
|
||||
|
||||
// cmdYtDlp handles /ytdlp update: every live worker updates yt-dlp when
|
||||
// it next asks for work.
|
||||
func (b *Bot) cmdYtDlp(ctx context.Context, m *telego.Message, act actor) {
|
||||
lang := act.lang
|
||||
if strings.TrimSpace(commandArgs(m)) != "update" {
|
||||
_, _ = b.send(ctx, m.Chat.ID, 0, b.tr.HTML(lang, "owner.ytdlp_usage", nil), nil)
|
||||
return
|
||||
}
|
||||
n := b.sched.Command(workerproto.CommandUpdateYtDlp)
|
||||
_, _ = b.send(ctx, m.Chat.ID, 0, b.tr.HTML(lang, "owner.ytdlp_update", i18n.Args{"Count": n}), nil)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user