Skip to content

feat(api): stop an application - #321

Merged
samuelmbabhazi merged 2 commits into
mainfrom
feat/256-stop-application
Nov 17, 2026
Merged

samuelmbabhazi merged 2 commits into
mainfrom
feat/256-stop-application

Conversation

@samuelmbabhazi

@samuelmbabhazi samuelmbabhazi commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

First part of #256 (application lifecycle): stopping an application. Deleting one and the screen follow in their own pull requests.

What changed

An application could not be stopped. Its replicas kept running, and since #313 they also kept what they reserve on their server, so the only way to free room was to deploy something else over them.

  • A stop is a deployment whose plan starts nothing. DeploymentPlan gains stop; such a deployment goes queued -> draining -> succeeded. The existing draining stage does the work: it removes the application's route on every server that served, then stops and removes every placed replica, whatever its release. Success marks those allocations stopped, which frees their reservation.
  • It resolves no image and places nothing, so it works when the registry is unreachable or the servers are full.
  • POST …/applications/{applicationId}/stop (201, deploy.run, session or API token) queues it. application_not_running (409) without a placed replica, deployment_in_progress (409, with the state) while another deployment runs.
  • After a stop nothing serves: serving() returns nothing, so any deployment that succeeded can be rolled back to, and deploying starts the application again. The application, its releases and its history stay.
  • A failed stop leaves the replicas it could not remove placed, with their reservation, and can be asked again.
  • Migration 20261117090000_deployment_stop adds deployments.plan_stop (false for every existing row). stop is a new boolean in every deployment answer; openapi.json, the API client and the route and role matrix are regenerated.
  • No agent change: only RemoveRoute, StopContainer and RemoveContainer, as a normal deployment already sends them for the replicas it replaces.

How it was tested

  • Unit tests written first: the state machine (queued, draining, succeeded, even when the plan also says build and release), the planner (routes and replicas removed, nothing resolved or placed, with and without domains, a replica that cannot be stopped) and the pipeline worker (reservation freed on success and kept on failure, registry never asked).

  • Integration test with PostgreSQL, the HTTP API and the real pipeline: refusals before anything runs and while a deployment runs, who may stop, the stop to its end, the reservation at zero, the application still there, the audit events, a second stop refused, and a rollback accepted afterwards.

  • Six mutations, each caught (one needed a tighter assertion first).

  • On a real agent, starting from the published v0.1.0-alpha.2 (control plane and agent), with one application deployed, then the control plane switched to an image built from this branch:

    Step Result
    Switch the image the migration applied to the alpha.2 database; the application kept answering
    Stop the application succeeded; the server's router holds no route, no container is left, GET …/replicas is empty
    Stop it again 409 application_not_running
    Deploy a second application (1 CPU by default) on the one-CPU server succeeded in the freed room
    Deploy the first one again while the server is full failed: No node can run replica 1 of 1 (node12: cpu).
    Stop the second, deploy the first succeeded, route back
    Stop, then roll back to the first deployment succeeded

    The agent stayed at v0.1.0-alpha.2 during all of this, and it applied the limits the new control plane sent (1 CPU, 512 MiB), which fix: give every container a CPU and a memory limit #313 had left untested.

Not tested

  • A stop on a server that is down or removed: its actions wait until they expire (one hour) and the stop fails. Skipping servers that no longer exist is not done here.
  • arm64, and a server that is not a container.

Security considerations

  • Stopping needs deploy.run, like deploying and cancelling, and is audited as deployment.queued with plan: stop, then by the pipeline's own events.
  • A stop is not destructive: nothing is deleted except containers that a new deployment recreates from the stored release, so it asks for no confirmation token. Deleting an application will.
  • No new agent action, no shell, no secret path. A stop sends no secret to an agent.

Found on the way

Prisma writes timestamps as UTC text without an offset, so with a PostgreSQL whose timezone is not UTC they are shifted against now() and against values written by other clients. The supported installation uses the official image (UTC) and CI too; my local test database did not. I will file it separately.

Refs #256

Summary by CodeRabbit

  • New Features
    • Added the ability to stop a running application. The stop deployment removes its routes and running replicas without building or starting the application.
    • Added an API endpoint to request a stop and report whether a deployment is a stop operation.
    • Applications can be restarted with a new deployment or rolled back to a previous successful deployment.
  • Bug Fixes
    • Stopped deployments are no longer reported as the application’s serving version.

An application could not be stopped: its replicas kept running, and kept
what they reserve on their node, until it was deployed again.

A stop is a deployment whose plan starts nothing. It goes from queued to
draining, removes the application's route and every placed replica, and
frees their reservation when it succeeds. It needs no image and no free
capacity. The application, its releases and its history stay, and
deploying or rolling back starts it again.

Refs #256

Signed-off-by: samuel mbabhazi <[email protected]>
@coderabbitai

coderabbitai Bot commented Nov 17, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The change adds stop deployments through the API. A stop deployment skips image resolution and placement, drains routes, and stops placed replicas. The deployment record and response include the stop plan, and the application and deployment history remain available.

Changes: Stop deployments

