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.
This commit is contained in:
2026-10-16 10:35:30 +08:00
parent 8faf25b436
commit 73ad03fa59
49 changed files with 8668 additions and 1817 deletions
+213
View File
@@ -0,0 +1,213 @@
param(
[string]$ProjectCode = "default",
[string]$ResultCode = ("demo_full_" + (Get-Date -Format "yyyyMMdd_HHmmss")),
[string]$ResultType = "ndvi",
[string]$SourceSystemCode = "",
[string]$RequestedBy = "demo-user",
[string]$StyleCode = "demo_continuous_style",
[string]$TargetSystemCode = "demo_callback",
[switch]$StartStack,
[switch]$StartCallbackReceiver,
[switch]$KeepProcesses,
[int]$ApiPort = 28910,
[int]$TilePort = 28900,
[int]$CallbackPort = 28920,
[int]$ZoomMin = 0,
[int]$ZoomMax = 3
)
. (Join-Path $PSScriptRoot "common.ps1")
$stack = $null
$callback = $null
$run = New-RsRunContext -Prefix "full_flow"
try {
Import-RsDemoEnvironment
if ($StartStack) {
$stack = Start-RsDemoStack -ApiPort $ApiPort -TilePort $TilePort -IncludeWorker -RunID $run.RunID
}
$callbackUrl = ""
if ($StartCallbackReceiver) {
$callback = Start-RsCallbackReceiver -Port $CallbackPort -RunID ($run.RunID + "_callback")
$callbackUrl = $callback.CallbackURL
}
$inputPath = Join-Path $run.OutputDirectory ($ResultCode + ".tif")
& (Get-RsPythonExe) (Join-Path $PSScriptRoot "create_sample_raster.py") `
--output $inputPath `
--preset "ndvi" | Out-Null
if ($LASTEXITCODE -ne 0) {
throw "Failed to create sample raster"
}
$inspection = Invoke-RsApi -Method "POST" -Path "/api/v1/admin/raster-inspections" -Body @{
path = [System.IO.Path]::GetFullPath($inputPath)
}
$styleVersion = Ensure-RsStyleVersion -ProjectCode $ProjectCode -StyleCode $StyleCode -StyleName "Demo Continuous Style"
$project = Encode-RsURIComponent $ProjectCode
$importBody = @{
requested_by = $RequestedBy
asset = @{
source_ref = $inspection.storage_uri
asset_type = "raster"
display_name = $ResultCode
description = "Imported by demo_full_flow.ps1"
metadata = @{
demo_run = $run.RunID
}
}
asset_version = @{
format = $inspection.format
storage_uri = $inspection.storage_uri
mime_type = $inspection.mime_type
size_bytes = $inspection.size_bytes
crs = $inspection.crs
width = $inspection.width
height = $inspection.height
band_count = $inspection.band_count
dtype = $inspection.dtype
nodata_value = $inspection.nodata_value
bbox = $inspection.bbox
resolution_x = $inspection.resolution_x
resolution_y = $inspection.resolution_y
metadata = $inspection.metadata
}
product = @{
code = $ResultCode
type = $ResultType
name = $ResultCode
description = "Demo end-to-end result"
}
product_version = @{
value_type = "continuous"
unit = "index"
bbox = $inspection.bbox
status = "ready"
}
style_binding = @{
style_version_id = $styleVersion.id
is_default = $true
availability = "enabled"
}
}
if ($SourceSystemCode) {
$importBody.source_system_code = $SourceSystemCode
}
$import = Invoke-RsApi -Method "POST" -Path "/api/v1/projects/$project/imports/results" -Body $importBody
$slice = Invoke-RsApi -Method "POST" -Path ("/api/v1/results/{0}/slice" -f $import.product.product.id) -Body @{
requested_by = $RequestedBy
build_cog = $true
generate_preview = $true
generate_legend = $true
generate_tiles = $true
build_cog_input = @{
compression = "deflate"
block_size = 256
force_rebuild = $true
}
preview_input = @{
style_version_id = $styleVersion.id
width = 512
height = 512
format = "png"
force_rebuild = $true
}
legend_input = @{
style_version_id = $styleVersion.id
width = 320
height = 80
format = "png"
force_rebuild = $true
}
tiles_input = @{
style_version_id = $styleVersion.id
zoom_min = $ZoomMin
zoom_max = $ZoomMax
tile_size = 256
tile_format = "webp"
force_rebuild = $true
}
}
$sliceJobs = @()
foreach ($job in @($slice.jobs)) {
$sliceJobs += Wait-RsJob -JobID $job.id
}
if ($callbackUrl) {
Ensure-RsTargetSystem `
-ProjectCode $ProjectCode `
-Code $TargetSystemCode `
-Name "Demo Callback Receiver" `
-CallbackUrl $callbackUrl `
-CallbackMethod "POST" `
-TimeoutSeconds 30 `
-CreatedBy $RequestedBy `
-CallbackHeaders @{
"X-Demo-Mode" = "full-flow"
} | Out-Null
}
$pushBody = @{
requested_by = $RequestedBy
style_version_id = $styleVersion.id
include_artifacts = $true
}
if ($callbackUrl) {
$pushBody.target_system_code = $TargetSystemCode
} else {
$pushBody.callback_url = "http://127.0.0.1:8920/callback"
}
$push = Invoke-RsApi -Method "POST" -Path ("/api/v1/results/{0}/pushes" -f $import.product.product.id) -Body $pushBody
$pushJob = Wait-RsJob -JobID $push.job.id
$delivery = Invoke-RsApi -Method "GET" -Path ("/api/v1/projects/{0}/products/{1}/delivery?version=latest&style_version_id={2}" -f `
(Encode-RsURIComponent $ProjectCode), `
(Encode-RsURIComponent $ResultCode), `
(Encode-RsURIComponent $styleVersion.id))
$callbackRecord = $null
if ($callback) {
Start-Sleep -Seconds 1
$callbackRecord = Get-RsLatestCallbackRecord -OutputDirectory $callback.OutputDirectory
}
[ordered]@{
run_id = $run.RunID
api_base_url = Get-RsApiBaseUrl
tile_base_url = Get-RsTileBaseUrl
callback_url = if ($callback) { $callback.CallbackURL } else { $push.callback_url }
result_id = $import.product.product.id
result_code = $ResultCode
product_version_id = $import.product_version.id
style_version_id = $styleVersion.id
slice_job_ids = @($sliceJobs | ForEach-Object { $_.id })
push_job_id = $push.job.id
push_job_status = $pushJob.status
cog_url = $delivery.downloads.cog_url
preview_url = if ($delivery.preview) { $delivery.preview.url } else { $null }
legend_url = if ($delivery.legend) { $delivery.legend.url } else { $null }
tileset_url = if ($delivery.tileset) { $delivery.tileset.url_template } else { $null }
callback_record_path = if ($callbackRecord) { $callbackRecord.Path } else { $null }
run_directory = $run.RunDirectory
log_directory = if ($stack) { $stack.LogDirectory } else { $null }
} | ConvertTo-Json -Depth 16
}
finally {
if (-not $KeepProcesses) {
if ($callback) {
Stop-RsBackgroundProcesses -Processes @($callback.Process)
}
if ($stack) {
Stop-RsBackgroundProcesses -Processes $stack.Processes
}
}
}