Class: Aidp::Jobs::BackgroundRunner

Inherits:
Object
  • Object
show all
Includes:
MessageDisplay, RescueLogging, SafeDirectory
Defined in:
lib/aidp/jobs/background_runner.rb

Overview

Manages background execution of work loops Runs harness in daemon process and tracks job metadata

Constant Summary collapse

StartError =
Class.new(StandardError)

Constants included from MessageDisplay

MessageDisplay::COLOR_MAP, MessageDisplay::CRITICAL_TYPES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SafeDirectory

#safe_mkdir_p

Methods included from RescueLogging

__log_rescue_impl, #log_rescue, log_rescue

Methods included from MessageDisplay

included, #message_display_prompt, #quiet_mode?

Constructor Details

#initialize(project_dir = Dir.pwd, suppress_display: false) ⇒ BackgroundRunner

Returns a new instance of BackgroundRunner.



24
25
26
27
28
29
# File 'lib/aidp/jobs/background_runner.rb', line 24

def initialize(project_dir = Dir.pwd, suppress_display: false)
  @project_dir = project_dir
  @jobs_dir = File.join(project_dir, ".aidp", "jobs")
  @suppress_display = suppress_display
  ensure_jobs_directory
end

Instance Attribute Details

#jobs_dirObject (readonly)

Returns the value of attribute jobs_dir.



22
23
24
# File 'lib/aidp/jobs/background_runner.rb', line 22

def jobs_dir
  @jobs_dir
end

#project_dirObject (readonly)

Returns the value of attribute project_dir.



22
23
24
# File 'lib/aidp/jobs/background_runner.rb', line 22

def project_dir
  @project_dir
end

Instance Method Details

#display_message(msg, type: :info) ⇒ Object



31
32
33
34
# File 'lib/aidp/jobs/background_runner.rb', line 31

def display_message(msg, type: :info)
  return if @suppress_display
  super
end

#follow_job_logs(job_id) ⇒ Object

Follow job logs in real-time



191
192
193
194
195
196
197
198
# File 'lib/aidp/jobs/background_runner.rb', line 191

def follow_job_logs(job_id)
  log_file = job_log_path(job_id)
  return unless log_file
  return unless File.exist?(log_file)

  # Use tail -f to follow logs
  exec("tail", "-f", log_file)
end

#job_logs(job_id, options = {}) ⇒ Object

Get job logs



178
179
180
181
182
183
184
185
186
187
188
# File 'lib/aidp/jobs/background_runner.rb', line 178

def job_logs(job_id, options = {})
  log_file = job_log_path(job_id)
  return nil unless log_file
  return nil unless File.exist?(log_file)

  if options[:tail]
    tail_job_logs(log_file, options[:lines])
  else
    File.read(log_file)
  end
end

#job_status(job_id) ⇒ Object

Get job status



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/aidp/jobs/background_runner.rb', line 112

def job_status(job_id)
   = (job_id)
  return nil unless 

  log_file = job_log_path(job_id)
  return nil unless log_file

  # Check if process is still running
  pid = [:pid]
  running = pid && process_running?(pid)

  # Get checkpoint data
  checkpoint = get_job_checkpoint(job_id)

  {
    job_id: job_id,
    mode: [:mode],
    status: determine_job_status(, running, checkpoint),
    pid: pid,
    running: running,
    started_at: [:started_at],
    completed_at: [:completed_at],
    checkpoint: checkpoint,
    log_file: log_file
  }
end

#list_jobsObject

List all jobs



102
103
104
105
106
107
108
109
# File 'lib/aidp/jobs/background_runner.rb', line 102

def list_jobs
  return [] unless Dir.exist?(@jobs_dir)

  Dir.glob(File.join(@jobs_dir, "*")).select { |d| File.directory?(d) }.map do |job_dir|
    job_id = File.basename(job_dir)
    (job_id)
  end.compact.sort_by { |job| job[:started_at] || Time.now }.reverse
end

#start(mode, options = {}) ⇒ Object

Start a background job Returns job_id



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/aidp/jobs/background_runner.rb', line 38

def start(mode, options = {})
  ensure_jobs_directory
  job_id = generate_job_id
  log_file = job_file_path(job_id, "output.log")
  pid_file = job_file_path(job_id, "job.pid")
  raise_start_error unless log_file && pid_file

  # Create job directory
  FileUtils.mkdir_p(File.dirname(log_file))

  # Fork and daemonize
  pid = fork do
    # Detach from parent process
    Process.daemon(true)

    # Redirect stdout/stderr to log file
    $stdout.reopen(log_file, "a")
    $stderr.reopen(log_file, "a")
    $stdout.sync = true
    $stderr.sync = true

    # Write PID file
    File.write(pid_file, Process.pid)

    begin
      # Run the harness
      puts "[#{Time.now}] Starting #{mode} mode in background"
      puts "[#{Time.now}] Job ID: #{job_id}"
      puts "[#{Time.now}] PID: #{Process.pid}"

      runner = Aidp::Harness::Runner.new(@project_dir, mode, options.merge(job_id: job_id))
      result = runner.run

      puts "[#{Time.now}] Job completed with status: #{result[:status]}"
      mark_job_completed(job_id, result)
    rescue => e
      log_rescue(e, component: "background_runner", action: "execute_job", fallback: "mark_failed", job_id: job_id, mode: mode)
      puts "[#{Time.now}] Job failed with error: #{e.message}"
      puts e.backtrace.join("\n")
      mark_job_failed(job_id, e)
    ensure
      # Clean up PID file
      File.delete(pid_file) if File.exist?(pid_file)
    end
  end

  # Wait for child to fork
  Process.detach(pid)

  # Wait for daemon to write PID file (with timeout)
  begin
    Aidp::Concurrency::Wait.for_file(pid_file, timeout: 5, interval: 0.05)
  rescue Aidp::Concurrency::TimeoutError
    # PID file not created - daemon may have failed to start
    # Continue anyway, metadata will reflect this
  end

  # Save job metadata in parent process
  (job_id, pid, mode, options)

  job_id
end

#stop_job(job_id) ⇒ Object

Stop a running job



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/aidp/jobs/background_runner.rb', line 140

def stop_job(job_id)
   = (job_id)
  return {success: false, message: "Job not found"} unless 

  pid = [:pid]
  unless pid && process_running?(pid)
    return {success: false, message: "Job is not running"}
  end

  begin
    # Send TERM signal
    Process.kill("TERM", pid)

    # Wait for process to terminate (max 10 seconds)
    10.times do
      sleep 0.5
      break unless process_running?(pid)
    end

    # Force kill if still running
    if process_running?(pid)
      Process.kill("KILL", pid)
    end

    mark_job_stopped(job_id)
    {success: true, message: "Job stopped successfully"}
  rescue Errno::ESRCH => e
    log_rescue(e, component: "background_runner", action: "stop_job", fallback: "mark_stopped", job_id: job_id, pid: pid, level: :info)
    # Process already dead
    mark_job_stopped(job_id)
    {success: true, message: "Job was already stopped"}
  rescue => e
    log_rescue(e, component: "background_runner", action: "stop_job", fallback: "error_result", job_id: job_id, pid: pid)
    {success: false, message: "Failed to stop job: #{e.message}"}
  end
end