Writing · Reliability
Old, shadow, new: replacing a job nobody understands
Old scheduled jobs that had run blind for years, and no safe way to touch them. I gave each one a switch at the top of its file, so the rewrite could run beside the original on live data before it ever took over. Here is how the switch works, why the shadow must be unable to write, and how a job goes live.
Every business that has been around a while has them. Scheduled jobs that run every few minutes, written years ago by someone who has since left. They leave no useful record of what they did. Nobody can say exactly what they do, only that something breaks when they stop.
I had a whole set of these to replace. Rewriting them was the easy part. I could read each old script, work out what it really did, and write a clean version with logging and tests. The hard part was the moment of switching over. A rewrite that passes its tests still has to prove it does the same thing as the old job on real data, and the old job never wrote down what it did.
So I stopped thinking about the rewrite and started thinking about a switch.
Three lines at the top of the old file
The schedule never changes. It keeps calling the old file every few minutes, as it always has. What changes is the top of that file. Three lines ask a database row what should happen this time:
<?php
/* SWITCH: flip this job from the switchboard */
require_once '/path/to/switch/cron-switch.php'; CronSwitch::gate(__FILE__);
/* END SWITCH */
require 'db.php'; // the old script, untouched, from here down
That row holds one of four positions.
| Position | What runs |
|---|---|
| Old | The old script, exactly as it ran before. This is the default, and the way back. |
| Shadow | The rewrite runs first and writes nothing. Then the old script does the real work. |
| New | The rewrite does the work. The old script never runs. |
| Off | Neither. A stop button for one job. |
A flip takes effect the next time the job runs. Nothing restarts, and nobody edits the schedule. A small switchboard page shows every job, its position, and every run it has had, with what that run printed. Going back is the same button, the other way.
Shadow is the whole point
Shadow is where the trust comes from. On the same run, against the same live data, both versions go. The old one does the real work, as it always has. The rewrite does everything except write: it reads the same rows, makes the same decisions, builds the same payloads, and records what it would have done.
The old job’s side gets recorded too, so there is something to compare against. Then a comparison gives one verdict per item: a match, different values, one acted and the other didn’t, or only one of them saw it at all.
When a job has gone a few days with nothing but matches, I know something no test could have told me. The rewrite agrees with the old job on real data, including the cases nobody ever wrote down.
One rule came out of this the hard way. A comparison that saw nothing is not a match. A shadow that compares nothing passes forever, so every comparison has to show it actually saw rows.
A shadow that can write is not a shadow
Shadow only works if the rewrite truly cannot write. If it leaks one write, it has changed the data the old job is about to use, and the comparison is spoiled.
The usual way to stop a script writing is a dry-run flag:
if (! $dryRun) {
$db->execute('UPDATE subscribers SET status = ? WHERE id = ?', [$status, $id]);
}
That works until someone adds a write and forgets the if. The flag is a promise the code has to keep at every write, forever, no matter who edits the file next.
So in shadow I don’t hand the rewrite a flag. I hand it a different database connection, one whose write methods can only refuse.
final class ReadOnlyConnection implements Connection
{
public function __construct(private Connection $real) {}
public function fetchAll(string $sql, array $bindings = []): array
{
$this->mustBeRead($sql);
return $this->real->fetchAll($sql, $bindings);
}
public function execute(string $sql, array $bindings = []): int
{
throw new WriteRefused('shadow: refused ' . substr($sql, 0, 120));
}
public function insert(string $sql, array $bindings = []): int
{
throw new WriteRefused('shadow: refused ' . substr($sql, 0, 120));
}
}
The choice between the real connection and this one is made once, when the job starts. After that, the job’s own code cannot write through it, however that code is arranged. That is what I mean by a dry run being a type, not a flag. The job cannot forget to check something, because the write path is not there.
Reads still go through, and those took some care. A check on the first word of a statement is not enough, because some statements that start like a read still write or lock. The read check looks at the whole statement and refuses anything it does not recognize as a plain read. It sometimes refuses a harmless query. That is the right way round: a refused read costs me a minute, and a write that slips through costs a great deal more.
It always falls back to the old job
The switch is built so that when anything goes wrong, the old script runs. No config, a database that is down, a missing row, a bug in the switch itself: every one of them ends with the old job doing what it has always done. Nothing but a deliberate choice, New or Off, stops the old script.
And there is a panic button. One file dropped into the switch’s folder sends every job back to its old script at once, before the switch even tries to read the database.
Two walls before a job goes live
Putting a job in shadow is a click. Putting it live takes one more step: a line in a config file on the server that clears that one job for the New position. Until that line is there, the New button is grayed out. If the row says New anyway, the switch refuses, runs the old script, and logs the refusal where I can see it.
So a click on a web page can never put a rewrite in charge by itself.
How one job goes live
- Put it in shadow.
- Read the board the next day. Did it run every time? Did anything fail? Did the comparison match, and did it actually see rows?
- Clear it for live with the one config line.
- Flip it to New.
- Watch the first hour. The board shows what the rewrite processed, which the old job never could.
- If anything looks wrong, flip it back to Old. The next run is the old script again.
Do the rollback once on purpose before you trust it. Put a harmless job on New, watch one run land, flip it back, and watch the next run come from the old script. Two minutes, and then you know.
What you get even if you never flip anything
The moment the three lines are in, every job gets a row on the board every time it runs, old script included. Before that, the only way to tell whether a job had run was to look at when its log file was last touched. That is how jobs that had quietly died went unnoticed for weeks.
When it is worth it
For a script you run by hand on your own data, a flag is fine.
For a job that runs on a schedule, touches data that matters, and that nobody fully understands, a switch beats rewrite-and-hope every time. The rewrite is the easy part. Proving it is the job.