Skip to content

Instantly share code, notes, and snippets.

@Ptico
Created March 18, 2011 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Ptico/876530 to your computer and use it in GitHub Desktop.
Save Ptico/876530 to your computer and use it in GitHub Desktop.
require 'resque/tasks'
task "resque:setup" => :environment
namespace :queue do
task :start => :environment do
pidfile = Rails.root.join('tmp', 'pids', 'resque.pid')
logfile = Rails.root.join('log', 'resque.log')
errlogfile = Rails.root.join('log', 'resque_errors.log')
pid = fork do
$stdout = File.new(logfile, 'w')
$stdin = File.new(errlogfile, 'w')
worker = Resque::Worker.new('*')
worker.work(ENV['INTERVAL'] || 5)
end
if pid
File.open(pidfile, 'w') do |f|
f.write(pid)
f.close
end
puts "Resque successfully started with pid: #{pid}"
end
end
task :stop => :environment do
require "timeout"
pidfile = Rails.root.join('tmp', 'pids', 'resque.pid')
if File.exists?(pidfile)
pid = File.read(pidfile).to_i
Process.kill(15, pid)
begin
Timeout::timeout(15) do
Process.waitpid(pid)
end
rescue Timeout::Error
Process.kill(9, pid)
rescue => e
puts e.to_s
end
else
puts "error: Pidfile does not exists"
end
File.delete(pidfile) if File.exists?(pidfile)
end
task :restart => :environment do
Rake::Task["queue:stop"].invoke
Rake::Task["queue:start"].invoke
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment