Class: ProxyMgr::Haproxy::State

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/proxymgr/haproxy/state.rb

Instance Method Summary collapse

Methods included from Logging

disable!, #logger, logger

Constructor Details

#initialize(process, config_file, socket_manager, socket = nil, opts = {}) ⇒ State

Returns a new instance of State.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/proxymgr/haproxy/state.rb', line 10

def initialize(process, config_file, socket_manager, socket = nil, opts = {})
  @process         = process
  @config_file     = config_file
  @socket          = socket
  @socket_manager  = socket_manager

  @sleep_interval  = opts[:sleep_interval] || 5
  @global_config   = opts[:global]
  @defaults_config = opts[:defaults]
  @socket_path     = opts[:socket_path]

  @file_descriptors = {}
  @backends         = {}
  @config_template  = ERB.new(File.read(File.join(ProxyMgr.template_dir, 'haproxy.cfg.erb')))
  @mutex            = Mutex.new
  @cv               = ConditionVariable.new
end

Instance Method Details

#socket?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/proxymgr/haproxy/state.rb', line 68

def socket?
  @socket and @socket.connected?
end

#startObject



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
58
59
60
61
62
63
64
65
66
# File 'lib/proxymgr/haproxy/state.rb', line 28

def start
  write_config

  @thread = Thread.new do
    @mutex.synchronize do
      sleep_interval = nil
      loop do
        logger.debug "Waiting..."
        wait(sleep_interval)

        restart_needed = true

        if @changeset or @backends
          if @changeset
            update_state_with_changeset
            restart_needed = @changeset.restart_needed?
          end
          @file_descriptors = @socket_manager.update(@backends)
          write_config
          @changeset = nil
          @backends  = nil
        elsif @process.exited? and !sleep_interval
          sleep_interval = @sleep_interval
          logger.info "Haproxy exited abnormally. Sleeping for #{sleep_interval}s"
          next
        end

        sleep_interval = nil
        @process.restart(@file_descriptors.values) if restart_needed
      end
    end
  end
  @thread.abort_on_exception = true

  @process.on_stop do |status|
    Thread.new { signal }.join if @process.exited?
  end
  @process.start
end

#stopObject



80
81
82
83
84
85
86
87
88
# File 'lib/proxymgr/haproxy/state.rb', line 80

def stop
  @thread.kill
  signal
  @thread.join
  begin
    @process.stop
  rescue Errno::ESRCH # sometimes there's no process here. that's OK.
  end
end

#update_state(backends, changeset) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/proxymgr/haproxy/state.rb', line 72

def update_state(backends, changeset)
  @mutex.synchronize do
    @changeset = changeset
    @backends  = backends
  end
  signal
end