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
+37
View File
@@ -9,6 +9,7 @@ import (
"github.com/noderunners/dlbot/internal/db"
"github.com/noderunners/dlbot/internal/i18n"
"github.com/noderunners/dlbot/internal/links"
"github.com/noderunners/dlbot/internal/sched"
"github.com/noderunners/dlbot/internal/tg"
"github.com/noderunners/dlbot/internal/ytdlp"
@@ -63,6 +64,7 @@ func (b *Bot) eventLoop(ctx context.Context) {
}
func (b *Bot) handleEvent(ctx context.Context, ev sched.Event) {
b.observe(ctx, ev)
switch ev.Type {
case sched.EventStarted, sched.EventProgress, sched.EventRequeued:
b.refreshStatus(ctx, ev.JobID)
@@ -234,6 +236,9 @@ func (b *Bot) setStatus(ctx context.Context, d *db.Delivery, text string, kb *te
b.statuses[d.ID] = &statusState{text: shown, buttons: keyboardSignature(kb), phase: phase, at: now}
b.statusMu.Unlock()
if b.liveStatus(ctx, d, kb) {
return
}
if err := b.edit(ctx, d.ChatID, d.StatusMessageID, d.StatusIsPhoto, text, kb); err != nil {
if wait := tg.RetryAfter(err); wait > 0 {
// Back off this message for as long as Telegram asked.
@@ -254,6 +259,9 @@ func (b *Bot) finalStatus(ctx context.Context, d *db.Delivery, text string) {
b.statusMu.Lock()
delete(b.statuses, d.ID)
b.statusMu.Unlock()
if b.liveFinal(ctx, d, text) {
return
}
if d.StatusMessageID != 0 {
err := b.edit(ctx, d.ChatID, d.StatusMessageID, d.StatusIsPhoto, text, nil)
if err == nil || !tg.IsMessageGone(err) {
@@ -262,3 +270,32 @@ func (b *Bot) finalStatus(ctx context.Context, d *db.Delivery, text string) {
}
_, _ = b.send(ctx, d.ChatID, d.ReplyToMessageID, text, nil)
}
// observe feeds the metrics from job events.
func (b *Bot) observe(ctx context.Context, ev sched.Event) {
if b.metrics == nil {
return
}
switch ev.Type {
case sched.EventStarted, sched.EventStored, sched.EventFailed, sched.EventCancelled:
default:
return
}
job, err := b.store.GetJob(ctx, ev.JobID)
if err != nil {
return
}
switch ev.Type {
case sched.EventStarted:
if job.QueuedAt > 0 && job.StartedAt >= job.QueuedAt {
b.metrics.QueueWait(time.Duration(job.StartedAt-job.QueuedAt) * time.Millisecond)
}
case sched.EventStored:
b.metrics.JobFinished("done", job.Lane)
case sched.EventFailed:
b.metrics.JobFinished("failed", job.Lane)
b.metrics.DownloadError(links.Site(job.URL), job.ErrorCode)
case sched.EventCancelled:
b.metrics.JobFinished("cancelled", job.Lane)
}
}