Skip to main content

Roll out Studio's changes on a single host

A small installation runs on one host and has no deployment pipeline, or doesn't want to wait for one, eg Shop.Api on one Linux server as a systemd service. Local rollout gets a change applied in Studio, eg a new Care instructions field, into the admin within a minute and without a deployment: the host builds the applied change itself and restarts on it, and when anything fails, the previous release keeps serving and the change's files are put back.

It builds on the local checkout target, which writes and commits the applied files into a checkout on the host. An installation with several hosts, or with a pipeline, uses a hosted repository target instead.

Turn it on​

Program.cs needs nothing beyond the usual last line, await app.RunVoxAsync(args);. Turn local rollout on in the settings:

appsettings.json
{
"Vox": {
"Studio": {
"Source": {
"Target": "LocalCheckout",
"LocalCheckout": { "Root": "/srv/shop", "Push": { "Remote": "origin", "Branch": "main" } }
},
"LocalRollout": { "Enabled": true, "StateDirectory": "/var/lib/shop/vox-studio" }
}
}
}
  • Vox:Studio:Source:Target must be LocalCheckout, since the host builds from that checkout. Startup fails with a message that names the setting otherwise.
  • StateDirectory is required, and startup fails without it. It holds the releases the host builds, and it should be outside the installed application's directory: deploying by replacing that directory would otherwise lose the active release. A relative path is relative to the working directory.
  • StartupTimeout (5 minutes by default) is how long a new release may take to start, StopTimeout (30 seconds) how long a release may take to stop before it's killed, and PollInterval (5 seconds) how often the host looks for applied changes. PublishArguments adds arguments to dotnet publish, eg ["-r", "linux-x64"].

Then start the application as before, eg dotnet Shop.Api.dll or the same systemd unit. That process is now the runner. It starts the application again as a child process, the serving child, which serves HTTP, runs background work and migrates the database as usual. The runner itself never serves, so it opens no ports and runs no background work; it only reads Studio's tables.

Because the same Program.cs runs in the runner, in each child and for vox migrate, the code before RunVoxAsync must only configure the application, as it already must, see hosts. Work that needs a started application registers on ApplicationStarted, which only fires in the serving child.

dotnet Shop.Api.dll vox migrate and its options keep working as before: they never start a runner.

What happens when a change is applied​

  1. Build. The runner builds the new commit with dotnet publish -c Release into <state>/releases/<n>, while the old child keeps serving. It builds the checkout's latest commit, which contains the change's commit and the commits that put back the files of earlier changes that didn't go live, in its own Git worktree, <state>/build. So unrelated uncommitted edits in the checkout are left out, and obj/ folders stay between builds, so a build takes seconds rather than a minute. Without Git, or with CommitToGit off, it builds the checkout itself. Apply then waits while a build runs, so that a half-written change never gets into a build.
  2. Preflight. It runs dotnet <release>/Shop.Api.dll vox migrate --preflight with the new release, still while the old child serves. A model the database can't take, eg "title was removed and heading added; declare whether it's a rename", stops the rollout without any downtime. When the configuration sets RequireAppliedModel, it also runs vox migrate with the new release before it switches. Neither has a time limit, so that a long migration isn't stopped part way; stopping the service stops them.
  3. Switch. It stops the old child and starts the new release. The application is unavailable while the new release starts, typically a few seconds: every rollout is a restart.
  4. Applied. The new release starts, finds the proposal's manifest in its own build and marks the proposal applied. The admin reconnects and shows the change.

When three changes are applied while a build runs, the next build is one build of the newest commit and one restart, not three: the newest commit contains the earlier ones, and each change still becomes applied through its own manifest.

A child has started when the application has started and Studio has marked the proposals of its build applied; it then writes a file in <state>/started. A child that exits before that, or hasn't started within StartupTimeout, has failed to start.

When a rollout fails​

When the build, the preflight or the new release's startup fails:

  • The previous release keeps serving. After a failed build or preflight, the old child never stopped. After a failed startup, the runner stops the new release and starts the previous one again.
  • The change's files are put back in the checkout. Edited and removed files get the text of the running build back, and added files and the proposal's manifest are deleted. With Git, the runner commits that with the host's committer identity, as Restore files of Studio proposals that didn't go live: <titles> with a Vox-Restored-Proposal: <id> trailer per proposal, and pushes it when the local checkout target pushes. Without the restore, the failed change would stay in the checkout: the next change would be built on top of it and fail the same way.
  • History shows the proposal as "Didn't go live", with the reason and the end of the build's, preflight's or startup's output (the last 64 KB). The proposal stays submitted; the proposer or a developer makes the change again.

Putting the files back must finish before the next change rolls out, since that change would otherwise be built with the failed one. When it keeps failing, eg since the checkout's files can't be written or a Git commit fails, the runner logs the error and tries again every PollInterval, and later changes wait until it succeeds. Fix the cause the log names.

