Implement 'barn kill' command #12

Closed
opened 2026-10-04 19:28:09 +00:00 by daniel · 1 comment
Owner

Imported from GitHub samson-media/barn#12 — opened by @daniel-samson on 2026-01-21 12:39:29 UTC

Overview

Implement the barn kill command that stops a running job.

Context

  • Read first: README.md (barn kill examples)
  • Depends on: #8 (CLI framework), #5 (process execution)

Requirements

1. Kill Command

com.samsonmedia.barn.cli.KillCommand.java:

@Command(name = "kill", description = "Stop a running job")
public class KillCommand extends BaseCommand {
    @Parameters(index = "0", description = "Job ID")
    private String jobId;

    @Option(names = {"--force", "-f"}, description = "Force kill (SIGKILL)")
    private boolean force;

    @Option(names = {"--quiet", "-q"}, description = "No output on success")
    private boolean quiet;

    @Override
    public Integer call() {
        // 1. Find job by ID
        // 2. Verify job is running
        // 3. Send kill signal
        // 4. Update job state to CANCELED
    }
}

2. Usage Examples

# Kill a job
barn kill job-9f83c

# Force kill (SIGKILL instead of SIGTERM)
barn kill --force job-9f83c

# Quiet mode
barn kill --quiet job-9f83c

# JSON output
barn kill --output=json job-9f83c

3. Kill Behavior

  1. First attempt: SIGTERM (graceful)
  2. Wait briefly for process to exit
  3. If --force or process doesn't exit: SIGKILL
public void killJob(Job job, boolean force) {
    if (force) {
        ProcessUtils.killTree(job.pid());  // SIGKILL
    } else {
        ProcessUtils.terminate(job.pid()); // SIGTERM
        if (!waitForExit(job.pid(), gracePeriod)) {
            ProcessUtils.killTree(job.pid());
        }
    }
    repository.complete(job.id(), -1, "Killed by user");
    repository.updateState(job.id(), JobState.CANCELED);
}

4. Output

Human format:

Job job-9f83c killed
  Previous state: running
  Exit code: -1 (killed)

JSON format:

{
  "job_id": "job-9f83c",
  "previous_state": "running",
  "new_state": "canceled",
  "exit_code": -1,
  "message": "Killed by user"
}

5. Error Handling

  • Job not found: exit 1 with "Job not found: job-xyz"
  • Job not running: exit 1 with "Job is not running (state: succeeded)"
  • Kill failed: exit 1 with "Failed to kill process: "

6. Tests Required

  • KillCommandTest.java: Test command parsing, error cases
  • Integration test: Start job, kill it, verify state

Acceptance Criteria

  • barn kill <id> stops a running job
  • --force uses SIGKILL
  • Job state updated to CANCELED
  • Proper error for non-running jobs
  • Proper error for non-existent jobs
  • All output formats work
  • mvn verify passes
  • Tests have 80%+ coverage

Implementation Notes

Cross-platform kill:

public static void terminate(long pid) {
    ProcessHandle.of(pid).ifPresent(ProcessHandle::destroy);
}

public static void killTree(long pid) {
    ProcessHandle.of(pid).ifPresent(ph -> {
        ph.descendants().forEach(ProcessHandle::destroyForcibly);
        ph.destroyForcibly();
    });
}

On Windows, destroy() is equivalent to destroyForcibly().

Definition of Done

  • Kill command stops processes
  • State updated correctly
  • Error handling is robust
  • All tests pass
<!-- github-import:issue:12 --> _Imported from GitHub samson-media/barn#12 — opened by **@daniel-samson** on 2026-01-21 12:39:29 UTC_ ## Overview Implement the `barn kill` command that stops a running job. ## Context - **Read first**: `README.md` (barn kill examples) - **Depends on**: #8 (CLI framework), #5 (process execution) ## Requirements ### 1. Kill Command `com.samsonmedia.barn.cli.KillCommand.java`: ```java @Command(name = "kill", description = "Stop a running job") public class KillCommand extends BaseCommand { @Parameters(index = "0", description = "Job ID") private String jobId; @Option(names = {"--force", "-f"}, description = "Force kill (SIGKILL)") private boolean force; @Option(names = {"--quiet", "-q"}, description = "No output on success") private boolean quiet; @Override public Integer call() { // 1. Find job by ID // 2. Verify job is running // 3. Send kill signal // 4. Update job state to CANCELED } } ``` ### 2. Usage Examples ```bash # Kill a job barn kill job-9f83c # Force kill (SIGKILL instead of SIGTERM) barn kill --force job-9f83c # Quiet mode barn kill --quiet job-9f83c # JSON output barn kill --output=json job-9f83c ``` ### 3. Kill Behavior 1. First attempt: SIGTERM (graceful) 2. Wait briefly for process to exit 3. If `--force` or process doesn't exit: SIGKILL ```java public void killJob(Job job, boolean force) { if (force) { ProcessUtils.killTree(job.pid()); // SIGKILL } else { ProcessUtils.terminate(job.pid()); // SIGTERM if (!waitForExit(job.pid(), gracePeriod)) { ProcessUtils.killTree(job.pid()); } } repository.complete(job.id(), -1, "Killed by user"); repository.updateState(job.id(), JobState.CANCELED); } ``` ### 4. Output Human format: ``` Job job-9f83c killed Previous state: running Exit code: -1 (killed) ``` JSON format: ```json { "job_id": "job-9f83c", "previous_state": "running", "new_state": "canceled", "exit_code": -1, "message": "Killed by user" } ``` ### 5. Error Handling - Job not found: exit 1 with "Job not found: job-xyz" - Job not running: exit 1 with "Job is not running (state: succeeded)" - Kill failed: exit 1 with "Failed to kill process: <reason>" ### 6. Tests Required - `KillCommandTest.java`: Test command parsing, error cases - Integration test: Start job, kill it, verify state ## Acceptance Criteria - [ ] `barn kill <id>` stops a running job - [ ] `--force` uses SIGKILL - [ ] Job state updated to CANCELED - [ ] Proper error for non-running jobs - [ ] Proper error for non-existent jobs - [ ] All output formats work - [ ] `mvn verify` passes - [ ] Tests have 80%+ coverage ## Implementation Notes Cross-platform kill: ```java public static void terminate(long pid) { ProcessHandle.of(pid).ifPresent(ProcessHandle::destroy); } public static void killTree(long pid) { ProcessHandle.of(pid).ifPresent(ph -> { ph.descendants().forEach(ProcessHandle::destroyForcibly); ph.destroyForcibly(); }); } ``` On Windows, `destroy()` is equivalent to `destroyForcibly()`. ## Definition of Done - Kill command stops processes - State updated correctly - Error handling is robust - All tests pass
daniel added this to the M3: CLI Commands milestone 2026-10-04 19:28:09 +00:00
daniel added the cli label 2026-10-04 19:28:09 +00:00
Author
Owner

@daniel-samson commented on 2026-01-21 14:29:17 UTC:

Implemented barn kill command with tests

<!-- github-import:comment:3778428871 --> _**@daniel-samson** commented on 2026-01-21 14:29:17 UTC:_ Implemented barn kill command with tests
This repo is archived. You cannot comment on issues.