Class: Appserver::Monit

Inherits:
Struct
  • Object
show all
Defined in:
lib/appserver/monit.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server_dir) ⇒ Monit

Returns a new instance of Monit.



8
9
10
# File 'lib/appserver/monit.rb', line 8

def initialize (server_dir)
  self.server_dir = server_dir
end

Instance Attribute Details

#server_dirObject

Returns the value of attribute server_dir

Returns:

  • (Object)

    the current value of server_dir



2
3
4
# File 'lib/appserver/monit.rb', line 2

def server_dir
  @server_dir
end

Class Method Details

.write_config(server_dir) ⇒ Object



4
5
6
# File 'lib/appserver/monit.rb', line 4

def self.write_config (server_dir)
  new(server_dir).write_config
end

Instance Method Details

#write_configObject



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
# File 'lib/appserver/monit.rb', line 12

def write_config
  Utils.safe_replace_file(server_dir.monit_conf) do |f|
    f.puts "# Monit configuration automagically generated by the \"appserver\" gem using"
    f.puts "# the appserver directory config #{server_dir.config_file}"
    f.puts "# Include this file into your system's monitrc (using an include statement)"
    f.puts "# to use it. See http://github.com/zargony/appserver for details."
    # Let Monit reload itself if this configuration changes
    f.puts "check file monit_conf with path #{server_dir.monit_conf}"
    f.puts "  if changed checksum then exec \"#{server_dir.monit_reload}\""
    # Reload Nginx if its configuration changes
    f.puts "check file nginx_conf with path #{server_dir.nginx_conf}"
    f.puts "  if changed checksum then exec \"#{server_dir.nginx_reload}\""
    # Add application-specific Monit configuration
    server_dir.apps.each do |app|
      f.puts ""
      f.puts "# Application: #{app.name}"
      if app.pid_file && app.start_server?
        cyclecheck = app.usage_check_cycles ? " for #{app.usage_check_cycles} cycles" : ''
        f.puts "check process #{app.name} with pidfile #{app.pid_file}"
        f.puts "  start program = \"#{server_dir.appserver_cmd('start', app.name)}\""
        f.puts "  stop program = \"#{server_dir.appserver_cmd('stop', app.name)}\""
        f.puts "  if totalcpu usage > #{app.max_cpu_usage}#{cyclecheck} then restart" if app.max_cpu_usage
        f.puts "  if totalmemory usage > #{app.max_memory_usage}#{cyclecheck} then restart" if app.max_memory_usage
        f.puts "  if failed unixsocket #{app.socket} protocol http request \"/\" timeout #{app.http_check_timeout} seconds then restart" if app.http_check_timeout
        f.puts "  if 5 restarts within 5 cycles then timeout"
        f.puts "  group appserver"
        f.puts "check file #{app.name}_revision with path #{app.revision_file}"
        f.puts "  if changed checksum then exec \"#{server_dir.appserver_cmd('restart', app.name)}\""
      end
    end
  end
end