Permalink
Browse files

- Pull web interface helper methods into there own module.

- Ensure other resque web interface code is loaded before our own.
- Replaced `Random.rand` with `rand` in demo code so it runs on 1.8.7.
  • Loading branch information...
1 parent 5e2dcec commit bd44d4f8b5fe570753d841b406c8a34091e46bdb @lantins committed Jun 4, 2012
Showing with 48 additions and 43 deletions.
  1. +3 −3 examples/demo/app.rb
  2. +45 −40 lib/resque-retry/server.rb
@@ -11,17 +11,17 @@ class ResqueRetryExampleApp < Sinatra::Base
end
post '/' do
- Resque.enqueue(SuccessfulJob, Random.rand(10000))
+ Resque.enqueue(SuccessfulJob, rand(10000))
redirect "/"
end
post '/failing' do
- Resque.enqueue(FailingJob, Random.rand(10000))
+ Resque.enqueue(FailingJob, rand(10000))
redirect "/"
end
post '/failing-with-retry' do
- Resque.enqueue(FailingWithRetryJob, Random.rand(10000))
+ Resque.enqueue(FailingWithRetryJob, rand(10000))
redirect "/"
end
end
@@ -1,5 +1,6 @@
require 'cgi'
require 'resque/server'
+require 'resque_scheduler/server'
# Extend Resque::Server to add tabs.
module ResqueRetry
@@ -10,46 +11,6 @@ module Server
# @api private
def self.included(base)
base.class_eval {
- helpers do
- # builds a retry key for the specified job.
- def retry_key_for_job(job)
- begin
- klass = Resque.constantize(job['class'])
- if klass.respond_to?(:redis_retry_key)
- klass.redis_retry_key(job['args'])
- else
- nil
- end
- rescue NameError
- nil
- end
- end
-
- # gets the number of retry attempts for a job.
- def retry_attempts_for_job(job)
- Resque.redis.get(retry_key_for_job(job))
- end
-
- # gets the failure details hash for a job.
- def retry_failure_details(retry_key)
- Resque.decode(Resque.redis.get("failure-#{retry_key}"))
- end
-
- # reads a 'local' template file.
- def local_template(path)
- # Is there a better way to specify alternate template locations with sinatra?
- File.read(File.join(File.dirname(__FILE__), "server/views/#{path}"))
- end
-
- # cancels job retry
- def cancel_retry(job)
- klass = Resque.constantize(job['class'])
- retry_key = retry_key_for_job(job)
- Resque.remove_delayed(klass, *job['args'])
- Resque.redis.del("failure-#{retry_key}")
- Resque.redis.del(retry_key)
- end
- end
get '/retry' do
erb local_template('retry.erb')
@@ -74,10 +35,54 @@ def cancel_retry(job)
}
end
+ # Helper methods used by retry tab.
+ module Helpers
+ # builds a retry key for the specified job.
+ def retry_key_for_job(job)
+ begin
+ klass = Resque.constantize(job['class'])
+ if klass.respond_to?(:redis_retry_key)
+ klass.redis_retry_key(job['args'])
+ else
+ nil
+ end
+ rescue NameError
+ nil
+ end
+ end
+
+ # gets the number of retry attempts for a job.
+ def retry_attempts_for_job(job)
+ Resque.redis.get(retry_key_for_job(job))
+ end
+
+ # gets the failure details hash for a job.
+ def retry_failure_details(retry_key)
+ Resque.decode(Resque.redis.get("failure-#{retry_key}"))
+ end
+
+ # reads a 'local' template file.
+ def local_template(path)
+ # Is there a better way to specify alternate template locations with sinatra?
+ File.read(File.join(File.dirname(__FILE__), "server/views/#{path}"))
+ end
+
+ # cancels job retry
+ def cancel_retry(job)
+ klass = Resque.constantize(job['class'])
+ retry_key = retry_key_for_job(job)
+ Resque.remove_delayed(klass, *job['args'])
+ Resque.redis.del("failure-#{retry_key}")
+ Resque.redis.del(retry_key)
+ end
+ end
+
+
end
end
Resque::Server.tabs << 'Retry'
Resque::Server.class_eval do
include ResqueRetry::Server
+ helpers ResqueRetry::Server::Helpers
end

0 comments on commit bd44d4f

Please sign in to comment.