require 'active_record' require 'yaml' require 'delayed_job_monitor/server' require 'delayed_job_monitor/application/app' require 'delayed_job_monitor/job' require 'delayed_job_monitor/version' module DelayedJobMonitor def self.[](key) unless @config @config = YAML.load_file("config.yml").symbolize_keys end @config[key.to_sym] end def self.[]=(key, value) @config[key.to_sym] = value end def self.establish_connection begin if DelayedJobMonitor[:database]["host"].nil? p "No database connection defined in config.yml" else if defined? connection p "There is already a DB connection locally...closing" DelayedJobMonitor.connection.disconnect! DelayedJobMonitor.connection.connection.close ActiveRecord::Base.clear_active_connections! ActiveRecord::Base.clear_reloadable_connections! end db = DelayedJobMonitor[:database] connection = ActiveRecord::Base.establish_connection(:adapter => db["adapter"],:database => db["database"],:host=>db["host"],:port=>db["port"],:username=>db["username"],:password=>db["password"],:encoding => "utf8",:template=>"template0") return connection end rescue raise true end end def self.delayed_job_running_locally? local_delayed_jobs = %x{ps aux}.split(/\n/).map{|x| x.split(/\s+/) if x.split(/\s+/).last.match("job")}.compact if local_delayed_jobs.empty? return false else return true end end def self.connected? if defined? DelayedJobMonitor.connection return true else return false end end def self.stand_alone_mode? !ENV["RAILS_ENV"].present? end def self.initialize_servers unless DelayedJobMonitor[:hosts].first["host"].nil? DelayedJobMonitor[:hosts].each do |host| DelayedJobMonitor::Server.new(host["name"],host["host"],host["rails_path"],host["username"],host["password"]) end if delayed_job_running_locally? DelayedJobMonitor::Server.new end return DelayedJobMonitor::Server.servers else DelayedJobMonitor::Server.new p "No hosts configured. Connecting to localhost" end end def self.start! if stand_alone_mode? && !connected? DelayedJobMonitor.establish_connection end DelayedJobMonitor.initialize_servers DelayedJobMonitor::App.run! end end #I know this is terrible, but I want to use the Delayed Job methods without rewriting them from scratch if DelayedJobMonitor.stand_alone_mode? class Rails def self.root return Pathname.new(Dir.pwd) end def self.logger return nil end end end