Class: Aidp::Database::Repositories::JobRepository
- Inherits:
-
Aidp::Database::Repository
- Object
- Aidp::Database::Repository
- Aidp::Database::Repositories::JobRepository
- Defined in:
- lib/aidp/database/repositories/job_repository.rb
Overview
Repository for background_jobs table Replaces jobs/* directory (metadata, logs, PID tracking)
Constant Summary collapse
- VALID_STATUSES =
%w[pending running completed failed stopped].freeze
Instance Attribute Summary
Attributes inherited from Aidp::Database::Repository
Instance Method Summary collapse
-
#cleanup(days_to_keep: 7) ⇒ Integer
Cleanup old completed jobs.
-
#complete(job_id, output: {}) ⇒ Object
Complete a job.
-
#create(job_type:, input: {}) ⇒ String
Create a new job.
-
#delete(job_id) ⇒ Object
Delete a job.
-
#fail(job_id, error:) ⇒ Object
Fail a job.
-
#find(job_id) ⇒ Hash?
Find job by ID.
-
#initialize(project_dir: Dir.pwd) ⇒ JobRepository
constructor
A new instance of JobRepository.
-
#list(status: nil, limit: 50) ⇒ Array<Hash>
List all jobs.
-
#running ⇒ Array<Hash>
List running jobs.
-
#start(job_id, pid: nil) ⇒ Object
Start a job.
-
#status(job_id) ⇒ Hash?
Get job status.
-
#stop(job_id) ⇒ Object
Stop a job.
Constructor Details
#initialize(project_dir: Dir.pwd) ⇒ JobRepository
Returns a new instance of JobRepository.
14 15 16 |
# File 'lib/aidp/database/repositories/job_repository.rb', line 14 def initialize(project_dir: Dir.pwd) super(project_dir: project_dir, table_name: "background_jobs") end |
Instance Method Details
#cleanup(days_to_keep: 7) ⇒ Integer
Cleanup old completed jobs
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/aidp/database/repositories/job_repository.rb', line 216 def cleanup(days_to_keep: 7) threshold = (Time.now - (days_to_keep * 24 * 60 * 60)).strftime("%Y-%m-%d %H:%M:%S") count = query_value( <<~SQL, SELECT COUNT(*) FROM background_jobs WHERE project_dir = ? AND status IN ('completed', 'failed', 'stopped') AND completed_at < ? SQL [project_dir, threshold] ) || 0 execute( <<~SQL, DELETE FROM background_jobs WHERE project_dir = ? AND status IN ('completed', 'failed', 'stopped') AND completed_at < ? SQL [project_dir, threshold] ) Aidp.log_debug("job_repository", "cleanup", deleted: count, threshold: threshold) count end |
#complete(job_id, output: {}) ⇒ Object
Complete a job
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/aidp/database/repositories/job_repository.rb', line 72 def complete(job_id, output: {}) now = execute( <<~SQL, UPDATE background_jobs SET status = 'completed', result = ?, completed_at = ? WHERE id = ? AND project_dir = ? SQL [serialize_json(output), now, job_id, project_dir] ) Aidp.log_debug("job_repository", "completed", job_id: job_id) end |
#create(job_type:, input: {}) ⇒ String
Create a new job
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/aidp/database/repositories/job_repository.rb', line 23 def create(job_type:, input: {}) job_id = generate_job_id now = execute( insert_sql([ :id, :project_dir, :job_type, :status, :options, :created_at ]), [ job_id, project_dir, job_type, "pending", serialize_json(input), now ] ) Aidp.log_debug("job_repository", "created", job_id: job_id, type: job_type) job_id end |
#delete(job_id) ⇒ Object
Delete a job
204 205 206 207 208 209 210 |
# File 'lib/aidp/database/repositories/job_repository.rb', line 204 def delete(job_id) execute( "DELETE FROM background_jobs WHERE id = ? AND project_dir = ?", [job_id, project_dir] ) Aidp.log_debug("job_repository", "deleted", job_id: job_id) end |
#fail(job_id, error:) ⇒ Object
Fail a job
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/aidp/database/repositories/job_repository.rb', line 93 def fail(job_id, error:) now = execute( <<~SQL, UPDATE background_jobs SET status = 'failed', error = ?, completed_at = ? WHERE id = ? AND project_dir = ? SQL [error, now, job_id, project_dir] ) Aidp.log_debug("job_repository", "failed", job_id: job_id, error: error) end |
#find(job_id) ⇒ Hash?
Find job by ID
133 134 135 136 137 138 139 |
# File 'lib/aidp/database/repositories/job_repository.rb', line 133 def find(job_id) row = query_one( "SELECT * FROM background_jobs WHERE id = ? AND project_dir = ?", [job_id, project_dir] ) deserialize_job(row) end |
#list(status: nil, limit: 50) ⇒ Array<Hash>
List all jobs
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 |
# File 'lib/aidp/database/repositories/job_repository.rb', line 146 def list(status: nil, limit: 50) rows = if status query( <<~SQL, SELECT * FROM background_jobs WHERE project_dir = ? AND status = ? ORDER BY created_at DESC LIMIT ? SQL [project_dir, status, limit] ) else query( <<~SQL, SELECT * FROM background_jobs WHERE project_dir = ? ORDER BY created_at DESC LIMIT ? SQL [project_dir, limit] ) end rows.map { |row| deserialize_job(row) } end |
#running ⇒ Array<Hash>
List running jobs
175 176 177 |
# File 'lib/aidp/database/repositories/job_repository.rb', line 175 def running list(status: "running") end |
#start(job_id, pid: nil) ⇒ Object
Start a job
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/aidp/database/repositories/job_repository.rb', line 51 def start(job_id, pid: nil) now = execute( <<~SQL, UPDATE background_jobs SET status = 'running', pid = ?, started_at = ? WHERE id = ? AND project_dir = ? SQL [pid, now, job_id, project_dir] ) Aidp.log_debug("job_repository", "started", job_id: job_id, pid: pid) end |
#status(job_id) ⇒ Hash?
Get job status
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/aidp/database/repositories/job_repository.rb', line 183 def status(job_id) job = find(job_id) return nil unless job pid = job[:pid] { job_id: job_id, mode: job[:job_type], status: job[:status], pid: pid, running: pid && process_running?(pid), started_at: job[:started_at], completed_at: job[:completed_at], error: job[:error] } end |
#stop(job_id) ⇒ Object
Stop a job
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/aidp/database/repositories/job_repository.rb', line 113 def stop(job_id) now = execute( <<~SQL, UPDATE background_jobs SET status = 'stopped', completed_at = ? WHERE id = ? AND project_dir = ? SQL [now, job_id, project_dir] ) Aidp.log_debug("job_repository", "stopped", job_id: job_id) end |