Build a Concurrent URL Health Checker CLI
A command-line tool that checks a list of URLs concurrently, follows redirects, records status and response time, retries transient failures, and prints a pass-fail summary.
Project goal
Build a CLI that checks many URLs concurrently with a bounded pool, reports status code and latency for each, retries transient errors with backoff, and exits non-zero when any check fails.
Final features
- Read URLs from arguments or a text file, one per line.
- Check all URLs concurrently with a configurable concurrency limit.
- Record status code and response time per URL and retry transient errors.
- Treat non-2xx responses and timeouts as failures with specific reasons.
- Print a table summary and exit non-zero when anything failed.
Architecture
An input parser yields a URL list; a checker performs one request with a timeout and classifies the outcome; a pool runs checks concurrently with a bound; and a reporter formats the results and computes the exit code. Retry logic wraps the single check so the pool stays simple.
Prerequisites
- Node.js 20 LTS or later
- Understanding of HTTP status classes and redirects
- Familiarity with concurrent task pools
Dependencies
- node >=20 — Global fetch, timers, and the CLI entry point
- vitest ^2.1.0 — Outcome classification and retry tests
Directory structure
health-check/
├── src/
│ ├── input.js
│ ├── check.js
│ ├── pool.js
│ ├── report.js
│ └── cli.js
├── test/
│ ├── check.test.js
│ └── pool.test.js
└── package.json
Phases
1. Input parsing
Collect URLs from arguments or a file, trim whitespace, and drop comments and blank lines.
Files: src/input.js
import { readFile } from 'node:fs/promises';
export async function loadUrls({ args = [], file }) {
const fromFile = file
? (await readFile(file, 'utf8')).split('\n').map((l) => l.trim())
: [];
const all = [...args, ...fromFile];
return all.filter((line) => line && !line.startsWith('#'));
}
export function validateUrl(url) {
try {
const parsed = new URL(url);
return parsed.protocol === 'http:' || parsed.protocol === 'https:';
} catch {
return false;
}
}Expected behavior: A file with blank lines and comments yields only the real URLs, and a non-http scheme fails validation.
2. Single check and classification
Perform one request with a timeout and classify the outcome as ok, client error, server error, timeout, or network failure.
Files: src/check.js
export function classify(status, latencyMs) {
const ok = status >= 200 && status < 300;
const retryable = status >= 500 || status === 429;
return { ok, status, latencyMs, retryable, reason: ok ? 'ok' : retryable ? 'server' : 'client' };
}
export async function checkOnce(url, { fetchImpl = fetch, timeoutMs = 5000 } = {}) {
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), timeoutMs);
const started = Date.now();
try {
const res = await fetchImpl(url, { redirect: 'follow', signal: controller.signal });
return classify(res.status, Date.now() - started);
} catch (err) {
return { url, ok: false, retryable: err.name === 'AbortError', reason: err.name === 'AbortError' ? 'timeout' : 'network', latencyMs: Date.now() - started };
} finally {
clearTimeout(timer);
}
}Expected behavior: A 200 classifies ok, a 503 classifies retryable server, and an aborted request classifies timeout with no exception escaping.
3. Retry wrapper
Retry only outcomes classified as retryable, with a small backoff and a hard attempt cap.
Files: src/retry.js
export async function withRetry(fn, { attempts = 3, backoff = (n) => 100 * n } = {}) {
let last;
for (let i = 1; i <= attempts; i++) {
last = await fn();
if (last.ok || !last.retryable) return last;
if (i < attempts) await new Promise((r) => setTimeout(r, backoff(i)));
}
return last;
}Expected behavior: A retryable failure is attempted up to the cap and a non-retryable failure stops after one attempt, both asserted with an injected clock.
4. Concurrent pool
Run checks concurrently up to a limit and collect every outcome in input order.
Files: src/pool.js
export async function runChecks(urls, { concurrency = 8, runner } = {}) {
const results = new Array(urls.length);
let next = 0;
async function worker() {
while (next < urls.length) {
const index = next++;
results[index] = await runner(urls[index]);
}
}
await Promise.all(Array.from({ length: Math.min(concurrency, urls.length) }, worker));
return results;
}Expected behavior: Results retain input order regardless of completion order, and at most concurrency runners are active at once.
5. Reporter and exit code
Format a readable table of results, summarise pass and fail counts, and derive the process exit code.
Files: src/report.js
export function summarize(results) {
const passed = results.filter((r) => r.ok).length;
return {
total: results.length,
passed,
failed: results.length - passed,
exitCode: passed === results.length ? 0 : 1,
};
}
export function formatTable(results) {
return results.map((r) => {
const status = r.ok ? 'OK ' : 'FAIL';
return status + ' ' + String(r.status ?? '-').padStart(3) + ' ' + String(r.latencyMs) + 'ms ' + (r.url ?? '');
}).join('\n');
}Expected behavior: A run where one URL fails yields exit code 1, and the table prints one aligned line per URL with status and latency.
6. CLI and tests
Wire input, pool, retry, and reporting together, then cover classification, pooling, and exit codes with tests.
Files: src/cli.js
import { loadUrls } from './input.js';
import { checkOnce } from './check.js';
import { withRetry } from './retry.js';
import { runChecks } from './pool.js';
import { formatTable, summarize } from './report.js';
const args = parse(process.argv.slice(2));
const urls = await loadUrls(args);
const results = await runChecks(urls, {
concurrency: Number(args.concurrency ?? 8),
runner: (url) => withRetry(() => checkOnce(url, { timeoutMs: Number(args.timeout ?? 5000) })),
});
console.log(formatTable(results));
const summary = summarize(results);
console.error('passed ' + summary.passed + '/' + summary.total);
process.exit(summary.exitCode);Expected behavior: Running against a healthy URL exits 0 and prints an OK line; a failing URL exits 1 and appears as FAIL in the table.
Testing
Run npx vitest. Classification tests assert that 200 is ok, 404 is a client failure, 500 is retryable, and a timeout is a failure with reason timeout. Pool tests assert that concurrency never exceeds the bound and that every URL is visited once. A report test asserts the exit code is non-zero when any outcome failed.
Troubleshooting
Checks take far longer than the timeouts suggest
Bound concurrency and abort each request on its own timer; without a per-request abort, one hanging server can stall the whole run.
A 404 causes pointless retries
Classify by status before retrying; only 5xx and 429 are transient, and retrying a permanent client error just slows the run.
Results come back in completion order, not input order
Pre-size the results array and write each outcome at its input index; the pool should preserve order even though completion does not.
Deployment
Run from a cron job or a CI smoke-test step and act on the exit code. Because concurrency and timeout are flags, the same binary serves a quick pre-deploy gate and a broader nightly sweep.
Known limitations
- Only status and latency are captured, not response content or TLS details.
- Redirects are followed silently, so a redirect loop that hits the timeout is reported as a timeout rather than a loop.
Project overview
Health checking a set of URLs is mostly about classification: what counts as healthy, what is retryable, and how long to wait before giving up. This project makes those rules explicit and testable by injecting the fetch function.
The checker runs bounded-concurrent requests, records latency, retries transient failures, and prints a summary that a cron job can act on through its exit code.
One check is a function that returns a classification rather than throwing, which keeps the pool free of exception handling:
export async function checkOnce(url, { fetchImpl = fetch, timeoutMs = 5000 } = {}) {
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), timeoutMs);
const started = Date.now();
try {
const res = await fetchImpl(url, { redirect: 'follow', signal: controller.signal });
const latencyMs = Date.now() - started;
return classify(res.status, latencyMs);
} catch (err) {
return { url, ok: false, reason: err.name === 'AbortError' ? 'timeout' : 'network', latencyMs: Date.now() - started };
} finally {
clearTimeout(timer);
}
}
Returning an outcome object instead of throwing means the pool never needs a try block and the report is a pure fold over outcomes.
Setup
The tool uses the global fetch available in modern Node so there is no HTTP client dependency to install or version.
| Dependency | Version | Why it is here |
|---|---|---|
| node | >=20 | Global fetch, timers, and the CLI entry point |
| vitest | ^2.1.0 | Outcome classification and retry tests |
Create the project and install:
mkdir health-check && cd health-check
npm init -y
npm install -D vitest@^2.1.0
Wire the CLI and tests in package.json:
{
"name": "health-check",
"type": "module",
"scripts": {
"check": "node src/cli.js",
"test": "vitest run"
}
}
Tests inject a fake fetch so classifications and retries are verified without any network access.
Phase 1: Input parsing
Collect URLs from arguments or a file, trim whitespace, and drop comments and blank lines. Each phase produces something demonstrable, so the tutorial never asks the reader to hold an unfinished design in their head.
import { readFile } from 'node:fs/promises';
export async function loadUrls({ args = [], file }) {
const fromFile = file
? (await readFile(file, 'utf8')).split('\n').map((l) => l.trim())
: [];
const all = [...args, ...fromFile];
return all.filter((line) => line && !line.startsWith('#'));
}
export function validateUrl(url) {
try {
const parsed = new URL(url);
return parsed.protocol === 'http:' || parsed.protocol === 'https:';
} catch {
return false;
}
}
Acceptance for this phase: A file with blank lines and comments yields only the real URLs, and a non-http scheme fails validation.
Phase 2: Single check and classification
Perform one request with a timeout and classify the outcome as ok, client error, server error, timeout, or network failure. Each phase produces something demonstrable, so the tutorial never asks the reader to hold an unfinished design in their head.
export function classify(status, latencyMs) {
const ok = status >= 200 && status < 300;
const retryable = status >= 500 || status === 429;
return { ok, status, latencyMs, retryable, reason: ok ? 'ok' : retryable ? 'server' : 'client' };
}
export async function checkOnce(url, { fetchImpl = fetch, timeoutMs = 5000 } = {}) {
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), timeoutMs);
const started = Date.now();
try {
const res = await fetchImpl(url, { redirect: 'follow', signal: controller.signal });
return classify(res.status, Date.now() - started);
} catch (err) {
return { url, ok: false, retryable: err.name === 'AbortError', reason: err.name === 'AbortError' ? 'timeout' : 'network', latencyMs: Date.now() - started };
} finally {
clearTimeout(timer);
}
}
Acceptance for this phase: A 200 classifies ok, a 503 classifies retryable server, and an aborted request classifies timeout with no exception escaping.
Phase 3: Retry wrapper
Retry only outcomes classified as retryable, with a small backoff and a hard attempt cap. Each phase produces something demonstrable, so the tutorial never asks the reader to hold an unfinished design in their head.
export async function withRetry(fn, { attempts = 3, backoff = (n) => 100 * n } = {}) {
let last;
for (let i = 1; i <= attempts; i++) {
last = await fn();
if (last.ok || !last.retryable) return last;
if (i < attempts) await new Promise((r) => setTimeout(r, backoff(i)));
}
return last;
}
Acceptance for this phase: A retryable failure is attempted up to the cap and a non-retryable failure stops after one attempt, both asserted with an injected clock.
Phase 4: Concurrent pool
Run checks concurrently up to a limit and collect every outcome in input order. Each phase produces something demonstrable, so the tutorial never asks the reader to hold an unfinished design in their head.
export async function runChecks(urls, { concurrency = 8, runner } = {}) {
const results = new Array(urls.length);
let next = 0;
async function worker() {
while (next < urls.length) {
const index = next++;
results[index] = await runner(urls[index]);
}
}
await Promise.all(Array.from({ length: Math.min(concurrency, urls.length) }, worker));
return results;
}
Acceptance for this phase: Results retain input order regardless of completion order, and at most concurrency runners are active at once.
Phase 5: Reporter and exit code
Format a readable table of results, summarise pass and fail counts, and derive the process exit code. Each phase produces something demonstrable, so the tutorial never asks the reader to hold an unfinished design in their head.
export function summarize(results) {
const passed = results.filter((r) => r.ok).length;
return {
total: results.length,
passed,
failed: results.length - passed,
exitCode: passed === results.length ? 0 : 1,
};
}
export function formatTable(results) {
return results.map((r) => {
const status = r.ok ? 'OK ' : 'FAIL';
return status + ' ' + String(r.status ?? '-').padStart(3) + ' ' + String(r.latencyMs) + 'ms ' + (r.url ?? '');
}).join('\n');
}
Acceptance for this phase: A run where one URL fails yields exit code 1, and the table prints one aligned line per URL with status and latency.
Phase 6: CLI and tests
Wire input, pool, retry, and reporting together, then cover classification, pooling, and exit codes with tests. Each phase produces something demonstrable, so the tutorial never asks the reader to hold an unfinished design in their head.
import { loadUrls } from './input.js';
import { checkOnce } from './check.js';
import { withRetry } from './retry.js';
import { runChecks } from './pool.js';
import { formatTable, summarize } from './report.js';
const args = parse(process.argv.slice(2));
const urls = await loadUrls(args);
const results = await runChecks(urls, {
concurrency: Number(args.concurrency ?? 8),
runner: (url) => withRetry(() => checkOnce(url, { timeoutMs: Number(args.timeout ?? 5000) })),
});
console.log(formatTable(results));
const summary = summarize(results);
console.error('passed ' + summary.passed + '/' + summary.total);
process.exit(summary.exitCode);
Acceptance for this phase: Running against a healthy URL exits 0 and prints an OK line; a failing URL exits 1 and appears as FAIL in the table.
Testing strategy
Run npx vitest. Classification tests assert that 200 is ok, 404 is a client failure, 500 is retryable, and a timeout is a failure with reason timeout. Pool tests assert that concurrency never exceeds the bound and that every URL is visited once. A report test asserts the exit code is non-zero when any outcome failed.
Retry classification is where the value lives: retrying a 404 wastes time, and not retrying a 503 hides a real outage, so both directions get a test.
Extending the project
Asserting response bodies, measuring time-to-first-byte, or checking TLS certificates are additional fields on the outcome. A YAML input file with per-URL expectations turns the tool into a smoke-test runner without changing the pool.