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:
co-authored by
Claude Opus 5.5
parent
f1f9c6156c
commit
3c153af97f
+61
-1
@@ -24,6 +24,7 @@ import (
|
||||
"github.com/noderunners/dlbot/internal/ffmpeg"
|
||||
"github.com/noderunners/dlbot/internal/i18n"
|
||||
"github.com/noderunners/dlbot/internal/links"
|
||||
"github.com/noderunners/dlbot/internal/metrics"
|
||||
"github.com/noderunners/dlbot/internal/probe"
|
||||
"github.com/noderunners/dlbot/internal/sched"
|
||||
"github.com/noderunners/dlbot/internal/service"
|
||||
@@ -61,6 +62,7 @@ type App struct {
|
||||
Service *service.Service
|
||||
Bot *bot.Bot
|
||||
HTTP *api.Server
|
||||
Metrics *metrics.Metrics
|
||||
|
||||
tgBot *telego.Bot
|
||||
runner ytdlp.Runner
|
||||
@@ -258,7 +260,61 @@ func New(ctx context.Context, opts Options) (*App, error) {
|
||||
if err := registerStaticWorkers(ctx, a.Store, cfg.WorkerTokens, log); err != nil {
|
||||
return fail(err)
|
||||
}
|
||||
a.Metrics = metrics.New(metrics.Sources{
|
||||
Queue: func(ctx context.Context) (metrics.Queue, error) {
|
||||
paid, free, oldest, err := a.Store.QueueDepth(ctx, time.Now().UnixMilli())
|
||||
if err != nil {
|
||||
return metrics.Queue{}, err
|
||||
}
|
||||
running, err := a.Store.CountRunning(ctx)
|
||||
return metrics.Queue{
|
||||
Paid: paid, Free: free, OldestWait: time.Duration(oldest) * time.Millisecond,
|
||||
Running: running, Capacity: a.Sched.Capacity(),
|
||||
}, err
|
||||
},
|
||||
Workers: func(ctx context.Context) ([]metrics.Worker, error) {
|
||||
ws, err := a.Store.ListWorkers(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]metrics.Worker, 0, len(ws))
|
||||
for _, w := range ws {
|
||||
mw := metrics.Worker{Name: w.ID, Capacity: w.Capacity, State: "active"}
|
||||
if w.LastSeenAt > 0 {
|
||||
mw.LastSeen = time.UnixMilli(w.LastSeenAt)
|
||||
}
|
||||
switch {
|
||||
case w.Revoked:
|
||||
mw.State = "revoked"
|
||||
case w.Draining:
|
||||
mw.State = "draining"
|
||||
case mw.LastSeen.IsZero() || time.Since(mw.LastSeen) > 2*time.Minute:
|
||||
mw.State = "offline"
|
||||
}
|
||||
out = append(out, mw)
|
||||
}
|
||||
return out, nil
|
||||
},
|
||||
Cache: func() (int64, int64, int64) {
|
||||
st := a.Cache.Stats()
|
||||
return st.Hits, st.Misses, st.BytesSaved
|
||||
},
|
||||
Invoices: func() (int64, int64) {
|
||||
st := a.Billing.Stats()
|
||||
return st.InvoicesCreated, st.InvoicesSettled
|
||||
},
|
||||
Lightning: func() (bool, bool) { return a.Billing.Enabled(), a.Billing.Reachable() },
|
||||
Books: func(ctx context.Context) (int, error) {
|
||||
bad, err := a.Store.VerifyWallets(ctx)
|
||||
if len(bad) > 0 {
|
||||
log.Error("wallet balances disagree with the ledger", "wallets", len(bad))
|
||||
}
|
||||
return len(bad), err
|
||||
},
|
||||
})
|
||||
|
||||
a.Bot, err = bot.New(ctx, bot.Deps{
|
||||
Metrics: a.Metrics,
|
||||
API: a.tgBot,
|
||||
Config: cfg,
|
||||
Store: a.Store,
|
||||
@@ -280,6 +336,9 @@ func New(ctx context.Context, opts Options) (*App, error) {
|
||||
}
|
||||
|
||||
a.HTTP = api.New(cfg.HTTP.Listen, func(ctx context.Context) error { return a.DB.SQL.PingContext(ctx) }, log)
|
||||
if cfg.HTTP.Metrics {
|
||||
a.HTTP.Mux().Handle("GET /metrics", a.Metrics.Handler())
|
||||
}
|
||||
a.HTTP.RegisterPayments(api.Payments{
|
||||
Billing: a.Billing, Store: a.Store, I18n: tr, Secret: cfg.LNbits.WebhookSecret,
|
||||
})
|
||||
@@ -312,7 +371,7 @@ func New(ctx context.Context, opts Options) (*App, error) {
|
||||
}
|
||||
|
||||
// allowedUpdates is the list of update types the bot asks Telegram for.
|
||||
var allowedUpdates = []string{"message", "callback_query", "my_chat_member"}
|
||||
var allowedUpdates = []string{"message", "callback_query", "my_chat_member", "widget_query"}
|
||||
|
||||
// Run starts every loop and blocks until ctx ends. Shutdown is ordered:
|
||||
// updates stop first, then the worker hands back its jobs, then the
|
||||
@@ -357,6 +416,7 @@ func (a *App) Run(ctx context.Context) error {
|
||||
run(func() { a.Sched.Run(ctx) })
|
||||
run(func() { a.Billing.Run(ctx) })
|
||||
run(func() { a.Cache.Run(ctx, a.cfg.Cache.SweepInterval.D()) })
|
||||
run(func() { a.Bot.SweepWidgets(ctx, time.Hour) })
|
||||
if a.worker != nil {
|
||||
run(func() { a.worker.Run(ctx) })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user