Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit b60fdaaf7842197a584f246cf5587c8f5edf4424 Jeff Gran committed Mar 7, 2011
Showing with 175 additions and 0 deletions.
  1. +16 −0 README.md
  2. +46 −0 Rakefile
  3. +7 −0 lib/resque-timeout.rb
  4. +44 −0 lib/resque/plugins/timeout.rb
  5. +4 −0 spec/resque-timeout_spec.rb
  6. +48 −0 spec/resque/plugins/timeout_spec.rb
  7. +1 −0 spec/spec.opts
  8. +9 −0 spec/spec_helper.rb
16 README.md
@@ -0,0 +1,16 @@
+resque-timeout
+====
+
+Resque plugin to allow long-running jobs to timeout (and fail) automatically.
+
+To install:
+
+ $ gem install resque-timeout
+
+To set the timeout (in seconds, default is 600):
+
+ Resque::Plugins::Timeout.timeout = 60
+
+To turn it off (it is on by default):
+
+ Resque::Plugins::Timeout.switch = :off:
46 Rakefile
@@ -0,0 +1,46 @@
+require 'rubygems'
+require 'rake'
+
+begin
+ require 'jeweler'
+ Jeweler::Tasks.new do |gem|
+ gem.name = "resque-timeout"
+ gem.summary = "Timeout for long-running jobs in Resque"
+ gem.description = 'Resque plugin allowing long-running jobs to automatically fail after a specified time.'
+ gem.email = "jeff.gran@gmail.com"
+ gem.homepage = "http://github.com/jeffgran/resque-timeout"
+ gem.authors = ["Jeff Gran"]
+ gem.add_dependency "resque", "~>1.0"
+ gem.add_development_dependency "rspec", ">= 1.2.9"
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
+ end
+ Jeweler::GemcutterTasks.new
+rescue LoadError
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
+end
+
+require 'spec/rake/spectask'
+Spec::Rake::SpecTask.new(:spec) do |spec|
+ spec.libs << 'lib' << 'spec'
+ spec.spec_files = FileList['spec/**/*_spec.rb']
+end
+
+Spec::Rake::SpecTask.new(:rcov) do |spec|
+ spec.libs << 'lib' << 'spec'
+ spec.pattern = 'spec/**/*_spec.rb'
+ spec.rcov = true
+end
+
+task :spec => :check_dependencies
+
+task :default => :spec
+
+require 'rake/rdoctask'
+Rake::RDocTask.new do |rdoc|
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
+
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = "resque-fairly #{version}"
+ rdoc.rdoc_files.include('README*')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
7 lib/resque-timeout.rb
@@ -0,0 +1,7 @@
+require 'rubygems'
+gem 'resque', :version => '~> 1.0'
+require 'resque'
+
+require File.expand_path('resque/plugins/timeout', File.dirname(__FILE__))
+
+
44 lib/resque/plugins/timeout.rb
@@ -0,0 +1,44 @@
+require 'resque/worker'
+require 'timeout'
+
+module Resque::Plugins
+ module Timeout
+
+ def self.switch
+ @switch ||= :on
+ end
+
+ def self.switch=(val)
+ @switch = val
+ end
+
+ def self.timeout
+ @timeout ||= 600
+ end
+
+ def self.timeout=(val)
+ @timeout = val
+ end
+
+ def around_perform_with_timeout(*args)
+ ::Timeout.timeout(Resque::Plugins::Timeout.timeout) do
+ yield
+ end
+ end
+
+ end
+
+ # inject the Timeout mixin into the job class right before it
+ # executes (if it's not already there). This will give it the
+ # `around_perform_...` method above which wraps the execution
+ # in the ::Timeout.timeout block
+ Resque.before_fork do |job|
+ if Resque::Plugins::Timeout.switch == :on &&
+ !(job.payload_class.kind_of?(Resque::Plugins::Timeout))
+ job.payload_class.send(:extend, Resque::Plugins::Timeout)
+ end
+
+ end
+end
+
+
4 spec/resque-timeout_spec.rb
@@ -0,0 +1,4 @@
+require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
+
+describe "ResqueTimeout" do
+end
48 spec/resque/plugins/timeout_spec.rb
@@ -0,0 +1,48 @@
+require File.expand_path("../../spec_helper", File.dirname(__FILE__))
+
+class TestJob
+ def self.perform
+ 1.upto 5 do
+ putc '+'
+ sleep(1)
+ end
+ end
+end
+
+describe Resque::Plugins::Timeout do
+ it "kills jobs that run past the time limit" do
+ # set up the timeout
+ Resque::Plugins::Timeout.timeout = 2
+
+ # create a job
+ Resque::Job.create('queueZ', TestJob)
+
+ # create a worker and get the job
+ worker = Resque::Worker.new('queueZ')
+ job = Resque::Job.reserve('queueZ')
+
+ # mimic the forking and running of the job (with plugin hooks)
+ worker.run_hook :before_fork, job
+ worker.process(job)
+
+ worker.failed.should == 1
+ end
+
+ it 'should not timeout if the plugin is turned off' do
+ # turn it off
+ Resque::Plugins::Timeout.switch = :off
+
+ # create a job
+ Resque::Job.create('queueZ', TestJob)
+
+ # create a worker and get the job
+ worker = Resque::Worker.new('queueZ')
+ job = Resque::Job.reserve('queueZ')
+
+ # mimic the forking and running of the job (with plugin hooks)
+ worker.run_hook :before_fork, job
+ worker.process(job)
+
+ worker.failed.should == 1
+ end
+end
1 spec/spec.opts
@@ -0,0 +1 @@
+--diff c
9 spec/spec_helper.rb
@@ -0,0 +1,9 @@
+$LOAD_PATH.unshift(File.dirname(__FILE__))
+$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+require 'resque-timeout'
+require 'spec'
+require 'spec/autorun'
+
+Spec::Runner.configure do |config|
+
+end

0 comments on commit b60fdaa

Please sign in to comment.