Skip to content

Instantly share code, notes, and snippets.

@mikhailov
Created December 22, 2009 19:42
Show Gist options
  • Save mikhailov/261978 to your computer and use it in GitHub Desktop.
Save mikhailov/261978 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'rubygems'
require 'daemons'
require 'optparse'
require 'ostruct'
# Daemons sets pwd to /, so we have to explicitly set RAILS_ROOT
RAILS_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
module Delayed
class Command
def initialize(args)
@options = OpenStruct.new(:sleep => 5)
opts = OptionParser.new do |opts|
opts.banner = "Usage: #{File.basename($0)} [options] start|stop|restart|run"
opts.on('-h', '--help', 'Show this message') do
puts opts
exit 1
end
opts.on('-e', '--environment=NAME', 'Specifies the environment to run this delayed jobs under (test/development/production).') do |e|
ENV['RAILS_ENV'] = e
end
end
@args = opts.parse!(args)
end
def run
Daemons.run_proc('delayed_job', :dir => "#{RAILS_ROOT}/tmp/pids", :dir_mode => :normal, :ARGV => @args) do |*args|
require "#{RAILS_ROOT}/config/environment"
Delayed::Worker.new.start
end
end
end
end
puts "\033[0;34mbefore"
if File.exist? "#{RAILS_ROOT}/tmp/pids/delayed_job.pid"
print "\033[0;31m"
system "sleep 2 && ps -p $(cat #{RAILS_ROOT}/tmp/pids/delayed_job.pid) --no-headers"
else
puts "\033[0;31mnothing"
end
Delayed::Command.new(ARGV).run
puts "\033[0;34mafter"
if File.exist? "#{RAILS_ROOT}/tmp/pids/delayed_job.pid"
print "\033[0;32m"
system "sleep 2 && ps -p $(cat #{RAILS_ROOT}/tmp/pids/delayed_job.pid) --no-headers"
else
puts "\033[0;31mnothing"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment