Class: Daemonizer::Engine

Inherits:
Object
  • Object
show all
Defined in:
lib/daemonizer/engine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, debug = false) ⇒ Engine

Returns a new instance of Engine.



5
6
7
8
# File 'lib/daemonizer/engine.rb', line 5

def initialize(config, debug = false)
  @config = config
  Daemonizer.logger_context = "#{Process.pid}/monitor"
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



3
4
5
# File 'lib/daemonizer/engine.rb', line 3

def config
  @config
end

Instance Method Details

#debug!Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/daemonizer/engine.rb', line 59

def debug!
  Daemonizer.init_console_logger('console')
  @config.handler.logger = Daemonizer.logger
  
  init_block = Proc.new do
    begin
      @config.handler.worker_id = 1
      @config.handler.workers_count = 1
      run_callback :before_start, @config.pool, 1, 1
      @config.handler.start
    rescue Exception => e
      log_error(e)
    end
  end

  begin
    run_callback :before_prepare, @config.pool
    @config.handler.prepare(init_block) do
      run_callback :after_prepare, @config.pool
    end
  rescue Exception => e
    log_error(e)
  end
end

#log_error(e) ⇒ Object



84
85
86
87
# File 'lib/daemonizer/engine.rb', line 84

def log_error(e)
  Daemonizer.logger.fatal e.to_s
  Daemonizer.logger.fatal "#{e.class}: #{e}\n" + e.backtrace.join("\n")
end

#run_callback(callback, *args) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/daemonizer/engine.rb', line 10

def run_callback(callback, *args)
  if callbacks = @config.callbacks[callback.to_sym] and callbacks.any?
    Daemonizer.logger.info "Running :#{callback} callbacks"
    callbacks.each do |callback|
      callback.call(*args)
    end
  end
end

#start!Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/daemonizer/engine.rb', line 19

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
        run_callback :before_start, @config.pool, process_id, @config.workers
        @config.handler.start
      end
    rescue Exception => e
      log_error(e)
    end
  end

  begin
    if @config.cow_friendly
      if GC.respond_to?(:copy_on_write_friendly=)
      	GC.copy_on_write_friendly = true
      	Daemonizer.logger.info "Enabling COW-friendly feature"
      else
      	Daemonizer.logger.info "COW-friendly feature is not supported by currently used ruby version"
      end
    end
    @config.handler.logger = Daemonizer.logger
    run_callback :before_prepare, @config.pool
    Daemonizer.logger.info "Workers count is #{config.workers}"
    @config.handler.prepare(init_block) do
      run_callback :after_prepare, @config.pool
    end
  rescue Exception => e
    log_error(e)
  end
  # Start monitoring loop
  
  setup_signals
  @pm.monitor_workers
end