feat(sense): add reconciliation safety controls [T-012]
This commit is contained in:
+38
-10
@@ -15,8 +15,10 @@ import (
|
||||
"yovision/sense/internal/auth"
|
||||
"yovision/sense/internal/config"
|
||||
"yovision/sense/internal/controlapi"
|
||||
"yovision/sense/internal/metrics"
|
||||
"yovision/sense/internal/mtx"
|
||||
"yovision/sense/internal/onvif"
|
||||
"yovision/sense/internal/orphan"
|
||||
"yovision/sense/internal/probe"
|
||||
"yovision/sense/internal/reconcile"
|
||||
"yovision/sense/internal/store"
|
||||
@@ -37,6 +39,14 @@ func run(logger *slog.Logger) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("load configuration: %w", err)
|
||||
}
|
||||
instanceID := cfg.InstanceID
|
||||
if instanceID == "" {
|
||||
instanceID, err = metrics.GenerateInstanceID()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
registry := metrics.New(instanceID, version)
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
@@ -76,22 +86,36 @@ func run(logger *slog.Logger) error {
|
||||
})
|
||||
}
|
||||
discovery := onvif.NewRouter(cameraAdapter, credentials)
|
||||
reconciler := reconcile.New(repository, discovery, mediaClient)
|
||||
reconciler := reconcile.NewWithOptions(repository, discovery, mediaClient, reconcile.Options{
|
||||
InstanceID: instanceID, LeaseDuration: cfg.ReconcileLeaseDuration,
|
||||
OperationTimeout: cfg.ReconcileOperationTimeout, Metrics: registry,
|
||||
})
|
||||
checker := probe.New(repository, mediaClient)
|
||||
var orphanScanner *orphan.Manager
|
||||
if cfg.OrphanScanEnabled {
|
||||
orphanStore, ok := repository.(store.OrphanRepository)
|
||||
if !ok {
|
||||
return errors.New("selected repository does not support orphan scanning")
|
||||
}
|
||||
orphanScanner = orphan.New(orphanStore, mediaClient, instanceID, registry)
|
||||
}
|
||||
report := func(err error) {
|
||||
// Domain and MediaMTX errors intentionally omit stream URIs and credentials.
|
||||
logger.Warn("background convergence error", "error", err)
|
||||
}
|
||||
var background sync.WaitGroup
|
||||
background.Add(2)
|
||||
go func() {
|
||||
defer background.Done()
|
||||
reconciler.Run(ctx, cfg.ReconcileInterval, report)
|
||||
}()
|
||||
go func() {
|
||||
defer background.Done()
|
||||
checker.Run(ctx, cfg.ProbeInterval, report)
|
||||
}()
|
||||
startBackground := func(run func()) {
|
||||
background.Add(1)
|
||||
go func() {
|
||||
defer background.Done()
|
||||
run()
|
||||
}()
|
||||
}
|
||||
startBackground(func() { reconciler.Run(ctx, cfg.ReconcileInterval, report) })
|
||||
startBackground(func() { checker.Run(ctx, cfg.ProbeInterval, report) })
|
||||
if orphanScanner != nil {
|
||||
startBackground(func() { orphanScanner.Run(ctx, cfg.OrphanScanInterval, report) })
|
||||
}
|
||||
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("GET /healthz", func(writer http.ResponseWriter, _ *http.Request) {
|
||||
@@ -104,6 +128,9 @@ func run(logger *slog.Logger) error {
|
||||
writer.WriteHeader(http.StatusOK)
|
||||
_, _ = writer.Write([]byte(`{"status":"ready"}`))
|
||||
})
|
||||
if cfg.MetricsEnabled {
|
||||
mux.Handle("GET /metrics", registry.Handler())
|
||||
}
|
||||
if cfg.ControlAPIEnabled {
|
||||
mux.Handle("/api/v1/", controlHandler)
|
||||
}
|
||||
@@ -118,6 +145,7 @@ func run(logger *slog.Logger) error {
|
||||
serverErrors := make(chan error, 1)
|
||||
go func() {
|
||||
logger.Info("Sense listening", "address", cfg.HTTPAddress, "version", version,
|
||||
"instance_id", instanceID,
|
||||
"control_api_enabled", cfg.ControlAPIEnabled)
|
||||
serverErrors <- server.ListenAndServe()
|
||||
}()
|
||||
|
||||
Reference in New Issue
Block a user