Implement macOS launchd integration #17

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

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

Overview

Implement macOS launchd integration for running Barn as a system service.

Context

  • Read first: README.md (cross-platform support)
  • Depends on: #15 (service daemon)

Requirements

1. Launchd Plist File

/Library/LaunchDaemons/com.samsonmedia.barn.plist (system-wide):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
  "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.samsonmedia.barn</string>

    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/bin/barn</string>
        <string>service</string>
        <string>start</string>
        <string>--foreground</string>
    </array>

    <key>RunAtLoad</key>
    <true/>

    <key>KeepAlive</key>
    <true/>

    <key>StandardOutPath</key>
    <string>/tmp/barn/logs/barn.log</string>

    <key>StandardErrorPath</key>
    <string>/tmp/barn/logs/barn.log</string>

    <key>WorkingDirectory</key>
    <string>/tmp/barn</string>
</dict>
</plist>

2. User Agent (Alternative)

~/Library/LaunchAgents/com.samsonmedia.barn.plist (per-user):

Same format but runs as user instead of root.

3. Installation Commands

@Command(name = "install", description = "Install as launchd service (macOS)")
public class InstallMacOsCommand extends BaseCommand {
    @Option(names = {"--user"}, description = "Install as user agent instead of daemon")
    private boolean userAgent;

    @Override
    public Integer call() {
        // 1. Generate plist file
        // 2. Copy to appropriate location
        // 3. Run launchctl load
    }
}

4. Uninstall Command

@Command(name = "uninstall", description = "Uninstall launchd service (macOS)")
public class UninstallMacOsCommand extends BaseCommand {
    @Override
    public Integer call() {
        // 1. Run launchctl unload
        // 2. Remove plist file
    }
}

5. Launchctl Operations

public class LaunchdManager {
    public void load(Path plistPath) {
        exec("launchctl", "load", plistPath.toString());
    }

    public void unload(Path plistPath) {
        exec("launchctl", "unload", plistPath.toString());
    }

    public boolean isLoaded(String label) {
        // launchctl list | grep label
    }

    public void start(String label) {
        exec("launchctl", "start", label);
    }

    public void stop(String label) {
        exec("launchctl", "stop", label);
    }
}

6. Service Status Integration

barn service status should detect launchd:

Barn Service Status
==================
Status:     running (launchd managed)
...

7. Tests Required

  • LaunchdManagerTest.java: Test launchctl operations (mock)
  • InstallMacOsCommandTest.java: Test plist generation
  • Manual test: Install/uninstall on macOS

Acceptance Criteria

  • Plist file is valid and loads correctly
  • barn service install installs on macOS
  • barn service uninstall removes cleanly
  • Service starts on boot
  • Service restarts on crash (KeepAlive)
  • Logs go to correct location
  • Both system daemon and user agent modes work
  • mvn verify passes

Implementation Notes

Detect if running under launchd:

public static boolean isUnderLaunchd() {
    return System.getenv("__CFBundleIdentifier") != null
        || ProcessHandle.current().parent()
            .map(p -> p.info().command().orElse(""))
            .map(c -> c.contains("launchd"))
            .orElse(false);
}

Plist locations:

  • System daemon: /Library/LaunchDaemons/ (requires sudo)
  • User agent: ~/Library/LaunchAgents/ (no sudo)

Definition of Done

  • Launchd integration works on macOS
  • Install/uninstall commands work
  • Service auto-starts on boot
  • All tests pass
