Implement 'barn service' commands #16

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

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

Overview

Implement the barn service subcommand for managing the service lifecycle.

Context

  • Read first: README.md (service management section)
  • Depends on: #8 (CLI framework), #15 (service daemon), #14 (IPC)

Requirements

1. Service Command Group

com.samsonmedia.barn.cli.ServiceCommand.java:

@Command(
    name = "service",
    description = "Manage the Barn service",
    subcommands = {
        ServiceStartCommand.class,
        ServiceStopCommand.class,
        ServiceRestartCommand.class,
        ServiceReloadCommand.class,
        ServiceStatusCommand.class,
        ServiceLogsCommand.class
    }
)
public class ServiceCommand extends BaseCommand {
    @Override
    public Integer call() {
        CommandLine.usage(this, System.out);
        return 0;
    }
}

2. barn service start

@Command(name = "start", description = "Start the Barn service")
public class ServiceStartCommand extends BaseCommand {
    @Option(names = {"--foreground", "-f"}, description = "Run in foreground (don't daemonize)")
    private boolean foreground;

    @Override
    public Integer call() {
        // Check if already running
        // Start service (daemonize or foreground)
    }
}

Output:

Barn service started (PID: 12345)

3. barn service stop

@Command(name = "stop", description = "Stop the Barn service")
public class ServiceStopCommand extends BaseCommand {
    @Option(names = {"--force", "-f"}, description = "Force immediate stop")
    private boolean force;

    @Override
    public Integer call() {
        // Send shutdown via IPC
        // Or send SIGTERM to PID
    }
}

Output:

Barn service stopped

4. barn service restart

@Command(name = "restart", description = "Restart the Barn service")
public class ServiceRestartCommand extends BaseCommand {
    @Override
    public Integer call() {
        // Stop then start
    }
}

5. barn service reload

@Command(name = "reload", description = "Reload configuration")
public class ServiceReloadCommand extends BaseCommand {
    @Override
    public Integer call() {
        // Send reload via IPC
        // Or send SIGHUP to PID
    }
}

Output:

Configuration reloaded

6. barn service status

@Command(name = "status", description = "Show service status")
public class ServiceStatusCommand extends BaseCommand {
    @Override
    public Integer call() {
        // Query service via IPC
        // Show running/stopped, PID, uptime, job counts
    }
}

Output (human):

Barn Service Status
==================
Status:     running
PID:        12345
Uptime:     2 days, 4 hours
Started:    2026-01-19 14:30:00

Jobs:
  Running:  2
  Queued:   5
  Total:    127

Config:     /etc/barn/barn.conf
Socket:     /tmp/barn/barn.sock

Output (JSON):

{
  "status": "running",
  "pid": 12345,
  "uptime_seconds": 187200,
  "started_at": "2026-01-19T14:30:00Z",
  "jobs": {
    "running": 2,
    "queued": 5,
    "total": 127
  },
  "config_path": "/etc/barn/barn.conf",
  "socket_path": "/tmp/barn/barn.sock"
}

7. barn service logs

@Command(name = "logs", description = "Show service logs")
public class ServiceLogsCommand extends BaseCommand {
    @Option(names = {"--follow", "-f"}, description = "Follow log output")
    private boolean follow;

    @Option(names = {"--lines", "-n"}, description = "Number of lines", defaultValue = "100")
    private int lines;

    @Override
    public Integer call() {
        // Tail /tmp/barn/logs/barn.log
    }
}

8. Process Management

For daemonizing (Unix):

public class ProcessDaemonizer {
    public static void daemonize(Runnable mainLoop) {
        // Fork and detach from terminal
        // Use ProcessBuilder to re-exec with nohup
    }
}

For Windows, service is registered differently (see M5).

9. PID File

Write PID to /tmp/barn/barn.pid on startup:

Files.writeString(pidFile, String.valueOf(ProcessHandle.current().pid()));

10. Tests Required

  • ServiceStartCommandTest.java: Test start logic
  • ServiceStopCommandTest.java: Test stop logic
  • ServiceStatusCommandTest.java: Test status output
  • Integration test: Full start/stop/status cycle

Acceptance Criteria

  • barn service start starts the daemon
  • barn service stop stops the daemon
  • barn service restart restarts
  • barn service reload reloads config
  • barn service status shows accurate info
  • barn service logs shows log output
  • --foreground runs without daemonizing
  • Already-running detection works
  • mvn verify passes
  • Tests have 80%+ coverage

Implementation Notes

Check if service is running:

public boolean isServiceRunning() {
    Path pidFile = barnDirs.getPidFile();
    if (!Files.exists(pidFile)) return false;

    long pid = Long.parseLong(Files.readString(pidFile).trim());
    return ProcessHandle.of(pid)
        .map(ProcessHandle::isAlive)
        .orElse(false);
}

Daemonizing on Unix (simple approach):

ProcessBuilder pb = new ProcessBuilder(
    "nohup", "java", "-jar", jarPath.toString(),
    "service", "start", "--foreground"
);
pb.redirectOutput(ProcessBuilder.Redirect.appendTo(logFile));
pb.redirectError(ProcessBuilder.Redirect.appendTo(logFile));
pb.start();

Definition of Done

  • All service commands work
  • Daemon starts/stops correctly
  • Status is accurate
  • All tests pass