Stopping the service while a change is built or checked doesn't fail the change: it's built again when the service starts.

When the previous release fails to start again too. Starting the previous release again can't undo model or data migrations that the new release's startup already ran, so the previous release may fail as well. The preflight catches most model problems before anything is applied, so this mostly concerns failures after the model was applied. Then the application is down: the runner records both failures on the proposal, logs them and exits with code 1, so the service manager shows the failure. Find the cause in the log, fix the database or the code, and start the service again; the runner then starts the release that served last.

A serving child that stops on its own also makes the runner exit, with the child's exit code, so the service manager's restart policy applies as it would without local rollout. So does a release that doesn't start when the runner starts, eg after a reboot whose database was slow to come up: the runner exits with code 1 and keeps that release as the active one, so the service manager's next start tries it again rather than silently going back to an older release.

The installed build wins​

The runner records the build it was started from. When it's started from another build, the operator has deployed a new version on purpose, and the runner serves that one from the installed directory, instead of the last release it built. If the newly installed build fails to start, the runner starts its last release instead.

If the new version's source doesn't contain Studio's commits, their changes are gone from the running application. A removed field stops the new version's startup, since the model refuses a removed property without a migration, and the runner then serves its last release again; an import mapping or a validator would disappear silently. So, in a local-first installation:

  • push Studio's commits, with the local checkout target's Push setting, so the builds you deploy include them;
  • update the host's checkout to the deployed commit when you deploy. Until then, Apply refuses every file that differs between the checkout and the running build, and names it.

What the state directory holds​

PathWhat it is
runner.jsonThe active and previous releases, the installed build, the serving child, and the rollout in progress with its step.
runner.lockHeld open by the runner while it runs, so that a second runner on the same state directory doesn't start. Give each installation its own state directory.
releases/<n>The dotnet publish output of a release, often a few hundred megabytes.
started/<release>.startedThe file a child writes once it has started.
buildThe Git worktree the runner builds commits in.

The directory doesn't grow with every release. The runner keeps at most three releases: the active one, the previous one, which it starts again when a new release fails, and a new one while it's rolled out. It deletes every other release after each rollout and when it starts, and logs what it kept and the directory's size. The build worktree is one directory reused for every build; deleting it makes the next build start from scratch, eg when an incremental build misbehaves. The texts that a failed rollout puts back are deleted once the rollout succeeds or the files are back.

After a crash, the runner carries on where it stopped when it starts again. It stops a child that's still running, so it doesn't hold the port; a child also stops on its own when its runner dies. A build or preflight that was interrupted is started again, a switch is completed when the new release had started and failed otherwise, and putting files back is done again.

Where releases run​

Each child runs in the runner's content root, with --contentRoot set to it, and with the runner's environment and command-line arguments. The content root is the installed directory, also for a Windows service, whose working directory is System32. So:

  • appsettings*.json, relative SQLite paths such as vox.db, and the ASP.NET content root are the installed directory's, the same for every release, so every release uses the same database and data;
  • code that reads files relative to AppContext.BaseDirectory sees the release's directory.

Proposals can't change settings or files other than C#, so nothing is lost by using the installed directory's. When LocalCheckout:Root isn't set, each child is told the runner's checkout, since a release built in the build worktree would otherwise take the worktree for the checkout.

The runner stops a child by closing its standard input, which the child watches, so a serving application must not read its standard input for anything else. .NET has no cross-platform way to send one child process a graceful stop signal.

Limits​

  • The host needs the .NET SDK, not just the runtime, and access to the NuGet feeds the application restores from.
  • The application must run as its own long-running process, eg a systemd service, a Windows service, a container whose main process is the application, or a console. IIS in-process hosting, and platforms that start the application process themselves and expect it to listen, such as Azure App Service, can't host the runner. With systemd, use Type=simple or Type=exec: the runner never sends the ready notification of Type=notify.
  • A container image doesn't change, so a container needs the checkout and the state directory on volumes, and the SDK in the image.
  • One host. Several hosts behind a load balancer use a hosted repository target and their pipeline.
  • Every rollout is a restart, with an outage as long as the new release's startup.
  • Migrations aren't rolled back when a new release fails after its startup applied them.

Try it on a development machine​

Keep local rollout off in your usual development settings, since dotnet watch and a debugger attach to the runner rather than to the child that serves. To try it, set a state directory in a folder your repository ignores and turn committing off, so the runner builds your working tree and the change stays as ordinary unstaged edits:

appsettings.Development.json
{
"Vox": {
"Studio": {
"Source": { "Target": "LocalCheckout", "LocalCheckout": { "CommitToGit": false } },
"LocalRollout": { "StateDirectory": "tmp/vox-studio-runner" }
}
}
}

Then run the application with local rollout on:

dotnet run -- --Vox:Studio:LocalRollout:Enabled=true

Apply a field in Studio and watch the console: the runner builds the application, restarts it, and the field appears. A change that doesn't compile leaves the running release serving, and its files are put back.