Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ticket_13675 resque setup
# Resque tasks
require 'resque/tasks'
require 'resque_scheduler/tasks'
namespace :resque do
# slight modification of rake resque:work
# https://github.com/defunkt/resque/blob/master/lib/resque/tasks.rb
task :work_dont_fork => ["resque:preload", "resque:setup"] do
require 'resque'
queues = (ENV['QUEUES'] || ENV['QUEUE']).to_s.split(',')
begin
worker = Resque::Worker.new(*queues)
worker.cant_fork = true
worker.verbose = ENV['LOGGING'] || ENV['VERBOSE']
worker.very_verbose = ENV['VVERBOSE']
rescue Resque::NoQueueError
abort "set QUEUE var, e.g.$ QUEUE=critical,high rake resque:work"
end
if ENV['BACKGROUND']
unless Process.respond_to?('daemon')
abort "env var BACKGROUND is set, which requires ruby >= 1.9"
end
Process.daemon(true)
end
if ENV['PIDFILE']
f = File.open(ENV['PIDFILE'], 'w')
f.write worker.pid
f.close
end
worker.log "Starting worker #{worker}"
worker.work(ENV['INTERVAL'] || 5) # interval, will block
end
end
rails_root = ENV['RAILS_ROOT'] || File.dirname(__FILE__) + '/../..'
rails_env = ENV['RAILS_ENV'] || 'development'
resque_config = YAML.load_file(rails_root + '/config/resque.yml')
Resque.redis = resque_config[rails_env]
Resque.redis.namespace = "resque:cp_v2_#{Rails.env}" # separate redis namespace for dev/prod/test environments
module Resque::Plugins::ExponentialBackoff
end
module Resque::Plugins::History
end
Resque.inline = ENV['RAILS_ENV'] == "test"
# cleaner tab in the server
require 'resque-cleaner'
require 'resque/failure/multiple'
require 'resque-retry'
require 'resque-retry/server'
require 'resque/failure/redis'
require 'resque/scheduler'
require 'resque_scheduler'
require 'resque_scheduler/server'
Resque.schedule = YAML.load_file("#{Rails.root}/config/resque_schedule.yml")
# Find a better place for ResqueDebugData
# This should not belong in the resque namespace, but it seemed to be a good idea at the time
module Resque
include ResqueDebugData
end
module Resque
module Failure
class StatsdLogger < Base
def save
STATSD.increment('jobs.failures')
end
end
class FailureLogger < Base
def save
logger = Logger.new("#{Rails.root}/log/resque_failures_#{Rails.env}.log")
logger.error('#######################################################################')
logger.error("#{Time.now.to_s(:db)} - Worker #{worker.inspect}")
logger.error payload.inspect
logger.error exception.inspect
logger.error filter_backtrace(exception.backtrace).map { |l| " " + l }.join("\n")
logger.error ""
logger.error ""
end
def filter_backtrace(backtrace)
index = backtrace.index { |item| item.include?('/lib/resque/job.rb') }
backtrace.first(index.to_i)
end
end
end
end
Resque::Failure::Multiple.classes = [Resque::Failure::Redis, Resque::Failure::StatsdLogger, Resque::Failure::FailureLogger]
Resque::Failure.backend = Resque::Failure::Multiple
# Resque::Failure::MultipleWithRetrySuppression.classes = [Resque::Failure::StatsdLogger, Resque::Failure::FailureLogger, Resque::Failure::Redis]
# Resque::Failure.backend = Resque::Failure::MultipleWithRetrySuppression
# Resque::Failure::Multiple.configure do |multi|
# # Always stores failure in Redis and writes to log
# multi.classes = Resque::Failure::Redis, Resque::Failure::Logger
# # Production/staging only: also email us a notification
# multi.classes << Resque::Failure::Notifier
# # if Rails.env.production? || Rails.env.staging?
# end
require_relative 'settings'
# require 'YAML'
env = ENV["RAILS_ENV"]
Settings.load!("./config/basic_auth.yml", env: env)
resque_config = YAML.load_file('config/resque.yml')
Resque.redis = resque_config[env]
Resque.redis.namespace = "resque:cp_v2_#{env}" # separate redis namespace for dev/prod/test environments
require 'resque-history/server'
require 'resque-cleaner'
require 'resque-retry/server'
require 'resque_scheduler/server'
# Set the AUTH env variable to your basic auth password to protect Resque
AUTH_PASSWORD = Settings.resque_server_password
if AUTH_PASSWORD
Resque::Server.use Rack::Auth::Basic, "Restricted Area" do |username, password|
password == AUTH_PASSWORD
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.