Sha256: fe46cc5f84aaee36cc26c842c433582c0013ce21d6d81ab876e7f7d80e487a2c

Contents?: true

Size: 1.76 KB

Versions: 2

Compression:

Stored size: 1.76 KB

Contents

require 'net/ssh'
require 'hashie'

module DelayedJobMonitor
	class Server
		@@instance_collector = []
		def initialize(name="default",host="localhost",rails_path=nil,username=nil,password=nil)
			unless host == "localhost"
				p host
				@connection = Net::SSH.start(host,username,:password=>password)
				@username = username
			else
				@connection = nil
			end
			@rails_path = rails_path
			@name = name
			@host = host
			@@instance_collector << self
		end

		def self.servers
			return @@instance_collector
		end

		def connection
			@connection
		end

		def close_connection
			connection.close
		end

		def host
			@host
		end

		def name
			@name
		end

		def rails_path
			@rails_path
		end

		# def tail_log
		# 	log_file = File.open( "tmp/logs/delayed_job_#{host.gsub(/\W/,"_")}.log","w")
		#   log_file << connection.exec!("tail -f /var/www/contently/log/delayed_job.log")
		#   log_file.close
		# end

		def delayed_job_workers
			@worker_list = []
			if self.connection
				workers = connection.exec!("ps aux").split(/\n/).map{|x| x.split(/\s+/) if x.split(/\s+/).last.match("job")}.compact
			else
				workers = %x{ps aux}.split(/\n/).map{|x| x.split(/\s+/) if x.split(/\s+/).last.match("job")}.compact
			end
			if workers.any?
				workers.each do |wkr|
					@worker_list << Hashie::Mash.new({:host=>host,:name=>wkr.last,:pid=>wkr[1],:cpu=>wkr[2],:mem=>wkr[3],:started=>wkr[8],:run_time=>wkr[9]})
				end
				return @worker_list
			else
				return nil
			end
		end

		def stop_workers(env=production)
			p "Attempting to stop delayed jobs workers..."
			command = connection.exec!("#{rails_path}/script/delayed_job stop")
			return command #get the status code as a string
		end

		def start_workers(env=production)
			p "Attempting to stop delayed jobs workers..."
		end
	end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
delayed_job_monitor-0.0.5 lib/delayed_job_monitor/server.rb
delayed_job_monitor-0.0.4 lib/delayed_job_monitor/server.rb