Phase 8: widgets and polish

- Live job card widget (progress bar, in-widget cancel via widget_query)
  and live invoice widget (QR drawn from a bitmap, countdown, paid state),
  Bot API 10.4 sendWidget, private chats only
- Automatic fallback to the ordinary message when sendWidget fails, with
  a breaker that pauses widgets after repeated failures or on servers
  without the method; TELEGRAM_WIDGETS
- Prometheus metrics on the core (/metrics) and on every worker: queue
  depth and wait, slots, heartbeats, job outcomes, errors per site,
  deliveries, cache, invoices, Lightning, ledger check, widgets
- Example Prometheus config and alert rules, Caddyfile, dashboard notes
- Load test: cmd/loadtest and deploy/loadtest (make loadtest)
- Browser check of the widgets' HTML, including decoding a drawn QR
- docs/DEPLOY.md (production in under an hour) and docs/MONITORING.md

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Michilis
2026-11-18 18:24:36 +00:00
co-authored by Claude Opus 5.5
parent f1f9c6156c
commit 3c153af97f
45 changed files with 3158 additions and 20 deletions
+32
View File
@@ -74,6 +74,9 @@ type Worker struct {
active atomic.Int32
// runs numbers the jobs this process has started, for directory names.
runs atomic.Uint64
// outcomes counts finished jobs by outcome, for metrics.
outcomesMu sync.Mutex
outcomes map[string]int64
// lastOK is the unix time of the last call the core answered.
lastOK atomic.Int64
// authFailed is set when the core rejected the token.
@@ -110,6 +113,30 @@ func New(cfg Config, coord Coordinator, runner ytdlp.Runner, log *slog.Logger) *
// Active returns the number of jobs running right now.
func (w *Worker) Active() int { return int(w.active.Load()) }
func (w *Worker) count(outcome string) {
w.outcomesMu.Lock()
if w.outcomes == nil {
w.outcomes = map[string]int64{}
}
w.outcomes[outcome]++
w.outcomesMu.Unlock()
}
// Outcomes returns how many jobs ended in each way since the start:
// done, failed, cancelled, handed_back, lease_lost.
func (w *Worker) Outcomes() map[string]int64 {
w.outcomesMu.Lock()
defer w.outcomesMu.Unlock()
out := make(map[string]int64, len(w.outcomes))
for k, v := range w.outcomes {
out[k] = v
}
return out
}
// Capacity is the number of downloads the worker runs at once.
func (w *Worker) Capacity() int { return w.cfg.Concurrency }
// Run starts the worker slots and blocks until the context ends and every
// running job has stopped.
func (w *Worker) Run(ctx context.Context) {
@@ -399,14 +426,17 @@ func (w *Worker) process(ctx context.Context, job *workerproto.Job) {
switch {
case leaseLost.Load():
log.Warn("lease lost, dropping job")
w.count("lease_lost")
return
case cancelled.Load():
w.count("cancelled")
w.report(log, w.coord.Fail(rctx, workerproto.FailRequest{
WorkerID: w.cfg.ID, JobID: job.ID, Code: string(ytdlp.CodeCancelled),
}))
return
case ctx.Err() != nil:
// Shutdown in the middle of a job: hand it back for another worker.
w.count("handed_back")
w.report(log, w.coord.Fail(rctx, workerproto.FailRequest{
WorkerID: w.cfg.ID, JobID: job.ID, Code: string(ytdlp.CodeWorkerLost), Retryable: true,
Detail: "worker shut down",
@@ -418,12 +448,14 @@ func (w *Worker) process(ctx context.Context, job *workerproto.Job) {
ye = &ytdlp.Error{Code: ytdlp.CodeUnknown, Retryable: true, Raw: err.Error()}
}
log.Info("job failed", "code", string(ye.Code), "retryable", ye.Retryable)
w.count("failed")
w.report(log, w.coord.Fail(rctx, workerproto.FailRequest{
WorkerID: w.cfg.ID, JobID: job.ID, Code: string(ye.Code), Retryable: ye.Retryable, Detail: ye.Raw,
}))
return
}
w.count("done")
w.report(log, w.coord.Complete(rctx, workerproto.CompleteRequest{
WorkerID: w.cfg.ID,
JobID: job.ID,