Module: Angels

Defined in:
lib/angels.rb,
lib/angels/version.rb

Constant Summary collapse

VERSION =
"0.0.9"

Class Method Summary collapse

Class Method Details

.run_loop(name, opts = {}, &block) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/angels.rb', line 6

def Angels.run_loop(name, opts = {}, &block)
  caller_dir = File.expand_path(File.dirname(caller[0].scan(/^([^:]*):/).first.first))
  opts = {
    :loop_sleep   => 60,
    :retry        => true,
    :retry_sleep  => 60,
    :rails_root   => File.join(caller_dir, '..'), 
    :load_rails   => false,
    :pid_dir      => File.join(caller_dir, '..', 'tmp', 'pids'),
    :default_env  => 'production',
  }.merge(opts)

  Daemons.run_proc(name, :dir_mode => :normal, :dir => opts[:pid_dir]) do 
    if opts[:load_rails]
      ENV['RAILS_ENV'] ||= opts[:default_env]
      require File.join(opts[:rails_root], 'config', 'environment')
    end

    loop {
      begin
        yield
        sleep opts[:loop_sleep]
      rescue SignalException, Interrupt; SystemExit; Process.exit
      rescue => e
        handler = opts[:exception_handler]
        if handler
          if handler.is_a? Symbol
            case handler
            when :hoptoad; HoptoadNotifier.notify(e)
            else raise handler.inspect 
            end
          else
            handler.call(e)
          end
        end
        raise e unless opts[:retry]
        sleep opts[:retry_sleep]
      end
    }
  end
end