Sha256: 2bbf2d5b18f0fb1dbff77047c1f98a4502ff6908008f86c701f1ca604f3973b5

Contents?: true

Size: 1.81 KB

Versions: 5

Compression:

Stored size: 1.81 KB

Contents

#!/usr/bin/env ruby
require 'rubygems'
require 'daemons'

env, cmd = 'development', nil
ARGV.each do |arg|
  if arg =~ /RAILS_ENV\s*=\s*(\w+)/
    env = $1
  else
    cmd = arg
  end
end

unless %w{index start stop restart}.include?(cmd)
  puts "Usage: script/worker RAILS_ENV=production [start|stop|restart]"
  exit -1
end

dir = File.expand_path('.')

daemon_options = {
  :multiple   => false,
  :dir_mode   => :normal,
  :dir        => File.join(dir, 'log'),
  :backtrace  => true,
  # Create a worker.output to redirect STDOUT and STDERR
  :log_output => true,
}

ARGV.clear
ARGV << cmd

Daemons.run_proc('worker', daemon_options) do
  ARGV.clear

  Dir.chdir dir
  ENV['RAILS_ENV'] = RAILS_ENV = env
  require File.join('config', 'environment')

  require 'delayed/worker'
  class Delayed::Job
    private
      # FIX Delayed Job to work with safe_yaml
      # TODO: Remove this when we upgrade to Ruby 1.9
      def deserialize(source)
        handler = YAML.load(source, :safe => false) rescue nil

        unless handler.respond_to?(:perform)
          if handler.nil? && source =~ ParseObjectFromYaml
            handler_class = $1
          end
          attempt_to_load(handler_class || handler.class)
          handler = YAML.load(source, :safe => false)
        end

        return handler if handler.respond_to?(:perform)

        raise DeserializationError,
          'Job failed to load: Unknown handler. Try to manually require the appropriate file.'
      rescue TypeError, LoadError, NameError => e
        raise DeserializationError,
          "Job failed to load: #{e.message}. Try to manually require the required file."
      end
  end
  
  begin
    require 'thinking_sphinx'
    require 'thinking_sphinx/deltas/delayed_delta'
  rescue LoadError
    # Ignore
  end

  puts "Starting worker..."

  Delayed::Worker.new.start
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
zena-1.2.8 bricks/worker/zena/worker
zena-1.2.7 bricks/worker/zena/worker
zena-1.2.6 bricks/worker/zena/worker
zena-1.2.5 bricks/worker/zena/worker
zena-1.2.4 bricks/worker/zena/worker