Class: Clockworkd::Command
- Inherits:
-
Object
- Object
- Clockworkd::Command
- Defined in:
- lib/clockworkd/command.rb
Instance Method Summary collapse
- #daemonize ⇒ Object
-
#initialize(args) ⇒ Command
constructor
A new instance of Command.
- #run(worker_name = nil) ⇒ Object
- #run_process(process_name, dir) ⇒ Object
Constructor Details
#initialize(args) ⇒ Command
Returns a new instance of Command.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/clockworkd/command.rb', line 9 def initialize(args) @files_to_reopen = [] @options = { :quiet => true, :identifier => 0, :pid_dir => "#{Rails.root}/tmp/pids", :clock_file => "#{Rails.root}/config/clockworkd.yml" } @monitor = false opts = OptionParser.new do |opts| opts. = "Usage: #{File.basename($0)} [options] start|stop|restart|run" opts.on('-h', '--help', 'Show this message') do puts opts exit 1 end opts.on('--clock-file=FILE', 'Specifies an alternate clockwork file with schuduling.') do |file| @options[:clock_file] = file end opts.on('--pid-dir=DIR', 'Specifies an alternate directory in which to store the process ids.') do |dir| @options[:pid_dir] = dir end opts.on('-i', '--identifier=n', 'A numeric identifier for the clockwork process.') do |n| @options[:identifier] = n end opts.on('-m', '--monitor', 'Start monitor process.') do @monitor = true end opts.on('--sleep-delay N', "Amount of time to sleep when no events are found") do |n| @options[:sleep_delay] = n end end @args = opts.parse!(args) end |
Instance Method Details
#daemonize ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/clockworkd/command.rb', line 46 def daemonize Clockworkd::Worker.before_fork ObjectSpace.each_object(File) do |file| @files_to_reopen << file unless file.closed? end dir = @options[:pid_dir] Dir.mkdir(dir) unless File.exists?(dir) process_name = "clockworkd.#{@options[:identifier]}" run_process(process_name, dir) end |
#run(worker_name = nil) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/clockworkd/command.rb', line 66 def run(worker_name = nil) Dir.chdir(Rails.root) # Re-open file handles @files_to_reopen.each do |file| begin file.reopen file.path, "a+" file.sync = true rescue ::Exception => e raise e end end Clockworkd::Worker.logger = Logger.new(File.join(Rails.root, 'log', 'clockworkd.log')) Clockworkd::Worker.after_fork Clockworkd::Worker.new(@options).run rescue => e Rails.logger.fatal e STDERR.puts e. exit 1 end |
#run_process(process_name, dir) ⇒ Object
60 61 62 63 64 |
# File 'lib/clockworkd/command.rb', line 60 def run_process(process_name, dir) Daemons.run_proc(process_name, :multiple => false, :dir => dir, :dir_mode => :normal, :monitor => @monitor, :ARGV => @args) do |*args| run process_name end end |