Class: Shoryuken::Later::CLI

Inherits:
Object
  • Object
show all
Includes:
Util, Singleton
Defined in:
lib/shoryuken/later/cli.rb

Instance Method Summary collapse

Instance Method Details

#run(args) ⇒ Object



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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/shoryuken/later/cli.rb', line 18

def run(args)
  self_read, self_write = IO.pipe

  %w[INT TERM USR1 USR2].each do |sig|
    trap sig do
      self_write.puts(sig)
    end
  end

  setup_options(args) do |cli_options|
    # this needs to happen before configuration is parsed, since it may depend on Rails env
    load_rails if cli_options[:rails]
  end
  initialize_logger
  require_workers
  validate!
  daemonize
  write_pid
  
  Shoryuken::Logging.with_context '[later]' do
    logger.info 'Starting'
    
    # Initialize the timers and poller.
    @timers = Timers::Group.new
    require 'shoryuken/later/poller'
    @pollers = Shoryuken::Later.tables.map{|tbl| Poller.new(tbl) }
      
    begin
      # Poll for items on startup, and every :poll_delay
      poll_tables
      @timers.every(Shoryuken::Later.poll_delay){ poll_tables }
      
      # Loop watching for signals and firing off of timers
      while @timers
        interval = @timers.wait_interval
        readable, writable = IO.select([self_read], nil, nil, interval)
        if readable
          handle_signal readable.first.gets.strip
        else
          @timers.fire
        end
      end
    rescue Interrupt
      @timers.cancel
      exit 0
    end
  end
end