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

#debugObject



44
45
46
47
48
49
50
51
# File 'lib/kryten/runner.rb', line 44

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



53
54
55
# File 'lib/kryten/runner.rb', line 53

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

#nameObject



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

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

#runObject



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

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

hook methods



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

def shutdown; nil; end

#startObject



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

def start
  setup
  log "started"
  @started = true

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

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

#statusObject



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

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

#stop_runningObject

stop the loop



39
40
41
42
# File 'lib/kryten/runner.rb', line 39

def stop_running
  @started = false
  shutdown
end