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
  GDC.set "#{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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/daemonizer/engine.rb', line 47

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
      @config.handler.start
    rescue Exception => e
      log_error(e)
    end
  end

  begin
    @config.handler.prepare(init_block)
  rescue Exception => e
    log_error(e)
  end
end

#log_error(e) ⇒ Object



68
69
70
71
# File 'lib/daemonizer/engine.rb', line 68

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

#start!Object



10
11
12
13
14
15
16
17
18
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
# File 'lib/daemonizer/engine.rb', line 10

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_fork
        @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
    Daemonizer.logger.info "Workers count is #{config.workers}"
    @config.handler.logger = Daemonizer.logger
    @config.handler.prepare(init_block)
  rescue Exception => e
    log_error(e)
  end
  # Start monitoring loop
  
  setup_signals
  @pm.monitor_workers
end