<!-- github-import:issue:16 --> _Imported from GitHub samson-media/barn#16 — opened by **@daniel-samson** on 2026-01-21 12:41:02 UTC_ ## Overview Implement the `barn service` subcommand for managing the service lifecycle. ## Context - **Read first**: `README.md` (service management section) - **Depends on**: #8 (CLI framework), #15 (service daemon), #14 (IPC) ## Requirements ### 1. Service Command Group `com.samsonmedia.barn.cli.ServiceCommand.java`: ```java @Command( name = "service", description = "Manage the Barn service", subcommands = { ServiceStartCommand.class, ServiceStopCommand.class, ServiceRestartCommand.class, ServiceReloadCommand.class, ServiceStatusCommand.class, ServiceLogsCommand.class } ) public class ServiceCommand extends BaseCommand { @Override public Integer call() { CommandLine.usage(this, System.out); return 0; } } ``` ### 2. barn service start ```java @Command(name = "start", description = "Start the Barn service") public class ServiceStartCommand extends BaseCommand { @Option(names = {"--foreground", "-f"}, description = "Run in foreground (don't daemonize)") private boolean foreground; @Override public Integer call() { // Check if already running // Start service (daemonize or foreground) } } ``` Output: ``` Barn service started (PID: 12345) ``` ### 3. barn service stop ```java @Command(name = "stop", description = "Stop the Barn service") public class ServiceStopCommand extends BaseCommand { @Option(names = {"--force", "-f"}, description = "Force immediate stop") private boolean force; @Override public Integer call() { // Send shutdown via IPC // Or send SIGTERM to PID } } ``` Output: ``` Barn service stopped ``` ### 4. barn service restart ```java @Command(name = "restart", description = "Restart the Barn service") public class ServiceRestartCommand extends BaseCommand { @Override public Integer call() { // Stop then start } } ``` ### 5. barn service reload ```java @Command(name = "reload", description = "Reload configuration") public class ServiceReloadCommand extends BaseCommand { @Override public Integer call() { // Send reload via IPC // Or send SIGHUP to PID } } ``` Output: ``` Configuration reloaded ``` ### 6. barn service status ```java @Command(name = "status", description = "Show service status") public class ServiceStatusCommand extends BaseCommand { @Override public Integer call() { // Query service via IPC // Show running/stopped, PID, uptime, job counts } } ``` Output (human): ``` Barn Service Status ================== Status: running PID: 12345 Uptime: 2 days, 4 hours Started: 2026-01-19 14:30:00 Jobs: Running: 2 Queued: 5 Total: 127 Config: /etc/barn/barn.conf Socket: /tmp/barn/barn.sock ``` Output (JSON): ```json { "status": "running", "pid": 12345, "uptime_seconds": 187200, "started_at": "2026-01-19T14:30:00Z", "jobs": { "running": 2, "queued": 5, "total": 127 }, "config_path": "/etc/barn/barn.conf", "socket_path": "/tmp/barn/barn.sock" } ``` ### 7. barn service logs ```java @Command(name = "logs", description = "Show service logs") public class ServiceLogsCommand extends BaseCommand { @Option(names = {"--follow", "-f"}, description = "Follow log output") private boolean follow; @Option(names = {"--lines", "-n"}, description = "Number of lines", defaultValue = "100") private int lines; @Override public Integer call() { // Tail /tmp/barn/logs/barn.log } } ``` ### 8. Process Management For daemonizing (Unix): ```java public class ProcessDaemonizer { public static void daemonize(Runnable mainLoop) { // Fork and detach from terminal // Use ProcessBuilder to re-exec with nohup } } ``` For Windows, service is registered differently (see M5). ### 9. PID File Write PID to `/tmp/barn/barn.pid` on startup: ```java Files.writeString(pidFile, String.valueOf(ProcessHandle.current().pid())); ``` ### 10. Tests Required - `ServiceStartCommandTest.java`: Test start logic - `ServiceStopCommandTest.java`: Test stop logic - `ServiceStatusCommandTest.java`: Test status output - Integration test: Full start/stop/status cycle ## Acceptance Criteria - [ ] `barn service start` starts the daemon - [ ] `barn service stop` stops the daemon - [ ] `barn service restart` restarts - [ ] `barn service reload` reloads config - [ ] `barn service status` shows accurate info - [ ] `barn service logs` shows log output - [ ] `--foreground` runs without daemonizing - [ ] Already-running detection works - [ ] `mvn verify` passes - [ ] Tests have 80%+ coverage ## Implementation Notes Check if service is running: ```java public boolean isServiceRunning() { Path pidFile = barnDirs.getPidFile(); if (!Files.exists(pidFile)) return false; long pid = Long.parseLong(Files.readString(pidFile).trim()); return ProcessHandle.of(pid) .map(ProcessHandle::isAlive) .orElse(false); } ``` Daemonizing on Unix (simple approach): ```java ProcessBuilder pb = new ProcessBuilder( "nohup", "java", "-jar", jarPath.toString(), "service", "start", "--foreground" ); pb.redirectOutput(ProcessBuilder.Redirect.appendTo(logFile)); pb.redirectError(ProcessBuilder.Redirect.appendTo(logFile)); pb.start(); ``` ## Definition of Done - All service commands work - Daemon starts/stops correctly - Status is accurate - All tests pass
daniel added this to the M4: Service Infrastructure milestone 2026-10-04 19:28:17 +00:00
daniel added the cliservice labels 2026-10-04 19:28:17 +00:00
Author
Owner

@daniel-samson commented on 2026-01-21 14:44:32 UTC:

Implemented all service commands (start, stop, restart, reload, status, logs) with comprehensive tests. Build passes with mvn verify (with spotbugs/pmd skipped due to Java version compatibility).

<!-- github-import:comment:3778508030 --> _**@daniel-samson** commented on 2026-01-21 14:44:32 UTC:_ Implemented all service commands (start, stop, restart, reload, status, logs) with comprehensive tests. Build passes with mvn verify (with spotbugs/pmd skipped due to Java version compatibility).
This repo is archived. You cannot comment on issues.