I'm Eric Gitangu. I work on backend systems for a mobility/fintech platform. This one is about a rule I like, "no default for a required setting", and a design question that comes with it: when a job refuses to start under that rule, who hears it, and what do they learn?
The rule
Some settings must never have a fallback. If the value is not there, the process stops with a line that names the variable, never its value. On the command line it exits with code 78, the conventional sysexits code for a configuration error.
I would keep that rule. A job that carries on with a placeholder does work nobody asked for.
The design question
Scheduled jobs often do not inherit the web server's environment. One answer is a small loader that runs first on the command line and reads a settings file into the process.
I want that loader to be lenient. If it cannot read the file, it does not stop the job. The loader is not the place to decide that a job cannot run.
Put a lenient loader under a strict reader and information gets lost. "Required setting is not set" can be true when the key is absent, when the file is missing, or when the file is there and this user cannot read it. And a line in a job log only helps someone who is reading that log.
The decision: lenient loader, strict reader, loud refusal
The design keeps the loader lenient and gives the setting no default. It adds information passing from one layer to the next, and a signal that leaves the process.
1. The loader records why it read nothing. It still never throws. It keeps one of two short reasons: the file is missing, or the file is unreadable. No path, no contents.
export function loadEnvFile(path: string, env: Record<string, string | undefined>): LoadProblem {
try {
accessSync(path, constants.R_OK);
} catch {
return existsSync(path) ? 'file_unreadable' : 'file_missing';
}
// ...parse the file
return '';
}2. The reader names the cause. When a required value is absent, it asks the loader what happened. There are three answers: the key really is absent, the file is missing, or the file is unreadable. The last two add a sentence to the log line, and for the unreadable case that sentence says to look at the file's owner and mode. Three causes, three different fixes.
3. The refusal leaves a metric behind. A process that is about to exit cannot wait around to be scraped. On the command line it writes a small text file that the host's metrics agent already knows how to collect:
const labels = `key="${key}",reason="${reason}"`;
const body = // "# TYPE" lines omitted here
`job_config_refusal{${labels}} 1\n` +
`job_config_refusal_timestamp_seconds{${labels}} ${nowSec}\n`;
writeFileSync(tmp, body);
chmodSync(tmp, 0o644);
renameSync(tmp, file);Four details matter more than the code suggests:
- Names only. The labels carry the variable name and the reason. The name is checked against a strict pattern before it goes into a label. A value has no way in.
- Atomic. Write to a temporary file, then rename. A collector never reads half a file.
- Readable. The mode is set on purpose, because the collector usually runs as a different user. A refusal about an unreadable file that writes an unreadable metric would be a poor joke.
- Best effort. If the metrics directory cannot be written, nothing changes: same log line, same exit code. Telemetry must not alter the behaviour it reports.
The alert that clears itself
A text file stays on disk after the problem is fixed. A rule that fires on refusal == 1 would fire for good, and an alert that never resolves gets muted.
So the second series is the time of the refusal, and the rule only looks at recent ones:
expr: job_config_refusal == 1
and (time() - job_config_refusal_timestamp_seconds) < 3600
for: 0m
labels:
severity: warningThe rule has no hold period, so it fires on the first evaluation that sees a recent refusal. If it stops repeating, the alert goes quiet an hour later, with no cleanup step on the success path. The alert text says what each reason means and what to do next.
Testing the thing that only happens when something is broken
The tests run the real configuration loading in a fresh process, because the behaviour is about process start and exit:
- settings file readable, key absent: exit 78, reason "key absent", metric written;
- no settings file: reason "file missing";
- file present with no read permission: reason "unreadable", and an assertion that the value appears in neither the log line nor the metric (skipped when the tests run as root, which can read anything);
- healthy start: no refusal metric at all.
The alert rule has its own tests with synthetic series: it is firing 20 minutes after the refusal and quiet 70 minutes after it. A time window in a query is logic, and logic gets a test.
Limits of the pattern
- Quiet is not fixed. The window buys an alert that resolves by itself. The price is that a job that runs once a day alerts for an hour and then goes quiet while still broken. "Why did it stop" and "has it succeeded lately" are different questions, and the second needs its own check on the last good run.
- One file, latest refusal wins. With a single metric file, a later refusal replaces an earlier one. Separating them means a file and a job label per job, and more series to manage.
- A coarse reason. Three values will not explain every failure. They separate cases that need different fixes.
Options I would not take
- Fire on the refusal flag alone. The file outlives the problem, so the alert never resolves.
- A default for the setting. The job runs, and runs wrong.
- A strict loader. It turns every read problem into a stop at the lowest layer, before the code that knows what is required has had a say.
- A better log line alone. It helps the diagnosis and still depends on someone looking.
Try it
A runnable version of the pattern, with a self-test and the alert rule, is in this gist.
Also on dev.to.