Class: Kitsune::Kit::RunJournal

Inherits:
Object
  • Object
show all
Defined in:
lib/kitsune/kit/run_journal.rb

Constant Summary collapse

RESUMABLE_STATUSES =
%w[running failure cancelled].freeze

Instance Method Summary collapse

Constructor Details

#initialize(state_store:, environment:, run_id:, clock: -> { Time.now.utc }) ⇒ RunJournal

Returns a new instance of RunJournal.



12
13
14
15
16
17
# File 'lib/kitsune/kit/run_journal.rb', line 12

def initialize(state_store:, environment:, run_id:, clock: -> { Time.now.utc })
  @state_store = state_store
  @environment = environment
  @run_id = run_id
  @clock = clock
end

Instance Method Details

#finish(status, error: nil) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/kitsune/kit/run_journal.rb', line 43

def finish(status, error: nil)
  update_run do |run|
    run["status"] = status
    run["error"] = error
    run["finished_at"] = timestamp
  end
end

#record_step(resource, status, error: nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/kitsune/kit/run_journal.rb', line 32

def record_step(resource, status, error: nil)
  update_run do |run|
    run["steps"] ||= {}
    run["steps"][resource] = {
      "status" => status,
      "error" => error,
      "updated_at" => timestamp
    }
  end
end

#resumable(requested_id = nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/kitsune/kit/run_journal.rb', line 51

def resumable(requested_id = nil)
  runs = state_store.read(environment)["runs"]
  selected_id = requested_id || runs.keys.reverse.find { |id| resumable_status?(runs.fetch(id)) }
  raise no_resumable_run_error unless selected_id

  run = runs[selected_id]
  raise Errors::ConfigurationError, "run not found: #{selected_id}" unless run

  validate_resumable!(selected_id, run)
  [selected_id, run]
end

#start(changes:, resumed_from: nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/kitsune/kit/run_journal.rb', line 19

def start(changes:, resumed_from: nil)
  update_run do |run|
    run.merge!(
      "status" => "running",
      "started_at" => timestamp,
      "finished_at" => nil,
      "resumed_from" => resumed_from,
      "changes" => normalize(changes),
      "steps" => {}
    )
  end
end