Class: Stormtroopers::Trooper

Inherits:
Object
  • Object
show all
Defined in:
lib/stormtroopers/trooper.rb

Direct Known Subclasses

DelayedJobTrooper, DummyTrooper

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parameters = {}, &block) ⇒ Trooper

Returns a new instance of Trooper.



5
6
7
8
# File 'lib/stormtroopers/trooper.rb', line 5

def initialize(parameters = {}, &block)
  @parameters = parameters
  @task = block
end

Instance Attribute Details

#parametersObject (readonly)

Returns the value of attribute parameters.



3
4
5
# File 'lib/stormtroopers/trooper.rb', line 3

def parameters
  @parameters
end

#started_atObject (readonly)

Returns the value of attribute started_at.



3
4
5
# File 'lib/stormtroopers/trooper.rb', line 3

def started_at
  @started_at
end

#taskObject (readonly)

Returns the value of attribute task.



3
4
5
# File 'lib/stormtroopers/trooper.rb', line 3

def task
  @task
end

Instance Method Details

#after_runObject



23
24
25
# File 'lib/stormtroopers/trooper.rb', line 23

def after_run
  # Empty hook for overriding
end

#before_runObject



19
20
21
# File 'lib/stormtroopers/trooper.rb', line 19

def before_run
  # Empty hook for overriding
end

#exception(exception) ⇒ Object



27
28
29
30
31
32
# File 'lib/stormtroopers/trooper.rb', line 27

def exception(exception)
  # Hook for to override handling exceptions
  logger.error "Error processing #{name}: #{exception.message}"
  logger.debug "Stacktrace #{name}:\n#{exception.backtrace.join("\n")}"
  raise exception
end

#nameObject



10
11
12
13
# File 'lib/stormtroopers/trooper.rb', line 10

def name
  # Override with a useful name here for logging purposes
  @task.to_s
end

#runObject



34
35
36
# File 'lib/stormtroopers/trooper.rb', line 34

def run
  task.call
end

#startObject



38
39
40
41
42
43
44
45
# File 'lib/stormtroopers/trooper.rb', line 38

def start
  @started_at = Time.now
  before_run
  run
  after_run
rescue => e
  exception(e)
end

#statusObject



15
16
17
# File 'lib/stormtroopers/trooper.rb', line 15

def status
  "#{name} running since #{started_at.strftime("%Y-%m-%d %H:%M:%S")} (#{(Time.now - started_at).to_i} seconds)" if started_at
end