Sha256: a9c4a1b02b13bd5ec9c6dd57b1d83329ec9d7f05982dfb08419f5c67832e3ebc

Contents?: true

Size: 1.85 KB

Versions: 2

Compression:

Stored size: 1.85 KB

Contents

require File.dirname(__FILE__) + '/daemoned'

module Sweatshop
  class Sweatd
    include Daemoned
    queues     = []
    groups     = []
    rails_root = nil
    start_cmd  = "ruby #{__FILE__} start #{ARGV.reject{|a| a == 'start'}.join(' ')}"

    arg '--workers=Worker,Worker', 'Workers to service (Default is all)' do |value|
      queues = value.split(',')
    end

    arg '--groups=GROUP,GROUP', 'Groups of queues to service' do |value|
      groups = value.split(',').collect{|g| g.to_sym}
    end

    arg '--worker-file=WORKERFILE', 'Worker file to load'  do |value|
      require value
    end

    arg '--worker-dir=WORKERDIR', 'Directory containing workers'  do |value|
      Dir.glob(value + '*.rb').each{|worker| require worker}
    end

    arg '--rails=DIR', 'Pass in RAILS_ROOT to run this daemon in a rails environment' do |value|
      rails_root = value
    end

    sig(:term, :int) do
      puts "Shutting down sweatd..."
      Sweatshop.stop
    end

    sig(:hup) do
      puts "Received HUP"
      Sweatshop.stop
      remove_pid!
      puts "Restarting sweatd with #{start_cmd}..."
      `#{start_cmd}`        
    end
    
    before do
      if rails_root
        puts "Loading Rails..."
        require rails_root + '/config/environment' 
      end
      require File.dirname(__FILE__) + '/../sweatshop'
    end

    daemonize(:kill_timeout => 20) do
      workers = []

      if groups.any?
        workers += Sweatshop.workers_in_group(groups)
      end

      if queues.any?
        workers += queues.collect{|q| Object.module_eval(q)}
      end

      if workers.any?
        worker_str = workers.join(',')
        puts "Starting #{worker_str}..." 
        $0 = "Sweatd: #{worker_str}"
        Sweatshop.do_tasks(workers)
      else
        puts "Starting all workers..." 
        $0 = 'Sweatd: all'
        Sweatshop.do_all_tasks
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sweatshop-1.5.1 lib/sweatshop/sweatd.rb
sweatshop-1.5.0 lib/sweatshop/sweatd.rb