Files
GsmNode/API Server/internal/api/sweeper.go
T
tajniak81 d6956395c8 Rebrand CommGate to gsmnode and adopt new design system
Rename the whole project from CommGate to gsmnode and re-skin every
surface onto the new signal-green + ink design system (Space Grotesk /
IBM Plex Sans / JetBrains Mono, flat surfaces, two-arrow routing mark,
lowercase gsm+node wordmark).

- Web App + API panel: rewrite token layer, gn-* utilities, data-gsm-theme,
  new logo/favicon assets; both builds verified.
- Phone App (Flutter): green theme, new mark/wordmark widgets, adaptive
  launcher icons; rename Android applicationId/package to app.gsmnode.phone;
  bump AGP to 8.6.0 and google_fonts to 8.1.0; debug APK build verified.
- API Server (Go): panel routes, User-Agent, health service id, branding.
- Home Assistant Plugin: rename integration domain sms_gateway to gsmnode
  (folder, DOMAIN, services, entity ids, classes); py_compile verified.
- Update all READMEs; add .gitignore (excludes Design/, build artifacts).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-06 08:53:14 +02:00

75 lines
2.2 KiB
Go

package api
import (
"context"
"log"
"time"
"smsgateway/apiserver/internal/pb"
)
// sweepInterval is how often stale messages are checked.
const sweepInterval = 30 * time.Second
// StartExpiryWorker launches a background loop that fails messages/calls which
// no device picked up or reported on within the configured MessageTTL. Without
// it, a message targeting an offline device would stay Pending forever.
func (s *Server) StartExpiryWorker(ctx context.Context) {
go func() {
ticker := time.NewTicker(sweepInterval)
defer ticker.Stop()
// Run once at startup so stale items clear promptly.
s.expireStaleMessages(ctx)
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
s.expireStaleMessages(ctx)
}
}
}()
}
// expireStaleMessages marks Pending/Processed messages older than MessageTTL as
// Failed. "Pending" means no online device pulled it; "Processed" means a device
// pulled it but never reported a terminal state (e.g. it crashed).
func (s *Server) expireStaleMessages(ctx context.Context) {
cutoff := time.Now().UTC().Add(-s.cfg.MessageTTL).Format("2006-01-02 15:04:05.000Z")
filter := `(status = "` + statusPending + `" || status = "` + statusProcessed +
`") && updated < "` + cutoff + `"`
res, err := s.pb.List(ctx, colMessages, pb.ListOptions{Filter: filter, PerPage: 200})
if err != nil {
log.Printf("expiry sweep failed: %v", err)
return
}
expired := 0
for _, rec := range res.Items {
// Don't expire messages scheduled for the future.
if asString(rec["schedule_at"]) != "" {
continue
}
id := asString(rec["id"])
const reason = "expired: no device processed the message within the timeout"
if _, err := s.pb.Update(ctx, colMessages, id, pb.Record{
"status": statusFailed,
"error": reason,
}); err != nil {
log.Printf("expire message %s: %v", id, err)
continue
}
expired++
s.dispatchWebhooks(asString(rec["owner"]), asString(rec["device"]), "sms:failed", map[string]any{
"message_id": id,
"type": asString(rec["type"]),
"status": statusFailed,
"error": reason,
})
}
if expired > 0 {
log.Printf("expired %d stale message(s) older than %s", expired, s.cfg.MessageTTL)
}
}