Layer / File(s) Summary
Stop plan and lifecycle behavior
packages/domain/src/lib/deployment.ts, packages/domain/src/lib/deployment.spec.ts, apps/api/src/modules/deployments/domain/release.ts, apps/api/src/modules/deployments/domain/pipeline.ts, apps/api/src/modules/deployments/domain/pipeline.spec.ts, docs/SPEC.md
Deployment plans can mark a stop. Stop plans advance through draining without build, release, or placement steps. The release helpers create stop drafts and report when no replica is running. Domain tests cover successful stops, cancellation, and stop failures.
Drain placed replicas
apps/api/src/modules/deployments/application/deployment-pipeline.ts, apps/api/src/modules/deployments/application/ports.ts, apps/api/src/modules/deployments/application/deployment-pipeline.spec.ts
The application pipeline treats all placed allocations as prior allocations for a stop and supplies no new allocations. Tests cover route and replica removal, and verify that a failed stop leaves the allocation unstopped.
Queue and persist stop requests
apps/api/prisma/migrations/*, apps/api/prisma/schema.prisma, apps/api/src/modules/deployments/application/deployment-commands.ts, apps/api/src/modules/deployments/infrastructure/prisma-deployment.store.ts, apps/api/src/modules/deployments/interface/deployment.controller.ts, apps/api/src/modules/deployments/interface/deployment.dto.ts, apps/api/src/audit-coverage.spec.ts, apps/api/src/modules/deployments/stop-http.integration.spec.ts, docs/SPEC.md
The API adds an authorized stop endpoint that queues a deployment. Storage persists the stop plan, and the deployment response exposes it. Integration tests cover access, conflict responses, stop completion, audit events, and rollback. The specification documents the endpoint and stop behavior.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Feature

Sequence Diagram(s)

sequenceDiagram
  participant DeploymentController
  participant DeploymentCommands
  participant DeploymentStore
  participant DeploymentPipeline
  DeploymentController->>DeploymentCommands: Request application stop
  DeploymentCommands->>DeploymentStore: Queue deployment with stop plan
  DeploymentCommands->>DeploymentPipeline: Wake queued deployment
Loading

Possibly related issues

  • shipkeepdev/shipkeep#256 — This change adds the stop operation and its audit events, which covers the stop portion of the lifecycle actions described in the issue.

Suggested labels: track:control-plane

Merge Risk: Minimal · up to 2962c

This adds an authorized API call that stops an application by draining its routes and removing its placed replicas, while keeping the application, releases and history. The review found no concrete defect in the authorization, persistence, pipeline or rollback behavior. Stops on down or removed servers were not tested, but that is a known limitation and does not block merging.

Security architecture risk: Moderate · up to 2962c

The stop operation preserves the existing permission and tenant boundaries, but cancellation can release the application for another deployment without fencing a previously loaded stop operation. A delayed stop worker can consequently remove routes belonging to the restarted application.

Retained concerns

  • Medium · reliability · inferred: A stop worker can load a draining plan, pause before enqueueing, and resume after cancellation has allowed a replacement deployment to finish. The enqueue path does not recheck the stop deployment's state, and its RemoveRoute actions target the application rather than a release generation. The delayed stop can therefore remove the restarted application's routes. Previously, ordinary draining excluded nodes serving the new release; the stop branch now targets every previously placed node, expanding this unfenced path to unchanged placement.
Security review details

Security Blast Radius

  • inferred — The destructive scope is one authorized application across all nodes holding its placed replicas, including allocations from multiple releases. The retained cancellation race can affect that application's routing after restart; the inspected path does not establish cross-tenant reachability or additional host authority.

Trust Boundaries and Controls

  • observed — The route requires the same tenant and API-token deploy.run permission as existing deployment mutations. Cookie-authenticated POST requests pass through Origin validation and mandatory CSRF credential parsing; bearer authentication checks the declared permission and target scope.

Resilience and Maintainability Implications

  • observed — Agent stop/remove handlers treat missing containers as successful cleanup and refuse foreign unmanaged containers. Stop records non-running intent before stopping, and route removal tolerates an absent route. These existing controls support retry after partial removal, but do not distinguish an old application's route generation from a newly installed one.

Hardening Proposals

  • proposed — Fence destructive stop actions across cancellation and successor deployment. Revalidate deployment state atomically with enqueueing, and prevent already queued route removals from deleting a successor generation, or retain lifecycle ownership until those actions are settled.
Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "feat(api): stop an application" matches the main change. The PR adds a POST endpoint that queues a stop deployment for an application. The stop deployment removes routes and placed replicas…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "feat(api): stop an application" matches the main change. The PR adds a POST endpoint that queues a stop deployment for an application. The stop deployment removes routes and placed replicas, and the diff adds the stop command, controller, DTO flag, pipeline handling, and migration for plan_stop. The title is concise, specific, and follows a conventional-commit style.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint install timed out. The project may have too many dependencies for the sandbox.

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 811b214 and 2962c1b.

Files ignored due to path filters: 3

  • apps/api/openapi.json is excluded by !apps/api/openapi.json
  • docs/generated/route-role-matrix.md is excluded by !**/generated/**, !docs/generated/**
  • packages/api-client/src/gen/openapi.ts is excluded by !**/gen/**, !packages/api-client/src/gen/**
Files selected for processing: 17

  • apps/api/prisma/migrations/20261117090000_deployment_stop/migration.sql
  • apps/api/prisma/schema.prisma
  • apps/api/src/audit-coverage.spec.ts
  • apps/api/src/modules/deployments/application/deployment-commands.ts
  • apps/api/src/modules/deployments/application/deployment-pipeline.spec.ts
  • apps/api/src/modules/deployments/application/deployment-pipeline.ts
  • apps/api/src/modules/deployments/application/ports.ts
  • apps/api/src/modules/deployments/domain/pipeline.spec.ts
  • apps/api/src/modules/deployments/domain/pipeline.ts
  • apps/api/src/modules/deployments/domain/release.ts
  • apps/api/src/modules/deployments/infrastructure/prisma-deployment.store.ts
  • apps/api/src/modules/deployments/interface/deployment.controller.ts
  • apps/api/src/modules/deployments/interface/deployment.dto.ts
  • apps/api/src/modules/deployments/stop-http.integration.spec.ts
  • docs/SPEC.md
  • packages/domain/src/lib/deployment.spec.ts
  • packages/domain/src/lib/deployment.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant