Class: QuickUtils::Watcher

Inherits:
RailsDaemon show all
Defined in:
lib/quick_utils/watcher.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from RailsDaemon

#daemonize, #initialize, set_process_name, set_rails_root

Constructor Details

This class inherits a constructor from QuickUtils::RailsDaemon

Class Method Details

.set_app_name(app_name) ⇒ Object



3
4
5
# File 'lib/quick_utils/watcher.rb', line 3

def self.set_app_name(app_name)
	@@main_proc = app_name
end

.set_proc_list(plist) ⇒ Object



7
8
9
# File 'lib/quick_utils/watcher.rb', line 7

def self.set_proc_list(plist)
	@@processes = plist
end

Instance Method Details

#run(worker_name = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/quick_utils/watcher.rb', line 11

def run(worker_name = nil)

	logger = Logger.new(@@log_file)

	while true
		# get num thins running
		thin_num = `ps -ef | grep "thin" | grep "#{@@main_proc}-#{@options[:stage].to_s}" | grep -v "grep" | wc | awk '{print $1}'`.to_i
		needs_restart = false
		@@processes.each do |proc|
			pnum = `ps -ef | grep "#{proc}" | grep -v "grep" | wc | awk '{print $1}'`.to_i
			needs_restart ||= (pnum == 0)
		end
		if thin_num == 0 || needs_restart
			logger.info("Restarting #{main_proc} now...");
			system "cd #{@@rails_root}; bundle exec script/restart #{@options[:stage].to_s};"
		end
		sleep @options[:delay]
	end
		
end