Module: Kryten::Runner

Included in:
Task
Defined in:
lib/kryten/runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#runningObject (readonly)

Returns the value of attribute running.



3
4
5
# File 'lib/kryten/runner.rb', line 3

def running
  @running
end

#startedObject (readonly)

Returns the value of attribute started.



3
4
5
# File 'lib/kryten/runner.rb', line 3

def started
  @started
end

#timerObject

Returns the value of attribute timer.



2
3
4
# File 'lib/kryten/runner.rb', line 2

def timer
  @timer
end

Instance Method Details

#after_runObject



77
# File 'lib/kryten/runner.rb', line 77

def after_run; nil; end

#after_setupObject



74
# File 'lib/kryten/runner.rb', line 74

def after_setup; nil; end

#before_runObject



75
# File 'lib/kryten/runner.rb', line 75

def before_run; nil; end

#before_setupObject

hook methods



73
# File 'lib/kryten/runner.rb', line 73

def before_setup; nil; end

#debugObject



49
50
51
52
53
54
55
56
# File 'lib/kryten/runner.rb', line 49

def debug
  log "debugging"
  setup
  2.times do
    run
    sleep timer
  end
end

#initialize(title = nil) ⇒ Object



5
6
7
# File 'lib/kryten/runner.rb', line 5

def initialize title=nil
  @name, @running, @started = title, false, false
end

#log(message) ⇒ Object



58
59
60
# File 'lib/kryten/runner.rb', line 58

def log message
  puts [name, message].join(': ')
end

#nameObject



62
63
64
# File 'lib/kryten/runner.rb', line 62

def name
  @name || self.class.to_s.gsub('::','-').downcase
end

#runObject



76
# File 'lib/kryten/runner.rb', line 76

def run; nil; end

#setupObject



9
10
11
12
13
# File 'lib/kryten/runner.rb', line 9

def setup
  log "setting up"
  Signal.trap("INT", proc { stop_running })
  Signal.trap("TERM", proc { stop_running })
end

#shutdownObject



78
# File 'lib/kryten/runner.rb', line 78

def shutdown; nil; end

#startObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/kryten/runner.rb', line 15

def start
  before_setup
  setup
  after_setup

  log "started"
  @started = true

  while started do
    sleep timer
    @running = true
    before_run
    run
    after_run
    @running = false
  end

  log "stopped"
  true
rescue => e
  log :error, "error: #{e} - from: #{e.backtrace.first}"
  raise
end

#statusObject



66
67
68
69
70
# File 'lib/kryten/runner.rb', line 66

def status
  log [started ? 'on and ' : 'off and ',
       running ? 'running' : 'sleeping'].join
  started
end

#stop_runningObject

stop the loop



44
45
46
47
# File 'lib/kryten/runner.rb', line 44

def stop_running
  @started = false
  shutdown
end