Class: GodWeb

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ GodWeb

Returns a new instance of GodWeb.



2
3
4
5
6
# File 'lib/god_web.rb', line 2

def initialize(config)
  @config = config
  setup
  ping
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object (private)



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/god_web.rb', line 82

def method_missing(meth,*args)
  if %w{groups status log quit terminate}.include?(meth.to_s)
    ping
    send("#{meth}_command")
  elsif %w{start stop restart unmonitor monitor}.include?(meth.to_s)
    ping
    lifecycle_command(args.first, meth.to_s)
  else
    raise NoMethodError
  end
end

Class Method Details

.possible_statuses(status) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/god_web.rb', line 69

def self.possible_statuses(status)
  case status
  when :up
    return %w{stop restart unmonitor}
  when :unmonitored
    return %w{start monitor}
  else
    return %w{start stop restart}
  end
end

.watch(options = {}) ⇒ Object



8
9
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
46
47
48
49
# File 'lib/god_web.rb', line 8

def self.watch(options = {})
  options[:port] ||= 8888
  options[:environment] ||= 'production'
  start_string = "god_web #{options[:config]} -e #{options[:environment]} -p #{options[:port]}"
  God.watch do |w|
    w.name              = "god_web"
    w.interval          = 1.minute
    w.start             = start_string
    w.start_grace       = 10.seconds
    w.restart_grace     = 10.seconds

    w.start_if do |start|
      start.condition(:process_running) do |c|
        c.running = false
      end
    end

    w.restart_if do |restart|
      restart.condition(:memory_usage) do |c|
        c.above = 20.megabytes
        c.times = [3, 5]
      end

      restart.condition(:cpu_usage) do |c|
        c.above = 25.percent
        c.times = 5
      end
    end

    w.lifecycle do |on|
      on.condition(:flapping) do |c|
        c.to_state = [:start, :restart]
        c.times = 5
        c.within = 5.minute
        c.transition = :unmonitored
        c.retry_in = 10.minutes
        c.retry_times = 5
        c.retry_within = 2.hours
      end
    end
  end
end

Instance Method Details

#pingObject

ping server to ensure that it is responsive



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/god_web.rb', line 57

def ping
  tries = 3
  begin
    @server.ping
  rescue Exception => e
    if (tries -= 1) > 0
      retry
    end
    raise e, "The server is not available (or you do not have permissions to access it)"
  end
end

#setupObject



51
52
53
54
# File 'lib/god_web.rb', line 51

def setup
  DRb.start_service
  @server = DRbObject.new(nil, God::Socket.socket(@config['god_port']))
end