Permalink
Browse files

Added script/delayed_job which manages a deamon for working off jobs

Run `script/generate delayed_job` to generate the script
  • Loading branch information...
1 parent 266fc15 commit d6d0a928899bd15df8d2e6e951cc0c7b41fae824 @bkeepers bkeepers committed Sep 13, 2008
View
@@ -76,6 +76,11 @@ which are stored as their text representation and loaded from the database fresh
h2. Running the jobs
+Run @script/generate delayed_job@ to add @script/delayed_job@. This script can then be used to manage a process which will start working off jobs.
+
+ $ ruby script/delayed_job -e production start
+ $ ruby script/delayed_job -e production stop
+
You can invoke @rake jobs:work@ which will start working off jobs. You can cancel the rake task with @CTRL-C@.
You can also run by writing a simple @script/job_runner@, and invoking it externally:
@@ -0,0 +1,9 @@
+class DelayedJobGenerator < Rails::Generator::Base
+
+ def manifest
+ record do |m|
+ m.template 'script', 'script/delayed_job', :chmod => 0755
+ end
+ end
+
+end
@@ -0,0 +1,7 @@
+#!/usr/bin/env ruby
+
+# Daemons sets pwd to /, so we have to explicitly set RAILS_ROOT
+RAILS_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
+
+require File.join(File.dirname(__FILE__), *%w(.. vendor plugins delayed_job lib delayed command))
+Delayed::Command.new(ARGV).run
View
@@ -0,0 +1,67 @@
+require 'rubygems'
+require 'daemons'
+require 'optparse'
+require 'ostruct'
+
+module Delayed
+ class Command
+ def initialize(args)
+ @options = OpenStruct.new(:sleep => 5)
+
+ opts = OptionParser.new do |opts|
+ opts.banner = "Usage: #{File.basename($0)} [options] start|stop|restart|run"
+
+ opts.on('-h', '--help', 'Show this message') do
+ puts opts
+ exit 1
+ end
+ opts.on('-e', '--environment=NAME', 'Specifies the environment to run this delayed jobs under (test/development/production).') do |e|
+ ENV['RAILS_ENV'] = e
+ end
+ opts.on('-s', '--sleep=seconds', "Number of seconds between checking for new jobs") do |secs|
+ @options.sleep = secs
+ end
+ end
+ @args = opts.parse!(args)
+ end
+
+ def run
+ Daemons.run_proc('delayed_job', :dir => "#{RAILS_ROOT}/tmp/pids", :dir_mode => :normal, :ARGV => @args) do |*args|
+ begin
+ require File.join(RAILS_ROOT, 'config', 'environment')
+
+ # Replace the default logger
+ logger = Logger.new(File.join(RAILS_ROOT, 'log', 'delayed_job.log'))
+ logger.level = ActiveRecord::Base.logger.level
+ ActiveRecord::Base.logger = logger
+
+ logger.info "*** Starting job worker #{Delayed::Job.worker_name}"
+
+ trap('TERM') { puts 'Exiting...'; $exit = true }
+ trap('INT') { puts 'Exiting...'; $exit = true }
+
+ loop do
+ result = nil
+ realtime = Benchmark.realtime { result = Delayed::Job.work_off }
+ count = result.sum
+
+ break if $exit
+
+ if count.zero?
+ sleep @options.sleep
+ logger.debug 'Waiting for more jobs...'
+ else
+ logger.info "#{count} jobs processed at %.4f j/s, %d failed ..." % [count / realtime, result.last]
+ end
+
+ break if $exit
+ end
+ rescue => e
+ logger.fatal e
+ STDERR.puts e.message
+ exit 1
+ end
+ end
+ end
+ end
+end

0 comments on commit d6d0a92

Please sign in to comment.