Module: ServiceManager
Defined Under Namespace
Classes: Service
Constant Summary collapse
- SERVICES_PATH =
"./config/services.rb"
Instance Method Summary collapse
- #define_service(name = nil, &block) ⇒ Object
- #load_services(path = nil) ⇒ Object
- #service_files_loaded ⇒ Object
- #services ⇒ Object
- #services_hash ⇒ Object
-
#start ⇒ Object
Starts all configured services.
-
#stop ⇒ Object
Stop all services.
Instance Method Details
#define_service(name = nil, &block) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/service_manager.rb', line 29 def define_service(name = nil, &block) name ||= File.basename(caller.first.gsub(/.rb:.+$/, "")) ServiceManager::Service.new(:name => name).tap do |service| yield service services << service end end |
#load_services(path = nil) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/service_manager.rb', line 22 def load_services(path = nil) path = File.(path || SERVICES_PATH, Dir.pwd) return if service_files_loaded.include?(path) service_files_loaded << path load path end |
#service_files_loaded ⇒ Object
18 19 20 |
# File 'lib/service_manager.rb', line 18 def service_files_loaded @service_files_loaded ||= [] end |
#services ⇒ Object
10 11 12 13 14 15 16 |
# File 'lib/service_manager.rb', line 10 def services return @services if @services (@services = []).tap do load_services if service_files_loaded.empty? end end |
#services_hash ⇒ Object
37 38 39 |
# File 'lib/service_manager.rb', line 37 def services_hash Hash[services.map { |s| [s.name.to_sym, s]}] end |
#start ⇒ Object
Starts all configured services. If service is detected as running already, don’t try and start it.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/service_manager.rb', line 49 def start raise RuntimeError, "No services defined" if services.empty? threads = services.map do |s| Thread.new do begin s.start rescue ServiceManager::Service::ServiceDidntStart puts "Quitting due to failure." exit(1) rescue Exception => e puts e puts e.backtrace exit(1) end end end threads.map(&:join) end |
#stop ⇒ Object
Stop all services. If service wasn’t started by this service manager session, don’t try and stop it.
42 43 44 45 46 |
# File 'lib/service_manager.rb', line 42 def stop return unless services.any? { |s| s.process } puts "Stopping the services..." services.map {|s| Thread.new { s.stop } }.map(&:join) end |