<!-- github-import:issue:17 --> _Imported from GitHub samson-media/barn#17 — opened by **@daniel-samson** on 2026-01-21 12:41:23 UTC_ ## Overview Implement macOS launchd integration for running Barn as a system service. ## Context - **Read first**: `README.md` (cross-platform support) - **Depends on**: #15 (service daemon) ## Requirements ### 1. Launchd Plist File `/Library/LaunchDaemons/com.samsonmedia.barn.plist` (system-wide): ```xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>com.samsonmedia.barn</string> <key>ProgramArguments</key> <array> <string>/usr/local/bin/barn</string> <string>service</string> <string>start</string> <string>--foreground</string> </array> <key>RunAtLoad</key> <true/> <key>KeepAlive</key> <true/> <key>StandardOutPath</key> <string>/tmp/barn/logs/barn.log</string> <key>StandardErrorPath</key> <string>/tmp/barn/logs/barn.log</string> <key>WorkingDirectory</key> <string>/tmp/barn</string> </dict> </plist> ``` ### 2. User Agent (Alternative) `~/Library/LaunchAgents/com.samsonmedia.barn.plist` (per-user): Same format but runs as user instead of root. ### 3. Installation Commands ```java @Command(name = "install", description = "Install as launchd service (macOS)") public class InstallMacOsCommand extends BaseCommand { @Option(names = {"--user"}, description = "Install as user agent instead of daemon") private boolean userAgent; @Override public Integer call() { // 1. Generate plist file // 2. Copy to appropriate location // 3. Run launchctl load } } ``` ### 4. Uninstall Command ```java @Command(name = "uninstall", description = "Uninstall launchd service (macOS)") public class UninstallMacOsCommand extends BaseCommand { @Override public Integer call() { // 1. Run launchctl unload // 2. Remove plist file } } ``` ### 5. Launchctl Operations ```java public class LaunchdManager { public void load(Path plistPath) { exec("launchctl", "load", plistPath.toString()); } public void unload(Path plistPath) { exec("launchctl", "unload", plistPath.toString()); } public boolean isLoaded(String label) { // launchctl list | grep label } public void start(String label) { exec("launchctl", "start", label); } public void stop(String label) { exec("launchctl", "stop", label); } } ``` ### 6. Service Status Integration `barn service status` should detect launchd: ``` Barn Service Status ================== Status: running (launchd managed) ... ``` ### 7. Tests Required - `LaunchdManagerTest.java`: Test launchctl operations (mock) - `InstallMacOsCommandTest.java`: Test plist generation - Manual test: Install/uninstall on macOS ## Acceptance Criteria - [ ] Plist file is valid and loads correctly - [ ] `barn service install` installs on macOS - [ ] `barn service uninstall` removes cleanly - [ ] Service starts on boot - [ ] Service restarts on crash (KeepAlive) - [ ] Logs go to correct location - [ ] Both system daemon and user agent modes work - [ ] `mvn verify` passes ## Implementation Notes Detect if running under launchd: ```java public static boolean isUnderLaunchd() { return System.getenv("__CFBundleIdentifier") != null || ProcessHandle.current().parent() .map(p -> p.info().command().orElse("")) .map(c -> c.contains("launchd")) .orElse(false); } ``` Plist locations: - System daemon: `/Library/LaunchDaemons/` (requires sudo) - User agent: `~/Library/LaunchAgents/` (no sudo) ## Definition of Done - Launchd integration works on macOS - Install/uninstall commands work - Service auto-starts on boot - All tests pass
daniel added this to the M5: Platform Integration milestone 2026-10-04 19:28:18 +00:00
daniel added the platform label 2026-10-04 19:28:18 +00:00
Author
Owner

@daniel-samson commented on 2026-01-21 15:08:48 UTC:

Implemented macOS launchd integration:

  • LaunchdManager class for launchctl operations
  • ServiceInstallCommand for installing as launchd service
  • ServiceUninstallCommand for removing launchd service
  • Updated ServiceStatusCommand to show launchd managed status
  • Tests for LaunchdManager and commands
  • Build passes with mvn verify
<!-- github-import:comment:3778653058 --> _**@daniel-samson** commented on 2026-01-21 15:08:48 UTC:_ Implemented macOS launchd integration: - LaunchdManager class for launchctl operations - ServiceInstallCommand for installing as launchd service - ServiceUninstallCommand for removing launchd service - Updated ServiceStatusCommand to show launchd managed status - Tests for LaunchdManager and commands - Build passes with mvn verify
This repo is archived. You cannot comment on issues.