Sha256: 654a8ac869fee3f00399a7dcd3204de9e81e5523ea0c5aaabfc0aa74987d670c

Contents?: true

Size: 1.76 KB

Versions: 6

Compression:

Stored size: 1.76 KB

Contents

module Daemonizer
  class Engine
    attr_reader :config

    def initialize(config, debug = false)
      @config = config
    end
    
    def logger
      @config.logger
    end

    def start!
      @pm = ProcessManager.new(@config)

      init_block = Proc.new do
        begin
          @pm.start_workers do |process_id| 
            @config.handler.worker_id = process_id
            @config.handler.workers_count = @config.workers
            @config.handler.after_init
          end
        rescue Exception => e
          log_error(e)
        end
      end

      begin
        @config.handler.logger = logger        
        @config.handler.before_init(init_block)
      rescue Exception => e
        log_error(e)
      end
      # Start monitoring loop
      
      setup_signals
      @pm.monitor_workers
    end

    def debug!
      outputter = Outputter.stdout
      outputter.formatter = PatternFormatter.new :pattern => "%d - %l %g - %m"
      logger.outputters = outputter

      init_block = Proc.new do
        begin
          @config.handler.worker_id = 1
          @config.handler.workers_count = 1
          @config.handler.after_init
        rescue Exception => e
          log_error(e)
        end
      end

      begin
        @config.handler.logger = logger        
        @config.handler.before_init(init_block)
      rescue Exception => e
        log_error(e)
      end
    end
    
    def log_error(e)
      logger.fatal e.to_s
      logger.fatal "#{e.class}: #{e}\n" + e.backtrace.join("\n")
    end

    private
      def setup_signals
        stop = proc {
          @config.logger.info "Received a signal... stopping..."
          @pm.start_shutdown!
        }

        trap('TERM', stop)
        trap('INT', stop)
        trap('EXIT', stop)
      end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
daemonizer-0.1.2 lib/daemonizer/engine.rb
daemonizer-0.1.1 lib/daemonizer/engine.rb
daemonizer-0.1.0 lib/daemonizer/engine.rb
daemonizer-0.0.8 lib/daemonizer/engine.rb
daemonizer-0.0.7 lib/daemonizer/engine.rb
daemonizer-0.0.6 lib/daemonizer/engine.rb