Class: QuickUtils::RailsDaemon

Inherits:
Object
  • Object
show all
Defined in:
lib/quick_utils/rails_daemon.rb

Direct Known Subclasses

RakeDaemon, Watcher

Constant Summary collapse

@@proc_name =
''
@@log_file =
''
@@rails_root =
Dir.pwd

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ RailsDaemon

Returns a new instance of RailsDaemon.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/quick_utils/rails_daemon.rb', line 22

def initialize(args)
	@options = {:worker_count => 1, :environment => :development, :delay => 5, :stage => :development}
	
	optparse = 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 apn_sender under ([development]/production).') do |e|
			@options[:environment] = e
		end
		opts.on('-n', '--number-of-workers=WORKERS', "Number of unique workers to spawn") do |worker_count|
			@options[:worker_count] = worker_count.to_i rescue 1
		end
		opts.on('-v', '--verbose', "Turn on verbose mode") do
			@options[:verbose] = true
		end
		opts.on('-d', '--delay=D', "Delay between rounds of work (seconds)") do |d|
			@options[:delay] = d
		end
		opts.on('-s', '--stage=NAME', "Stage ([development]/staging/production)") do |s|
			@options[:stage] = s
		end
	end
	
	# If no arguments, give help screen
	@args = optparse.parse!(args.empty? ? ['-h'] : args)
end

Class Method Details

.set_process_name(pname) ⇒ Object



13
14
15
16
# File 'lib/quick_utils/rails_daemon.rb', line 13

def self.set_process_name(pname)
	@@proc_name = pname
	@@log_file = File.join(@@rails_root, 'log', "#{@@proc_name}.log")
end

.set_rails_root(dir) ⇒ Object



18
19
20
# File 'lib/quick_utils/rails_daemon.rb', line 18

def self.set_rails_root(dir)
	@@rails_root = dir
end

Instance Method Details

#daemonizeObject



53
54
55
56
57
58
59
60
# File 'lib/quick_utils/rails_daemon.rb', line 53

def daemonize
	@options[:worker_count].times do |worker_index|
		process_name = @options[:worker_count] == 1 ? @@proc_name : "#{@@proc_name}.#{worker_index}"
		Daemons.run_proc(process_name, :dir => "#{@@rails_root}/tmp/pids", :dir_mode => :normal, :ARGV => @args) do |*args|
			run process_name
		end
	end
end

#run(worker_name = nil) ⇒ Object



62
63
64
# File 'lib/quick_utils/rails_daemon.rb', line 62

def run(worker_name = nil)

end