fix(deployment): take the operation lock before validating the state transition (DEP-2, WP7 task 7.4)

Deploy/Disable/Enable/Delete now acquire the per-instance lock first, then reload the instance
(new IDeploymentManagerRepository.ReloadInstanceAsync) and validate on the fresh state, so a
Delete queued behind a deploy no longer takes the NotDeployed shortcut and orphans the running instance.
This commit is contained in:
Joseph Doherty
2026-12-03 09:45:24 -05:00
parent 47a7c0dde4
commit 517f093ce9
6 changed files with 247 additions and 16 deletions
@@ -90,6 +90,7 @@ All mutating operations on a single instance (deploy, disable, enable, delete) s
- Only one mutating operation per instance can be in-flight at a time. A second operation is rejected with an "operation in progress" error.
- **Different instances**: Operations on different instances can proceed **in parallel**, even at the same site. Each tracks status independently. This supports the bulk "deploy all out-of-date instances" operation efficiently.
- **Lock first, then validate.** Each command loads the instance (to learn its name), acquires the lock, then **reloads the instance and runs the state-transition check on the fresh state** (`IDeploymentManagerRepository.ReloadInstanceAsync`; an instance deleted meanwhile reports "not found"). A command that waited behind another one therefore never acts on the state it loaded before the wait — e.g. a Delete queued behind a deploy sees `Enabled`, not the stale `NotDeployed`, and tears the site down instead of orphaning it (review DEP-2).
> **`OperationLockManager` scope invariant (recorded decision, arch-review 05).** The
> per-instance locks are **per-node, in-memory**. Mutual exclusion therefore holds only
@@ -149,7 +150,7 @@ optimistically-concurrent status record.
It runs the ordinary deployment pipeline in three phases, of which only the middle
one is parallel:
1. **Prepare (serial).** Validate transition, take the operation lock, flatten +
1. **Prepare (serial).** Take the operation lock, validate the transition against the freshly reloaded instance, flatten +
validate, run query-before-redeploy reconciliation, insert the `InProgress`
record. Every step here touches the scoped, non-thread-safe `DbContext`, so the
phase is strictly serial. All instances share ONE `FlattenSession`, so a