Files
map-asset-gateway/services/api-go/internal/basemap/workers.go
T
Harmon 747eed63b0 Basemap Pusher: catalog and distribution gateway for existing tiles
A Go service that scans existing basemap tile directories and vector files,
catalogues them in SQLite, issues scoped access tokens, and serves tiles and
downloads to downstream systems. It deliberately does not generate or process
raster data: GIS software produces the tiles, this service catalogues,
authorises and distributes them.

Capabilities
- scans basemap tile directories (manifest-based or raw z/x/y) and vector
  assets (.geojson, .json, .zip, shapefiles with sidecars)
- token auth with per-resource grants, expiry and coarse scopes (read, slice)
- resource health inspection flags a moved or deleted tile root before a client
  hits a 404
- downstream push with exponential-backoff retries and manual re-queue
- image slicing jobs driven by the GDAL bundled with QGIS, including VRT
  mosaicking so successive drone flights accumulate into one seamless layer
- a narrow, non-admin slicing API for external callers: submit, poll,
  idempotent by job code, completion callback
- ops console covering health, ledger, tokens, pushes, scans, slicing and
  service info
- native Windows service install/start/stop with crash recovery, auto-start and
  a rotating log file, so no NSSM or WinSW wrapper is needed

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

99 lines
2.2 KiB
Go

package basemap
import (
"context"
"log"
"time"
)
const (
// pushRetryInterval is how often due retries are looked for.
pushRetryInterval = 15 * time.Second
// tileJobInterval is how often the queue is checked for new jobs.
tileJobInterval = 2 * time.Second
)
// StartBackgroundWorkers launches the periodic maintenance loops: resource
// health inspection, push retries and the tile slicing queue. Close stops them.
// Calling it more than once has no additional effect.
func (s *Store) StartBackgroundWorkers() {
s.bgOnce.Do(func() {
ctx, cancel := context.WithCancel(context.Background())
s.bgCtx = ctx
s.bgCancel = cancel
s.bgWG.Add(1)
go func() {
defer s.bgWG.Done()
s.healthLoop(ctx)
}()
s.bgWG.Add(1)
go func() {
defer s.bgWG.Done()
s.pushRetryLoop(ctx)
}()
s.bgWG.Add(1)
go func() {
defer s.bgWG.Done()
s.tileJobLoop(ctx)
}()
})
}
func (s *Store) healthLoop(ctx context.Context) {
// Prime the catalog immediately so a resource that vanished since the last
// scan shows up without waiting a full interval.
if _, err := s.CheckHealth(ctx); err != nil && ctx.Err() == nil {
log.Printf("health check failed: %v", err)
}
interval := s.cfg.HealthCheckInterval
if interval <= 0 {
return
}
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
if _, err := s.CheckHealth(ctx); err != nil && ctx.Err() == nil {
log.Printf("health check failed: %v", err)
}
}
}
}
func (s *Store) pushRetryLoop(ctx context.Context) {
ticker := time.NewTicker(pushRetryInterval)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
if err := s.dispatchPushRetries(ctx); err != nil && ctx.Err() == nil {
log.Printf("push retry dispatch failed: %v", err)
}
if err := s.dispatchTileJobCallbacks(ctx); err != nil && ctx.Err() == nil {
log.Printf("tile job callback dispatch failed: %v", err)
}
}
}
}
func (s *Store) tileJobLoop(ctx context.Context) {
ticker := time.NewTicker(tileJobInterval)
defer ticker.Stop()
for {
s.dispatchTileJobs(ctx)
select {
case <-ctx.Done():
return
case <-ticker.C:
}
}
}