Files
minstrel/internal/library/eventbus.go
T
bvandeusenandClaude Opus 5.5 7e6228c13f
release / govulncheck (push) Successful in 20s
release / web (push) Successful in 1m20s
release / go (push) Successful in 1m37s
release / integration (push) Successful in 5m15s
release / android (push) Successful in 5m26s
release / Build signed APK (releases and dev) (push) Successful in 5m45s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 16s
release / Verify release artifacts (tag releases only) (push) Skipped
docs: library comments say what the scanner and resolver do, not how they came to be (#6277)
Rewrites the internal/library comment blocks to rule 32. Task, milestone,
decision and rule ids are gone; the incidents behind them are restated as
the constraint they left (why a file is deleted before its row, why the
missing mark is a mark, why duplicates block on duration). Counts and
album names from one library are dropped from production comments; test
comments keep the names their fixtures use. The tagReadVersion log keeps
each version's before and after, without the measurements.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-23 12:27:34 -04:00

82 lines
2.3 KiB
Go

package library
import (
"sync"
"github.com/jackc/pgx/v5/pgtype"
"git.fabledsword.com/bvandeusen/minstrel/internal/eventbus"
)
// Package-level bus pointer for scan lifecycle events. Set once
// at startup via SetEventBus; reads are nil-safe so test contexts that
// never call SetEventBus simply skip publishing.
//
// A package-global is the right shape here because:
//
// - There is exactly one bus per process (the same instance powers
// the SSE subscriber endpoint and every publisher across the codebase).
// - Threading it through RunScan + TryStartScan + the Scheduler +
// every test caller would touch ~10 call sites without changing
// behavior at the boundaries that don't care.
// - Other Go stdlib packages use the same pattern (log.SetDefault,
// etc.) for process-wide singletons.
var (
busMu sync.RWMutex
bus *eventbus.Bus
)
// SetEventBus wires the live-event bus into the library package so scan
// runs emit scan.run_started / scan.run_finished events. Call once during
// process startup; subsequent calls overwrite the previous bus.
func SetEventBus(b *eventbus.Bus) {
busMu.Lock()
defer busMu.Unlock()
bus = b
}
// publishScanEvent broadcasts a scan lifecycle event. No-op when no bus
// has been set. Scan events are always broadcast (empty UserID) because
// only admins see the scan status card; non-admin clients' invalidation
// dispatcher will receive the event but won't have any provider keyed on
// scan status, so the receive is harmless.
func publishScanEvent(kind string, runID pgtype.UUID, data map[string]any) {
busMu.RLock()
b := bus
busMu.RUnlock()
if b == nil {
return
}
payload := map[string]any{"run_id": uuidToBusString(runID)}
for k, v := range data {
payload[k] = v
}
b.Publish(eventbus.Event{
Kind: kind,
UserID: "", // broadcast
Data: payload,
})
}
// uuidToBusString renders a pgtype.UUID in canonical 8-4-4-4-12 form.
// Inlined to avoid a back-edge dependency on internal/api or another
// helper-bearing package.
func uuidToBusString(u pgtype.UUID) string {
if !u.Valid {
return ""
}
const hex = "0123456789abcdef"
out := make([]byte, 36)
pos := 0
for i, b := range u.Bytes {
if i == 4 || i == 6 || i == 8 || i == 10 {
out[pos] = '-'
pos++
}
out[pos] = hex[b>>4]
out[pos+1] = hex[b&0xf]
pos += 2
}
return string(out)
}