Sha256: 73653672ff51c6b641b21951abaee312f6c6961a661495b8b6680ca6d1330c20

Contents?: true

Size: 1.48 KB

Versions: 3

Compression:

Stored size: 1.48 KB

Contents

module Freyr
  class ServiceGroup < Array
    
    def initialize(*args)
      super(*args)
    end
    
    def find_by_name(n)
      find {|s| s.name == n}
    end
    
    # Take care this can make a stack overflow
    def run
      return [] if empty?
      
      needs_to_run = ServiceGroup.new
      
      kill = false
      names = []
      
      each do |svc|
        
        unless svc.dependencies.empty?
          if n = svc.dependencies.find {|s| !Service.alive?(s)}
            if find_by_name(n)
              needs_to_run << svc
            elsif s = Service[n].first
              needs_to_run << s
              needs_to_run << svc
            else
              puts "Can't run #{svc.name} because dependency #{n} cannot be found"
              kill = true
            end
            
            next
          end
        end
        
        Freyr.logger.debug('starting service') {svc.name}
        pid = svc.start!
        names << svc.name if pid
      end
      
      names += needs_to_run.run unless kill
      names
    end
    
    def stop
      changed_names = collect {|s| s.name if s.alive?}.compact
      each do |svc|
        Freyr.logger.debug('stopping service') {svc.name}
        svc.stop!
      end
      
      changed_names
    end
    
    def restart
      names = collect {|s| s.name}
      
      each do |s|
        Freyr.logger.debug('restart service') {s.name}
        s.restart!
        names.delete(s.name)
      end
      
      names
    end
    
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
freyr-0.3.13 lib/freyr/service_group.rb
freyr-0.3.12 lib/freyr/service_group.rb
freyr-0.3.11 lib/freyr/service_group.rb