refactor: extract validation and socat cleanup from stop.rs handle#126
Merged
jamiesunderland merged 1 commit intocoast-guard:mainfrom Mar 23, 2026
Conversation
Extract two helpers from the stop handler to reduce complexity and add test coverage: - validate_stoppable: pure status check for Running, CheckedOut, Idle (ok) vs Stopped, transitional states (error). Enqueued is handled separately by the caller. - kill_instance_socat_processes: iterates port allocations, kills socat PIDs, and clears them in the DB. Adds 11 unit tests (8 for validate_stoppable, 3 for socat cleanup). The #[allow] annotations on handle remain as it still triggers both lints after extraction. Closes coast-guard#122
jamiesunderland
approved these changes
Mar 23, 2026
Contributor
jamiesunderland
left a comment
There was a problem hiding this comment.
Looks great. Thanks @mukeshblackhat
Closed
6 tasks
mukeshblackhat
added a commit
to mukeshblackhat/coasts
that referenced
this pull request
Mar 24, 2026
Extract status validation into a pure function with an exhaustive match on all InstanceStatus variants, following the same pattern as validate_stoppable in stop.rs (PR coast-guard#126). Startable: Stopped, Idle, Enqueued, Unassigning (preserving original fall-through behavior). Rejected: Running/CheckedOut (already running), Provisioning/Assigning/Starting/Stopping (transitional). Adds 10 unit tests covering every InstanceStatus variant. The #[allow] annotations on handle remain as it still triggers both lints. Closes coast-guard#133
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
validate_stoppable— pure function that checks if an instance status allows stoppingkill_instance_socat_processes— kills socat PIDs and clears them in the DBhandleto call both helpers instead of inline logic#[allow(clippy::cognitive_complexity, clippy::too_many_lines)]kept onhandle— still triggers both lints after extraction (follow-up ticket)What was there before
handlewas ~220 lines with status validation (lines 64-88) and socat cleanup (lines 200-216) inlined. The status checks were a chain ofifstatements comparing against individualInstanceStatusvariants.What changed
Single file:
coast-daemon/src/handlers/stop.rsExtracted functions:
validate_stoppable(status, name)Okfor Running/CheckedOut/Idle,Errfor Stopped and transitional stateskill_instance_socat_processes(db, project, name)Deviations from ticket suggestions
Idlegrouped withEnqueued(not stoppable)Idlegrouped withRunning/CheckedOut(stoppable)Idle— it fell through to the stoppable path.coast archivedepends on this:handle_archivecallsstop::handleforIdleinstances. RejectingIdlebroketest_handle_archive_stops_only_running_checked_out_and_idle_instances(expected 3 stopped, got 2). Fixed by preserving the original behavior.kill_instance_socat_processesasasync fnfn(sync)StateDband process signals viakill_socat.StateDbis notSend, so marking the functionasynccauses compilation errors. The ticket's suggestion was reasonable but doesn't work with the current type constraints.Test plan
Run new tests
Verify archive tests still pass
Run lint and full tests
Verify behavior is unchanged
Closes #122