Files
map-asset-gateway/services/api-go/internal/basemap/health.go
T
Harmon 73ad03fa59 Rewrite the gateway around health, retries, slicing and a Windows service
Scanner
- manifest lookup is now depth-bounded and prunes numeric zoom directories, so
  a scan no longer walks the whole tile payload
- rescans only report a version as updated when something actually changed
- overlapping runs are refused instead of interleaving their bookkeeping

Hot path
- token authorisation loads only that token grants and throttles the
  last_used_at write instead of touching it on every tile
- SQLite runs in WAL with a busy timeout and a real connection pool
- the catalog lookup used by /tiles no longer loads every basemap

Security
- the admin surface requires ADMIN_TOKEN (bearer) or ADMIN_USER/ADMIN_PASSWORD
  (basic), and the service refuses to bind off-host without credentials
- CORS is an explicit allow-list; newly minted tokens never reach a redirect URL
- client tokens are redacted from the access log

Operations
- resource health inspection flags a moved or deleted tile root before a 404
- pushes retry with backoff and can be re-queued by hand; the schedule lives in
  the database so a restart does not lose it
- access log plus /metrics counters

Image slicing
- tile_jobs drive the GDAL bundled with QGIS via scripts/gdal2tiles-qgis.bat
- layer_sources remembers what a layer is made of: submit one source to append
  a flight, or all of them to rebuild the layer as a VRT mosaic with no tile
  seams; the console exposes both, including a one-click rebuild
- slicing is available to integrations through a narrow slice token scope
  (submit, poll, idempotent by code, completion callback)

Console
- rebuilt around a fixed rail and single-view navigation with health and
  outstanding work leading, instead of one long scrolling page

Windows service
- native install/start/stop/status with crash recovery, auto-start and a
  rotating log file, so no NSSM wrapper is needed
- start-all.bat and stop-all.bat are removed: they killed the process by port
  with taskkill, which fought the service restart policy

Verified with 49 tests and live runs against real tile data and real GDAL.
2026-10-16 10:35:30 +08:00

156 lines
4.1 KiB
Go

package basemap
import (
"context"
"errors"
"os"
"time"
)
// Availability verdicts recorded on catalog rows.
const (
healthOK = "ok"
healthMissing = "missing"
healthUnknown = "unknown"
)
// HealthReport summarizes one inspection pass.
type HealthReport struct {
CheckedAt time.Time `json:"checked_at"`
VersionsChecked int `json:"versions_checked"`
VersionsMissing int `json:"versions_missing"`
VectorsChecked int `json:"vectors_checked"`
VectorsMissing int `json:"vectors_missing"`
}
// CheckHealth stats every catalogued path and records whether it is still
// there, so a directory that was moved or deleted surfaces before a client
// hits a 404. It shares scanMu with the scanners because both rewrite catalog
// rows.
func (s *Store) CheckHealth(ctx context.Context) (HealthReport, error) {
s.scanMu.Lock()
defer s.scanMu.Unlock()
checkedAt := nowUTC()
report := HealthReport{CheckedAt: checkedAt}
type verdict struct {
id string
health string
detail string
}
versions, err := s.collectHealth(ctx, `SELECT id, tile_root_path FROM basemap_versions`, true)
if err != nil {
return report, err
}
vectors, err := s.collectHealth(ctx, `SELECT id, file_path FROM vector_assets`, false)
if err != nil {
return report, err
}
tx, err := s.db.BeginTx(ctx, nil)
if err != nil {
return report, err
}
defer tx.Rollback()
stamp := toRFC3339(checkedAt)
for _, item := range versions {
if _, err := tx.ExecContext(ctx, `
UPDATE basemap_versions SET health = ?, health_checked_at = ?, health_detail = ? WHERE id = ?
`, item.health, stamp, item.detail, item.id); err != nil {
return report, err
}
report.VersionsChecked++
if item.health != healthOK {
report.VersionsMissing++
}
}
for _, item := range vectors {
if _, err := tx.ExecContext(ctx, `
UPDATE vector_assets SET health = ?, health_checked_at = ?, health_detail = ? WHERE id = ?
`, item.health, stamp, item.detail, item.id); err != nil {
return report, err
}
report.VectorsChecked++
if item.health != healthOK {
report.VectorsMissing++
}
}
if err := tx.Commit(); err != nil {
return report, err
}
return report, nil
}
type healthVerdict struct {
id string
health string
detail string
}
func (s *Store) collectHealth(ctx context.Context, query string, wantDir bool) ([]healthVerdict, error) {
rows, err := s.db.QueryContext(ctx, query)
if err != nil {
return nil, err
}
defer rows.Close()
var verdicts []healthVerdict
for rows.Next() {
var id, path string
if err := rows.Scan(&id, &path); err != nil {
return nil, err
}
health, detail := inspectPath(path, wantDir)
verdicts = append(verdicts, healthVerdict{id: id, health: health, detail: detail})
}
return verdicts, rows.Err()
}
func inspectPath(path string, wantDir bool) (string, string) {
info, err := os.Stat(path)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return healthMissing, "路径不存在"
}
return healthUnknown, err.Error()
}
if wantDir && !info.IsDir() {
return healthMissing, "路径不是目录"
}
if !wantDir && info.IsDir() {
return healthMissing, "路径是目录"
}
return healthOK, ""
}
// The console reads health through these maps so the row scanners on the tile
// request path stay untouched.
func (s *Store) loadVersionHealth(ctx context.Context) (map[string]ResourceHealth, error) {
return s.loadHealth(ctx, `SELECT id, health, health_detail, health_checked_at FROM basemap_versions`)
}
func (s *Store) loadVectorHealth(ctx context.Context) (map[string]ResourceHealth, error) {
return s.loadHealth(ctx, `SELECT id, health, health_detail, health_checked_at FROM vector_assets`)
}
func (s *Store) loadHealth(ctx context.Context, query string) (map[string]ResourceHealth, error) {
rows, err := s.db.QueryContext(ctx, query)
if err != nil {
return nil, err
}
defer rows.Close()
result := map[string]ResourceHealth{}
for rows.Next() {
var id, health, detail, checkedAt string
if err := rows.Scan(&id, &health, &detail, &checkedAt); err != nil {
return nil, err
}
result[id] = ResourceHealth{Health: health, Detail: detail, CheckedAt: parseTime(checkedAt)}
}
return result, rows.Err()
}