Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 71 lines (58 sloc) 2.31 KB
#!/usr/bin/env ruby
# async-observer - Rails plugin for asynchronous job execution
# Copyright (C) 2007 Philotic Inc.
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Use the same pointer (and therefore same buffer) for stdout and stderr.
$VERBOSE = nil; STDERR = $stderr = STDOUT = $stdout; $VERBOSE = false
require 'time'
def load_rails_and_run
# Rails initialization.
# We do this here instead of using script/runner because script/runner
# breaks __FILE__, which we use below.
begin
puts "#!load-rails!begin!#{Time.now.utc.xmlschema(6)}"
require File.expand_path(File.dirname(__FILE__) + '/../../../../config/boot')
require File.expand_path(File.dirname(__FILE__) + '/../../../../config/environment')
puts "RAILS_ROOT=#{Rails.root}"
ensure
puts "#!load-rails!end!#{Time.now.utc.xmlschema(6)}"
end
require 'async_observer/worker'
AsyncObserver::Worker.new(binding).run()
end
# set environment
if ARGV.include?('-e') # check rails env
env=nil
ARGV.each_with_index{|arg,i| env = ARGV[i+1] if arg == '-e' }
RAILS_ENV=env unless env.nil?
end
if ARGV.include?('-d')
pidpath = 'log/worker.pid'
if ARGV.include?('--pid') # look up the pid path
ARGV.each_with_index{|arg,i| pidpath = ARGV[i+1] if arg == '--pid' }
STDERR.puts "Missing pid file path!" and exit(1) if pidpath.nil?
end
unless File.writable?(File.dirname(pidpath))
STDERR.puts "#{pidpath} not writable!"
exit(1)
end
if File.exist?(pidpath)
STDERR.puts "#{pidpath} exits! Make sure the worker isn't still running and try again after rm #{pidpath}"
exit(1)
end
require File.dirname(__FILE__) + '/../lib/async_observer/daemonize'
AsyncObserver::Daemonize.detach(pidpath) do
load_rails_and_run
end
else
load_rails_and_run
end