Files
core/internal/api/server.go
Michael Chus 4c284505a8 Async ingest, deferred history, batch delete, vendor normalization, CI identifiers
- history/worker: fix deadlock by moving stale job requeue out of claimNextJob
  into dedicated staleJobRequeuer goroutine (runs every 2 min)
- history/service,tx_apply,cross_entity: add deferred=true mode — write events+
  snapshots but skip projection updates; queue recompute after commit
- ingest/service: IngestHardwareDeferred uses deferred mode; CSV workers up to 8
  (INGEST_CSV_WORKERS env); serial/prefetch lookups use normalize.SerialKey
- api/ingest: JSON /ingest/hardware now async (202 + job_id); new GET
  /ingest/hardware/jobs/{id} endpoint; CSV already async
- history/admin_cancel: replace per-event softDelete loop with batchSoftDeleteEvents
  using IN-clause chunks of 500 to prevent request timeout on large deletes
- normalize: new internal/normalize package with VendorKey, VendorDisplay,
  VendorDisplayPtr, SerialKey, FirmwareKey
- ingest/parser_hardware: vendor fields use normalize.VendorDisplayPtr
- migrations/0021_ci_identifiers: change identifier columns to utf8mb4_unicode_ci
  (case-insensitive) in parts, machines, observations, machine_firmware_states
- bible submodule: update to add identifier-normalization contract

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-01 22:23:17 +03:00

142 lines
3.8 KiB
Go

package api
import (
"context"
"database/sql"
"encoding/json"
"log"
"net/http"
"runtime/debug"
"time"
"reanimator/internal/history"
"reanimator/internal/ingest"
"reanimator/internal/repository/failures"
"reanimator/internal/repository/registry"
"reanimator/internal/repository/timeline"
)
type Server struct {
httpServer *http.Server
cancelBg context.CancelFunc
}
type statusCaptureResponseWriter struct {
http.ResponseWriter
status int
}
func (w *statusCaptureResponseWriter) WriteHeader(status int) {
w.status = status
w.ResponseWriter.WriteHeader(status)
}
func (w *statusCaptureResponseWriter) Write(b []byte) (int, error) {
if w.status == 0 {
w.status = http.StatusOK
}
return w.ResponseWriter.Write(b)
}
func withErrorLogging(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
sw := &statusCaptureResponseWriter{ResponseWriter: w}
start := time.Now()
defer func() {
if rec := recover(); rec != nil {
log.Printf("http panic method=%s path=%s remote=%s duration_ms=%d panic=%v\nstack=%s", r.Method, r.URL.Path, r.RemoteAddr, time.Since(start).Milliseconds(), rec, debug.Stack())
if sw.status == 0 {
writeJSON(sw, http.StatusInternalServerError, map[string]string{"error": "internal server error"})
}
return
}
status := sw.status
if status == 0 {
status = http.StatusOK
}
if status >= 400 {
log.Printf("http response error method=%s path=%s raw_query=%q status=%d remote=%s duration_ms=%d", r.Method, r.URL.Path, r.URL.RawQuery, status, r.RemoteAddr, time.Since(start).Milliseconds())
}
}()
next.ServeHTTP(sw, r)
})
}
func NewServer(addr string, readTimeout, writeTimeout time.Duration, db *sql.DB) *Server {
mux := http.NewServeMux()
mux.HandleFunc("/health", healthHandler)
cancelBg := func() {}
if db != nil {
bgCtx, cancel := context.WithCancel(context.Background())
cancelBg = cancel
failureRepo := failures.NewFailureRepository(db)
assetRepo := registry.NewAssetRepository(db)
componentRepo := registry.NewComponentRepository(db)
installationRepo := registry.NewInstallationRepository(db)
timelineRepo := timeline.NewEventRepository(db)
historySvc := history.NewService(db)
historySvc.StartWorker(bgCtx)
RegisterRegistryRoutes(mux, RegistryDependencies{
Assets: assetRepo,
Components: componentRepo,
History: historySvc,
})
RegisterHistoryRoutes(mux, HistoryDependencies{
Service: historySvc,
})
RegisterIngestRoutes(mux, IngestDependencies{
Service: ingest.NewService(db),
})
RegisterAssetComponentRoutes(mux, AssetComponentDependencies{
Assets: assetRepo,
Components: componentRepo,
Installations: installationRepo,
Timeline: timelineRepo,
History: historySvc,
})
RegisterFailureRoutes(mux, FailureDependencies{
Failures: failureRepo,
Assets: assetRepo,
Components: componentRepo,
Installations: installationRepo,
History: historySvc,
})
RegisterUIRoutes(mux, UIDependencies{
Assets: assetRepo,
Components: componentRepo,
Installations: installationRepo,
Timeline: timelineRepo,
Failures: failureRepo,
})
}
return &Server{
httpServer: &http.Server{
Addr: addr,
Handler: withErrorLogging(mux),
ReadTimeout: readTimeout,
WriteTimeout: writeTimeout,
},
cancelBg: cancelBg,
}
}
func (s *Server) Start() error {
return s.httpServer.ListenAndServe()
}
func (s *Server) Shutdown(ctx context.Context) error {
if s.cancelBg != nil {
s.cancelBg()
}
return s.httpServer.Shutdown(ctx)
}
func healthHandler(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
}