Class: ChaoticJob::Scenario

Inherits:
Object
  • Object
show all
Defined in:
lib/chaotic_job/scenario.rb

Defined Under Namespace

Classes: Event

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(job, glitch:, raise: RetryableError, capture: nil) ⇒ Scenario

Returns a new instance of Scenario.



12
13
14
15
16
17
18
# File 'lib/chaotic_job/scenario.rb', line 12

def initialize(job, glitch:, raise: RetryableError, capture: nil)
  @job = job
  @glitch = (Glitch === glitch) ? glitch : (raise Error.new("glitch: must be a Glitch instance, but got #{glitch.inspect}"))
  @raise = binding.local_variable_get(:raise)
  @capture = capture
  @events = []
end

Instance Attribute Details

#eventsObject (readonly)

Returns the value of attribute events.



8
9
10
# File 'lib/chaotic_job/scenario.rb', line 8

def events
  @events
end

#glitchObject (readonly)

Returns the value of attribute glitch.



8
9
10
# File 'lib/chaotic_job/scenario.rb', line 8

def glitch
  @glitch
end

#jobObject (readonly)

Returns the value of attribute job.



8
9
10
# File 'lib/chaotic_job/scenario.rb', line 8

def job
  @job
end

Instance Method Details

#before_call?(key) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
52
# File 'lib/chaotic_job/scenario.rb', line 48

def before_call?(key)
  return false unless :call == @glitch.event

  key == @glitch.key
end

#before_line?(key) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
45
46
# File 'lib/chaotic_job/scenario.rb', line 42

def before_line?(key)
  return false unless :line == @glitch.event

  key == @glitch.key
end

#before_return?(key) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
57
58
# File 'lib/chaotic_job/scenario.rb', line 54

def before_return?(key)
  return false unless :return == @glitch.event

  key == @glitch.key
end

#glitched?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/chaotic_job/scenario.rb', line 38

def glitched?
  @glitch.executed?
end

#run(&block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/chaotic_job/scenario.rb', line 20

def run(&block)
  @job.class.retry_on RetryableError, attempts: 10, wait: 1, jitter: 0
  @glitch.set_action { raise @raise }

  ActiveSupport::Notifications.subscribed(->(*args) { @events << Event.new(*args) }, @capture) do
    @glitch.inject! do
      @job.enqueue
      if block
        block.call
      else
        Performer.perform_all
      end
    end
  end

  self
end

#to_sObject



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
# File 'lib/chaotic_job/scenario.rb', line 60

def to_s
  # ChaoticJob::Scenario(
  #   job: Job(arguments),
  #   glitch: Glitch()
  # )
  buffer = +"ChaoticJob::Scenario(\n"

  job_attributes = @job.serialize
  buffer << "  job: #{job_attributes["job_class"]}"
  buffer << "("
  buffer << job_attributes["arguments"].join(", ")
  buffer << "),\n"

  glitch_start, *glitch_lines = @glitch.to_s.split("\n")
  buffer << "  glitch: #{glitch_start}\n"
  glitch_lines.each do |line|
    buffer << "  #{line}\n"
  end
  buffer << "  events: [\n"
  @events.sort_by { |it| it.started }.each do |it|
    buffer << "    #{it.started.utc.iso8601(6)}: #{it.name}\n"
  end
  buffer << "  ]\n"
  buffer << ")"

  buffer
end