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

Constants included from MessageDisplay

MessageDisplay::COLOR_MAP

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

#in_test_environment?, included, #message_display_prompt, #suppress_display_message?

Constructor Details

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

Returns a new instance of BackgroundRunner.



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

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.



20
21
22
# File 'lib/aidp/jobs/background_runner.rb', line 20

def jobs_dir
  @jobs_dir
end

#project_dirObject (readonly)

Returns the value of attribute project_dir.



20
21
22
# File 'lib/aidp/jobs/background_runner.rb', line 20

def project_dir
  @project_dir
end

Instance Method Details

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



29
30
31
32
# File 'lib/aidp/jobs/background_runner.rb', line 29

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

#follow_job_logs(job_id) ⇒ Object

Follow job logs in real-time



184
185
186
187
188
189
190
# File 'lib/aidp/jobs/background_runner.rb', line 184

def follow_job_logs(job_id)
  log_file = File.join(@jobs_dir, job_id, "output.log")
  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



171
172
173
174
175
176
177
178
179
180
181
# File 'lib/aidp/jobs/background_runner.rb', line 171

def job_logs(job_id, options = {})
  log_file = File.join(@jobs_dir, job_id, "output.log")
  return nil unless File.exist?(log_file)

  if options[:tail]
    lines = options[:lines] || 50
    `tail -n #{lines} #{log_file}`
  else
    File.read(log_file)
  end
end

#job_status(job_id) ⇒ Object

Get job status



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/aidp/jobs/background_runner.rb', line 108

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

  # 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: File.join(@jobs_dir, job_id, "output.log")
  }
end

#list_jobsObject

List all jobs



98
99
100
101
102
103
104
105
# File 'lib/aidp/jobs/background_runner.rb', line 98

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



36
37
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
# File 'lib/aidp/jobs/background_runner.rb', line 36

def start(mode, options = {})
  job_id = generate_job_id
  log_file = File.join(@jobs_dir, job_id, "output.log")
  pid_file = File.join(@jobs_dir, job_id, "job.pid")

  # 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



133
134
135
136
137
138
139
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
# File 'lib/aidp/jobs/background_runner.rb', line 133

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