Adds speedtest pg-boss worker with injectable runner for testing, hourly cron enqueue, and /api/speedtest/history (GET) + /run (POST, owner-only) routes. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
24 lines
833 B
JavaScript
24 lines
833 B
JavaScript
import { execFile } from 'node:child_process';
|
|
import { promisify } from 'node:util';
|
|
import * as repo from '../../db/repos/speedtest.js';
|
|
import { log } from '../../log.js';
|
|
const pexec = promisify(execFile);
|
|
|
|
export const NAME = 'speedtest';
|
|
|
|
// Default runner uses speedtest-cli --json (bits/s → Mbps). Swap binary/flags
|
|
// here if the box has the Ookla `speedtest -f json` CLI instead.
|
|
async function defaultRunner() {
|
|
const { stdout } = await pexec('speedtest-cli', ['--json'], { timeout: 120000 });
|
|
const j = JSON.parse(stdout);
|
|
return { down_mbps: j.download / 1e6, up_mbps: j.upload / 1e6, ping_ms: j.ping };
|
|
}
|
|
let runner = defaultRunner;
|
|
export function _setRunner(fn) { runner = fn; }
|
|
|
|
export async function handler(_job) {
|
|
const r = await runner();
|
|
await repo.record(r);
|
|
log.info(r, 'speedtest recorded');
|
|
}
|