lib/kryten/runner.rb in kryten-0.1.0 vs lib/kryten/runner.rb in kryten-0.2.0
- old
+ new
@@ -1,48 +1,69 @@
module Kryten::Runner
+ attr_accessor :timer
+ attr_reader :running, :started
+ def initialize title=nil
+ @name, @running, @started = title, false, false
+ log "initializing"
+ Signal.trap("INT", proc { stop_running })
+ end
+
def start
- log "starting #{name}"
setup
+ log "starting"
+ @started = true
- @running = true
- Signal.trap("INT", proc { stop_running })
- Signal.trap("TERM", proc { stop_running })
-
- while @running do
- sleep @timer || 4
+ while started do
+ sleep timer
+ @running = true
run
+ @running = false
end
- log "stopped #{name}"
+ log "stopped"
+ true
rescue => e
log "error #{e}"
#raise
end
def run
- log "running #{name}"
+ log "running"
end
+ def timer
+ @timer || 4
+ end
+
def stop_running
- # no logging here, this is a signal handler
- @running = false
+ @started = false
end
def setup
- log "setting up #{name}"
+ log "setting up"
end
def debug
- log "debugging #{name}"
+ log "debugging"
setup
2.times do
run
- sleep @timer || 4
+ sleep timer
end
end
def log message
- puts message
+ puts [name, message].join(': ')
+ end
+
+ def name
+ @name || self.class.to_s.gsub('::','-').downcase
+ end
+
+ def status
+ log [started ? 'on and ' : 'off and ',
+ running ? 'running' : 'sleeping'].join
+ started
end
end