Class: Roast::Workflow::ReplayHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/roast/workflow/replay_handler.rb

Overview

Handles replay functionality for workflows Manages skipping to specific steps and loading previous state

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(workflow, state_repository: nil) ⇒ ReplayHandler

Returns a new instance of ReplayHandler.



10
11
12
13
14
# File 'lib/roast/workflow/replay_handler.rb', line 10

def initialize(workflow, state_repository: nil)
  @workflow = workflow
  @state_repository = state_repository || StateRepositoryFactory.create(workflow.storage_type)
  @processed = false
end

Instance Attribute Details

#processedObject (readonly)

Returns the value of attribute processed.



8
9
10
# File 'lib/roast/workflow/replay_handler.rb', line 8

def processed
  @processed
end

Instance Method Details

#load_state_and_restore(step_name, timestamp: nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/roast/workflow/replay_handler.rb', line 34

def load_state_and_restore(step_name, timestamp: nil)
  state_data = if timestamp
    $stderr.puts "Looking for state before '#{step_name}' in session #{timestamp}..."
    @state_repository.load_state_before_step(@workflow, step_name, timestamp: timestamp)
  else
    $stderr.puts "Looking for state before '#{step_name}' in most recent session..."
    @state_repository.load_state_before_step(@workflow, step_name)
  end

  if state_data
    $stderr.puts "Successfully loaded state with data from previous step"
    restore_workflow_state(state_data)
  else
    session_info = timestamp ? " in session #{timestamp}" : ""
    $stderr.puts "Could not find suitable state data from a previous step to '#{step_name}'#{session_info}."
    $stderr.puts "Will run workflow from '#{step_name}' without prior context."
  end

  state_data
end

#process_replay(steps, replay_option) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/roast/workflow/replay_handler.rb', line 16

def process_replay(steps, replay_option)
  return steps unless replay_option && !@processed

  timestamp, step_name = parse_replay_option(replay_option)
  skip_index = StepFinder.find_index(steps, step_name)

  if skip_index
    $stderr.puts "Replaying from step: #{step_name}#{timestamp ? " (session: #{timestamp})" : ""}"
    @workflow.session_timestamp = timestamp if timestamp && @workflow.respond_to?(:session_timestamp=)
    steps = load_state_and_get_remaining_steps(steps, skip_index, step_name, timestamp)
  else
    $stderr.puts "Step #{step_name} not found in workflow, running from beginning"
  end

  @processed = true
  steps
end