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 Method Summary collapse

Constructor Details

#initialize(project_dir, config, options = {}) ⇒ Runner

Returns a new instance of Runner.



13
14
15
16
17
18
19
20
21
22
# File 'lib/aidp/daemon/runner.rb', line 13

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

Instance Method Details

#attachObject

Attach to running daemon (restore REPL)



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/aidp/daemon/runner.rb', line 57

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)



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

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



25
26
27
28
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
# File 'lib/aidp/daemon/runner.rb', line 25

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