Module: FunCi::Persistence::StageJob

Defined in:
lib/fun_ci/persistence/stage_job.rb

Constant Summary collapse

TERMINAL_STATUSES =
%w[completed failed timed_out cancelled].freeze

Class Method Summary collapse

Class Method Details

.create(db, pipeline_run_id:, stage:) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/fun_ci/persistence/stage_job.rb', line 10

def self.create(db, pipeline_run_id:, stage:)
  db.execute(
    "INSERT INTO stage_jobs (pipeline_run_id, stage, status) VALUES (?, ?, 'scheduled')",
    [pipeline_run_id, stage]
  )
  db.last_insert_row_id
end

.elapsed_duration(job) ⇒ Object



35
36
37
38
# File 'lib/fun_ci/persistence/stage_job.rb', line 35

def self.elapsed_duration(job)
  return nil unless job[:started_at] && job[:completed_at]
  Time.parse(job[:completed_at]) - Time.parse(job[:started_at])
end

.find(db, id) ⇒ Object



18
19
20
21
22
# File 'lib/fun_ci/persistence/stage_job.rb', line 18

def self.find(db, id)
  row = db.execute("SELECT id, pipeline_run_id, stage, status, started_at, completed_at FROM stage_jobs WHERE id = ?", [id]).first
  return nil unless row
  row_to_hash(row)
end

.update_status(db, id, new_status) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/fun_ci/persistence/stage_job.rb', line 24

def self.update_status(db, id, new_status)
  now = Time.now.utc.iso8601
  if new_status == "running"
    db.execute("UPDATE stage_jobs SET status = ?, started_at = ? WHERE id = ?", [new_status, now, id])
  elsif TERMINAL_STATUSES.include?(new_status)
    db.execute("UPDATE stage_jobs SET status = ?, completed_at = ? WHERE id = ?", [new_status, now, id])
  else
    db.execute("UPDATE stage_jobs SET status = ? WHERE id = ?", [new_status, id])
  end
end