Created
August 10, 2011 18:01
-
-
Save taf2/1137645 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# daemonized resque scheduler runner | |
require 'yaml' | |
env = ENV['RAILS_ENV'] || 'development' | |
scheduler_config = YAML.load_file(File.expand_path('../../config/resque_schedule.yml', __FILE__))[env] | |
puts scheduler_config.inspect | |
paths = [] | |
paths << stdout_path = scheduler_config['stdout_path'] | |
paths << stderr_path = scheduler_config['stderr_path'] | |
paths << pidfile_path = scheduler_config['pidfile_path'] | |
paths << rundir_path = scheduler_config['rundir_path'] | |
paths.each {|p| | |
if !File.exist?(File.dirname(p)) | |
raise "error path not found: #{File.dirname(p)}" | |
end | |
} | |
if File.exist?(pidfile_path) | |
pid = File.read(pidfile_path).to_i | |
status = system("kill -0 #{pid}") | |
if status #Process.kill(pid, 0) | |
STDERR.puts "process is currently running #{pid} with #{pidfile_path}" | |
exit(1) | |
else | |
STDERR.puts "overwriting stale pidfile: #{pidfile_path}" | |
end | |
end | |
# Set up gems listed in the Gemfile. | |
require 'rubygems' | |
gemfile = File.expand_path('../../Gemfile', __FILE__) | |
begin | |
ENV['BUNDLE_GEMFILE'] = gemfile | |
require 'bundler' | |
Bundler.setup | |
rescue Bundler::GemNotFound => e | |
STDERR.puts e.message | |
STDERR.puts "Try running `bundle install`." | |
exit! | |
end if File.exist?(gemfile) | |
require File.expand_path('../../config/environment', __FILE__) | |
require 'resque' | |
require 'resque_scheduler' | |
fork do | |
Process.setsid | |
fork do | |
begin | |
Process.setsid | |
File.umask 0000 | |
Dir.chdir(rundir_path) | |
STDIN.reopen "/dev/null" # turn stdin off | |
STDOUT.reopen stdout_path, 'a' # send this to stdout | |
STDERR.reopen stderr_path, 'a' # send this to stderr | |
File.open(pidfile_path, 'wb') { |f| f << Process.pid.to_s } | |
STDERR.puts "alive with config: #{scheduler_config.inspect}" | |
Resque::Scheduler.dynamic = scheduler_config['dynamic'] | |
Resque::Scheduler.verbose = scheduler_config['verbose'] | |
Resque::Scheduler.run | |
ensure | |
File.unlink(pidfile_path) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment