Class: KeepRunning

Inherits:
Object
  • Object
show all
Defined in:
lib/keep_running.rb,
lib/keep_running/version.rb

Constant Summary collapse

VERSION =
'0.0.4'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.load_runner(runner) ⇒ Object



6
7
8
# File 'lib/keep_running.rb', line 6

def load_runner(runner)
  load @runner_location = runner
end

.process_argvObject



14
15
16
# File 'lib/keep_running.rb', line 14

def process_argv
  ARGV.shift if File.basename($0) == 'keep_running'
end

.run(&block) ⇒ Object



18
19
20
21
22
23
# File 'lib/keep_running.rb', line 18

def run(&block)
  runner = new
  process_argv
  runner.instance_eval &block
  runner.run
end

.runner_locationObject



10
11
12
# File 'lib/keep_running.rb', line 10

def runner_location
  @runner_location ||= File.expand_path($0)
end

Instance Method Details

#email(e) ⇒ Object



27
# File 'lib/keep_running.rb', line 27

def email(e)              ; @email_opts = e         ; end

#pidfile(p) ⇒ Object



26
# File 'lib/keep_running.rb', line 26

def pidfile(p)            ; @pidfile = p            ; end

#process(p) ⇒ Object



30
31
32
# File 'lib/keep_running.rb', line 30

def process(p)
  @process = File.expand_path(File.join(File.dirname(self.class.runner_location), p))
end

#restart_delay(d) ⇒ Object



29
# File 'lib/keep_running.rb', line 29

def restart_delay(d)      ; @restart_delay = d      ; end

#restart_threshold(t) ⇒ Object



28
# File 'lib/keep_running.rb', line 28

def restart_threshold(t)  ; @restart_threshold = t  ; end

#runObject



34
35
36
37
38
39
40
41
42
43
# File 'lib/keep_running.rb', line 34

def run
  write_pidfile
  
  output = nil
  loop do
    output = spawn { start_process }              # load the given script
    send_mail subject(@process), output           # send the email
    throttle_restart                              # don't restart too fast
  end
end