Sha256: 5a6009b52eef0d7cb82a5779c1a9076457d06185983b2a08cfd46eb3bde5ab1e

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

require 'tcpsocket-wait'
require 'background_process'
require 'net/http'

module ServiceManager
  SERVICES_PATH = "./config/services.rb"

  extend self

  def services
    return @services if @services
    @services = []
    load_services
    @services
  end

  def load_services(path = nil)
    path ||= SERVICES_PATH
    return if @services_loaded
    load path
    @services_loaded = true
  end

  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

  def services_hash
    Hash[services.map { |s| [s.name.to_sym, s]}]
  end

  # Stop all services.  If service wasn't started by this service manager session, don't try and stop it.
  def stop
    return unless services.any? { |s| s.process }
    puts "Stopping the services..."
    services.map {|s| Thread.new { s.stop } }.map(&:join)
  end

  # Starts all configured services. If service is detected as running already, don't try and start it.
  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
end

require "service_manager/service"

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
service_manager-0.5 lib/service_manager.rb