The watch after a release
On this page
A release that passed every test can still be wrong in ways only the live system shows: a function that now fails one call in ten, a page that is slow, a job that fails the next time it runs. The watch keeps asking whether the live system is well for a while after a release, and undoes the release if it is not.
The factory does not know where your project is hosted. So you supply the commands that reach your live system, and the factory supplies everything round them: when to look, whether failures have risen, when a bad look counts, running the undo once, telling a person, and holding later releases.
#Your commands
| What it does | How it answers | |
|---|---|---|
| The check | Asks the live system whether it is well. Whatever "well" means for you: a page answers, the error rate is no higher than last week, a queue is not growing | Exit 0: well. Exit 10: not well. Anything else, or taking too long: could not tell |
| The counts | Prints how many runs and failures each part of the live system has had since the release, and over a time before it (a week, say). The factory judges them: see below | Exit 0 with the numbers as JSON. Anything else: could not tell |
| The undo (optional) | Puts back what was live before the release | Exit 0: done |
"Not well" is 10, not 1, on purpose. A script that crashes ends with 1 (an uncaught error in Python or Node, a failed command under set -e), and a check that crashed has not seen the live system. So only a check that looked, found the live system unwell, and said so with 10 can undo a release. Everything else, a crash included, is "could not tell", and the watch never undoes a release on the strength of that.
The check asks whether the live system is well, not whether it is the release being watched. A later release, or an undo, changes what is live while a watch is going; a check that compares versions calls a healthy system unwell. And your release job must not end by running the check: a release that goes red because the system is unwell is not a release at all, so nothing watches it and nothing is undone.
Give the check, the counts, or both. With both, a look is well only if both are: the counts are asked first, and the check only if they are steady.
#Judging counts: have failures risen?
Most live systems fail a little all the time, so "is there an error?" is the wrong question. The right one is "is it worse than before the release?" Your counts command prints the numbers, and the factory answers:
{"since": {"api": {"errors": 7, "calls": 90}, "nightly-job": {"errors": 2, "calls": 2}},
"before": {"api": {"errors": 40, "calls": 10000}, "nightly-job": {"errors": 1, "calls": 30}}}
A part has gone wrong when either:
- every run since the release failed: at least 2 runs, all failed, and before the release fewer than half did. This catches a job that runs rarely and now fails every time;
- failures have risen: at least 5 failures since the release, at a rate above 2%, and at least 3 times the rate before.
All three conditions of the second rule must hold, so a single failure, a rate that was always high, and a tiny rate on a busy part are none of them a rise. The three numbers are --min-errors, --rate-floor and --rate-multiple; a part can give its own least number of failures as "min_errors" beside its counts. A part with no history counts as never having failed before. A part's name is letters, digits, spaces and . _ : / -, at most 80 characters, and there may be up to 200 parts.
Print every part every time, with both numbers, and noughts when nothing has happened yet. The watch looks first straight after the release, when there may be nothing to count. An answer with no parts, a part without errors or calls under exactly those names, or more failures than runs (two numbers not counting the same thing), is "could not tell", never "well": a misspelt field read as nought would let every release pass unwatched. Three such answers in a row end the watch and tell a person. A part missing from before is fine: it has no history.
A part's name starts with a letter or digit, and "min_errors" is a whole number.
since must count from the release to now, every time it is asked: the numbers grow through the watch. before can be any stretch you choose that shows what normal is.
These rules were taken from a watch that has guarded a production system since November 2026, and the factory's answers were checked against that program's on 200,000 sets of numbers: none differed.
When the counts say a release has gone wrong, the issue lists each part with its numbers. After an undo, the counts cannot say whether things are well again, because they count everything since the release; if you also gave a check, that is asked.
All commands must be runnable as written: a script named by itself needs its executable bit, or name the program that runs it (python3 scripts/live_check.py).
Each command is a program and its arguments, run without a shell. For pipes or several steps, write a script and name it.
#Running it
A job in your release workflow, after production. It needs whatever your two commands need (a checkout, your cloud role), plus a checkout of Open Factory Floor and issues: write:
watch:
needs: production
runs-on: ubuntu-latest
timeout-minutes: 60 # --minutes plus the undo's time (--undo-seconds, 15 minutes unless said) plus ten: if GitHub stops the job, nothing is undone and nobody is told
permissions: {contents: read, issues: write}
steps:
- uses: actions/checkout@<commit> # your own repository, for your two scripts
- uses: actions/checkout@<commit>
with: {repository: theaiengineers/factory, ref: <commit>, path: .factory, persist-credentials: false}
- run: python3 -m pip install --require-hashes --only-binary :all: -r .factory/requirements.txt
- env:
GITHUB_TOKEN: ${{ github.token }}
PYTHONPATH: .factory
run: python3 -m factory.watch --line api --minutes 30 --check "scripts/live_check.sh" --undo "scripts/undo_release.sh"
And in the factory file, so that a release that went wrong holds the next one:
- gate:
unattended: true
hold_when:
- open_action: watch-failed
| Setting | Unless said | Meaning |
|---|---|---|
--minutes | 30 | How long to watch |
--every | 5 | Minutes between looks. After a look that was not "well" it looks again one minute later |
--confirm | 2 | Looks in a row that must say "not well" before the release counts as gone wrong |
--lost | 3 | Looks in a row that could not tell, before the watch gives up |
--min-errors, --rate-floor, --rate-multiple | 5, 0.02, 3 | The counts rules above |
--check-seconds, --undo-seconds | 120, 900 | How long one look (the check, and the counts), and the undo, may take |
#What happens
- Well to the end: the job passes. Nothing is written anywhere but the run's summary.
- Not well, twice with no "well" between: the release has gone wrong. (A look that could not tell, between two that said not well, does not clear the first.) The undo runs, once. The check runs once more, to say what state things were left in. An issue is opened, assigned to the people under
people.action_needed, and the job fails. - Could not tell, three times in a row: nothing is undone, since nothing is known to be wrong. But nobody is watching, so the same issue is opened, saying that, and the job fails.
The issue says what happened, whether the release was undone, whether the live system was well afterwards, and what to do. While it is open, every unattended release on the line is held. A person closes it; nothing else does. If it happens again while the issue is open, a comment says what happened that time.
#What to know
- One bad look never undoes a release. A check can fail for a moment by itself. Raise
--confirmif yours is jumpy. With counts the second look reads nearly the same numbers as the first, so it protects less: there the protection is the rules themselves, and--min-errorsis the one to raise. - The undo does not fix the branch. The branch still holds the change that went wrong, and the next release would send it out again. Revert or fix it first. The issue says so, and holds the line until then.
- The undo undoes what your command undoes. If a release also changed a database or a setting and your undo puts back only code, say so in your own notes: the factory cannot know.
- A failed watch makes the run fail, and a failed run is not counted as a release by the gate. The next release is then judged against the release before, which is the cautious reading whether or not the undo worked.
- While the watch runs, the run has not finished. If your release workflow has a
concurrencygroup for the whole workflow, the next release waits for the watch, which stops two releases landing inside one watch. The price is that everything about the second merge waits up to--minutes: its tests and its staging as well as its release, and the run of the first shows as unfinished throughout. To avoid that, run the watch in a workflow of its own: see below. - Nothing your check or undo prints is copied into the issue. Their output stays in the run's log, where GitHub hides secrets. From the counts, the issue carries the names of the parts that rose and their numbers, and nothing else: do not put anything private in a part's name.
- The check's token. Your commands run with the job's environment, the GitHub token included, as any step of your own would.
- If the issue cannot be written the job still fails, and the run's summary says so.
- The watch leaves one notice on its job (
factory-watch), saying how it ended. The record reads it back, so that a release the watch undid is recorded as undone.
#A workflow of its own
Run as a job of the release workflow, the watch keeps the run open until it ends. Run in a workflow of its own, started when the release workflow finishes, it does not: the release run finishes when the release does, the next merge's tests and staging start at once, and only the next release waits for the watch to end. This is also the place for a watch that needs a stronger cloud role than the rest of the release.
1. The watch workflow. It starts on every finished run of the release workflow, and the watch itself works out whether that run released anything:
name: release-watch
on:
workflow_run:
workflows: ["release"] # the release workflow's name
types: [completed]
concurrency:
group: release-watch-${{ github.event.workflow_run.id }}
cancel-in-progress: false
permissions: {contents: read, issues: write, actions: read}
jobs:
watch:
runs-on: ubuntu-latest
timeout-minutes: 60
steps:
- uses: actions/checkout@<commit> # the default branch's scripts: no `ref:`
- uses: actions/checkout@<commit>
with: {repository: theaiengineers/factory, ref: <commit>, path: .factory, persist-credentials: false}
- run: python3 -m pip install --require-hashes --only-binary :all: -r .factory/requirements.txt
- env:
GITHUB_TOKEN: ${{ github.token }}
PYTHONPATH: .factory
RELEASE_RUN: ${{ github.event.workflow_run.id }}
run: python3 -m factory.watch --line api --release-run "$RELEASE_RUN" --minutes 30 --counts "scripts/live_counts.sh" --undo "scripts/undo_release.sh"
2. The factory file. Name both workflows on the line, so the gate knows there is a watch to wait for and the record knows where to read how it ended:
- id: api
workflow: release.yml
watch: release-watch.yml
What --release-run does:
- Decides whether there is anything to watch. A run that held, failed before releasing, released nothing on this line, or was on another branch ends the watch at once, green. A run counts as a release when it succeeded and the gate said go in it. If a release can be live in a run that later turned red (a tidy-up step after it failed), also give
--release-job "<the name of your job that does the releasing>": the run is then watched whenever that job succeeded. The name must be exactly one job's, as GitHub shows it; if it is no job's, the watch fails rather than watch nothing. - Tells your commands which release. They are run with
FACTORY_RELEASE_RUN,FACTORY_RELEASE_HEAD(the commit) andFACTORY_RELEASED_AT(when it went out) in their environment, so your counts can count from the right moment. - Never undoes over a later release. Just before the undo, the watch asks whether the line has been released again, or is being. If so the undo is not run, since putting back what was there before would take the later release away too; the issue says so. (The gate's wait, below, means this should happen only when a person released by hand during the watch.) A later run counts once the gate has said go in it, even if its release then failed part-way: what is live is then not known, and the undo is left to a person. If the question cannot be answered at all, the undo runs: a release known to be wrong is the surer harm.
- Writes the issue about the release, not about its own run.
What the gate does, for a line that names watch:
- A release that would go out unattended first waits until no run of the watch workflow is queued or going, no run of the release workflow finished in the last two minutes (its watch may not be listed yet), and no other run of the release workflow is still going in which the gate has already said go (that release is on its way out, and its watch follows it). Two releases inside one watch could leave live the code the first watch meant to take away. Then it asks everything again, because the watch may just have ended badly.
- It waits up to 75 minutes (
watch_wait_minuteson the gate's workflow, 1 to 180), then holds and tells a person. Set it above your--minutes. If GitHub cannot be asked, it holds. - A release a person starts by hand does not wait for the watch. But one gate decides at a time for a line, so if a merge's gate is already waiting, the person's run queues behind it. To release at once, cancel the waiting run first; what it carried goes out with yours.
- Only the newest waiting run is kept. GitHub keeps one run queued behind a gate that is deciding and cancels an older queued one when a newer arrives. Nothing is lost: the newest run releases everything merged since the last release. But the older run shows as cancelled.
- A release run stuck part-way (waiting for an approval, say) after its gate said go makes later merges wait, then hold. Finish or cancel it.
What to know:
- The release run has succeeded before the watch ends, so a release the watch undoes still counts as the last release when the gate next works out what has changed. What keeps the undone change from going out again unattended is the watch's issue, which holds the line until a person closes it: fix or revert on the branch first, as the issue says.
- The watch runs the default branch's copy of its workflow and scripts, whatever branch the release ran on. Do not download anything a run produced and execute it.
- A change to the watch workflow holds the next release, as a change to any workflow file does (it holds even if the gate's
not_in_releasenames it). - If the watch's run cannot read the release it is for, it fails and says that nothing is being watched. Nothing is undone and no issue is opened: the red run is how a person sees it.
#What is not built
- Different rules for different parts, beyond each part's own least number of failures.
This guide is docs/WATCH.md in the repository, and this page is made from it. Something wrong or unclear? Open an issue or send a pull request. Next: Hoppers and the bin.