Sha256: 4d60e7eec9fc5091f43e609582c7ccaad60714bf446f05539dd9b17ae9ed6349

Contents?: true

Size: 1.25 KB

Versions: 3

Compression:

Stored size: 1.25 KB

Contents

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

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

  extend self

  def 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[ServiceManager.services.map { |s| [s.name.to_sym, s]}]
  end

  def stop(which = :all)
    puts "Stopping the services..."
    services.map {|s| Thread.new { s.stop } }.map(&:join)
  end

  def start(which = :all)
    load_services
    raise RuntimeError, "No services defined" if services.empty?
    threads = services.map do |s|
      Thread.new do
        begin
          s.start
        rescue ServiceManager::Service::ServerDidntStart
          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

3 entries across 3 versions & 1 rubygems

Version Path
service_manager-0.4 lib/service_manager.rb
service_manager-0.3 lib/service_manager.rb
service_manager-0.2 lib/service_manager.rb