Files
TREK/server/src/nest/paddle-obstacles/import-runner.ts
T
Maurice 1bc82f97fd feat(tours): route paddle tours on the water, shaping points and auto-loaded regions
Paddle tours are planned on the water by TREK's own router: a water network
per paddle region, built from the same Geofabrik extract as the obstacles and
searched in a worker thread of the server (no sidecar). Rivers, canals and
lakes (direct or along the shore), portages round weirs, locks by policy,
whitewater by boat, gaps marked. Found portages and locks become points of
their own; an unplannable leg says why and offers a way out.

- Shaping points: a click on the line sets a point that steers the route
  without being a stop (hikes and paddle tours); recompute a single leg.
- Lake style direct/shore, suggested from the wind forecast.
- Regions load by themselves where a paddler plans (admin setting: automatic,
  on request, off; size cap and a daily count), with requests for the admins,
  a notification, and the load followed on the leg.
- Portage from the last point, portages follow footways via the walking
  router (or stay straight), "put in here" on slipways and put-ins.
- MCP plan_paddle_route with the REST route's rules.
2026-10-18 21:21:32 +00:00

98 lines
3.5 KiB
TypeScript

import { type ImportOutcome, type ImportPhase, importExtract } from './paddle-import';
import { existsSync } from 'node:fs';
import { join } from 'node:path';
import { Worker } from 'node:worker_threads';
/** Reads a region's extract; in a thread of its own where the build ships the script, else on the calling thread. */
export interface ImportRunner {
run(
path: string,
regionId: string,
onProgress: (phase: ImportPhase, fraction: number | null) => void,
signal: AbortSignal,
): Promise<ImportOutcome>;
}
export const PADDLE_IMPORT_RUNNER = Symbol('PADDLE_IMPORT_RUNNER');
/** The thread's heap: a large region's read builds its network in this much, and fails cleanly when it does not fit. */
const IMPORT_HEAP_MB = 1536;
export class InlineImportRunner implements ImportRunner {
async run(
path: string,
regionId: string,
onProgress: (phase: ImportPhase, fraction: number | null) => void,
signal: AbortSignal,
): Promise<ImportOutcome> {
const outcome = await importExtract(path, regionId, onProgress);
signal.throwIfAborted();
return outcome;
}
}
/**
* The read in a worker thread: the server's thread stays free while a large
* region takes minutes, and a read that runs out of memory ends its thread,
* not the server. Calling it off ends the thread at once.
*/
export class WorkerImportRunner implements ImportRunner {
constructor(private readonly entry: string) {}
async run(
path: string,
regionId: string,
onProgress: (phase: ImportPhase, fraction: number | null) => void,
signal: AbortSignal,
): Promise<ImportOutcome> {
signal.throwIfAborted();
const execArgv = process.execArgv.filter(
(arg, index, all) =>
arg === '--require' || arg === '-r' || all[index - 1] === '--require' || all[index - 1] === '-r',
);
const worker = new Worker(this.entry, {
workerData: { path, regionId },
execArgv,
resourceLimits: { maxOldGenerationSizeMb: IMPORT_HEAP_MB },
});
return await new Promise<ImportOutcome>((resolve, reject) => {
let settled = false;
const finish = (action: () => void) => {
if (settled) return;
settled = true;
signal.removeEventListener('abort', abort);
action();
void worker.terminate();
};
const abort = () =>
finish(() => reject(signal.reason instanceof Error ? signal.reason : new Error(String(signal.reason))));
signal.addEventListener('abort', abort);
worker.on(
'message',
(message: {
progress?: { phase: ImportPhase; fraction: number | null };
outcome?: ImportOutcome;
error?: string;
}) => {
if (message.progress) onProgress(message.progress.phase, message.progress.fraction);
else if (message.outcome) {
const outcome = message.outcome;
if (outcome.graph) outcome.graph = { ...outcome.graph, bytes: new Uint8Array(outcome.graph.bytes) };
finish(() => resolve(outcome));
} else finish(() => reject(new Error(message.error ?? 'the read failed')));
},
);
worker.on('error', (err) => finish(() => reject(err)));
worker.on('exit', (code) =>
finish(() => reject(new Error(`the read ended early (${code}); a smaller region may fit`))),
);
});
}
}
export function importRunner(): ImportRunner {
const entry = join(__dirname, 'paddle-import.worker.js');
return existsSync(entry) ? new WorkerImportRunner(entry) : new InlineImportRunner();
}