Class: Aidp::Daemon::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/aidp/daemon/runner.rb

Overview

Main daemon runner for background mode execution Manages work loops, watch mode, and IPC communication

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_dir, config, options = {}, process_manager: nil, work_loop_runner: nil, watch_runner: nil, ipc_server: nil) ⇒ Runner

Returns a new instance of Runner.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/aidp/daemon/runner.rb', line 16

def initialize(project_dir, config, options = {}, process_manager: nil,
  work_loop_runner: nil, watch_runner: nil, ipc_server: nil)
  @project_dir = project_dir
  @config = config
  @options = options
  @process_manager = process_manager || ProcessManager.new(project_dir)
  @running = false
  @work_loop_runner = work_loop_runner
  @watch_runner = watch_runner
  @ipc_server = ipc_server
end

Instance Attribute Details

#runningObject

Allow reading/writing @running for testability



14
15
16
# File 'lib/aidp/daemon/runner.rb', line 14

def running
  @running
end

Instance Method Details

#attachObject

Attach to running daemon (restore REPL)



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/aidp/daemon/runner.rb', line 61

def attach
  unless @process_manager.running?
    return {success: false, message: "No daemon running"}
  end

  unless @process_manager.socket_exists?
    return {success: false, message: "Daemon socket not available"}
  end

  {
    success: true,
    message: "Attached to daemon",
    pid: @process_manager.pid,
    activity: Aidp.logger.info("activity", "summary")
  }
end

#run_daemon(mode) ⇒ Object

Run daemon main loop (called in forked process)



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/aidp/daemon/runner.rb', line 79

def run_daemon(mode)
  Aidp.logger.info("daemon_lifecycle", "Daemon started", mode: mode, pid: Process.pid)
  @running = true

  # Set up signal handlers
  setup_signal_handlers

  # Start IPC server
  start_ipc_server

  # Run appropriate mode
  case mode
  when :watch
    run_watch_mode
  when :work_loop
    run_work_loop_mode
  else
    Aidp.logger.error("daemon_error", "Unknown mode: #{mode}")
  end
rescue => e
  Aidp.logger.error("daemon_error", "Fatal error: #{e.message}", backtrace: e.backtrace.first(5).join("\n"))
ensure
  cleanup
end

#start_daemon(mode: :watch) ⇒ Object

Start daemon in background



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/aidp/daemon/runner.rb', line 29

def start_daemon(mode: :watch)
  if @process_manager.running?
    return {success: false, message: "Daemon already running (PID: #{@process_manager.pid})"}
  end

  # Fork daemon process
  daemon_pid = fork do
    Process.daemon(true)
    @process_manager.write_pid
    run_daemon(mode)
  end

  Process.detach(daemon_pid)

  # Wait for daemon to start (check if it's running)
  begin
    Aidp::Concurrency::Wait.until(timeout: 5, interval: 0.1) do
      @process_manager.running?
    end

    {
      success: true,
      message: "Daemon started in #{mode} mode",
      pid: daemon_pid,
      log_file: @process_manager.log_file_path
    }
  rescue Aidp::Concurrency::TimeoutError
    {success: false, message: "Failed to start daemon (timeout)"}
  end
end