Skip to content

Instantly share code, notes, and snippets.

@activefx
Created January 22, 2013 03:36
Show Gist options
  • Save activefx/4591852 to your computer and use it in GitHub Desktop.
Save activefx/4591852 to your computer and use it in GitHub Desktop.
resque.rake with error monitoring, resque scheduler and queue clearer
require 'resque/tasks'
require 'resque_scheduler/tasks'
require 'resque/failure/multiple'
require 'resque/failure/redis'
# https://github.com/lantins/resque-exceptional
# Configuration Options
# Required
# api_key - your getexceptional.com api key.
# HTTP Proxy Options (optional)
# proxy_host - proxy server ip / hostname.
# proxy_port - proxy server port.
# proxy_user - proxy server username.
# proxy_pass - proxy server password.
# HTTP Client Options (optional)
# use_ssl - set true if your plan supports ssl. (default: false)
# http_open_timeout - timeout in seconds to establish the connection. (default: 2)
# http_read_timeout - timeout in seconds to wait for a reply. (default: 5)
Resque::Failure::Exceptional.configure do |config|
config.api_key = configatron.exceptional.api_key
end
Resque::Failure::Multiple.classes = [Resque::Failure::Redis, Resque::Failure::Exceptional]
Resque::Failure.backend = Resque::Failure::Multiple
task "resque:setup" => :environment
namespace :resque do
# https://github.com/bvandenbos/resque-scheduler
task :setup => :environment do
require 'resque'
require 'resque_scheduler'
require 'resque/scheduler'
# you probably already have this somewhere
# Resque.redis = configatron.redis.url
# If you want to be able to dynamically change the schedule,
# uncomment this line. A dynamic schedule can be updated via the
# Resque::Scheduler.set_schedule (and remove_schedule) methods.
# When dynamic is set to true, the scheduler process looks for
# schedule changes and applies them on the fly.
# Note: This feature is only available in >=2.0.0.
Resque::Scheduler.dynamic = true
# The schedule doesn't need to be stored in a YAML, it just needs to
# be a hash. YAML is usually the easiest.
# Resque.schedule = YAML.load_file('your_resque_schedule.yml')
# If your schedule already has +queue+ set for each job, you don't
# need to require your jobs. This can be an advantage since it's
# less code that resque-scheduler needs to know about. But in a small
# project, it's usually easier to just include you job classes here.
# So, something like this:
# require 'jobs'
end
# Clear resque queues and stats
#
# See also:
# https://gist.github.com/1228863
# http://stackoverflow.com/questions/5880962/how-to-destroy-jobs-enqueued-by-resque-workers - old version
# https://github.com/defunkt/resque/issues/49
# http://redis.io/commands - new commands
#
desc "Clear pending tasks"
task :clear => :environment do
queues = Resque.queues
queues.each do |queue_name|
puts "Clearing #{queue_name}..."
Resque.redis.del "queue:#{queue_name}"
end
puts "Clearing delayed..." # in case of scheduler - doesn't break if no scheduler module is installed
Resque.redis.keys("delayed:*").each do |key|
Resque.redis.del "#{key}"
end
Resque.redis.del "delayed_queue_schedule"
puts "Clearing stats..."
Resque.redis.set "stat:failed", 0
Resque.redis.set "stat:processed", 0
end
end
# Use this rake task to overwrite the default resque:work task
# as foreman uses jobs:work to start resque workers
#
task "jobs:work" => "resque:work"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment