firenxis-god 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (123) hide show
  1. data/Announce.txt +135 -0
  2. data/History.txt +393 -0
  3. data/README.txt +59 -0
  4. data/Rakefile +142 -0
  5. data/bin/god +132 -0
  6. data/ext/god/.gitignore +5 -0
  7. data/ext/god/extconf.rb +55 -0
  8. data/ext/god/kqueue_handler.c +125 -0
  9. data/ext/god/netlink_handler.c +168 -0
  10. data/god.gemspec +164 -0
  11. data/lib/god.rb +701 -0
  12. data/lib/god/behavior.rb +52 -0
  13. data/lib/god/behaviors/clean_pid_file.rb +21 -0
  14. data/lib/god/behaviors/clean_unix_socket.rb +21 -0
  15. data/lib/god/behaviors/notify_when_flapping.rb +51 -0
  16. data/lib/god/cli/command.rb +256 -0
  17. data/lib/god/cli/run.rb +172 -0
  18. data/lib/god/cli/version.rb +23 -0
  19. data/lib/god/compat19.rb +36 -0
  20. data/lib/god/condition.rb +96 -0
  21. data/lib/god/conditions/always.rb +23 -0
  22. data/lib/god/conditions/complex.rb +86 -0
  23. data/lib/god/conditions/cpu_usage.rb +80 -0
  24. data/lib/god/conditions/degrading_lambda.rb +52 -0
  25. data/lib/god/conditions/disk_usage.rb +32 -0
  26. data/lib/god/conditions/file_mtime.rb +28 -0
  27. data/lib/god/conditions/flapping.rb +128 -0
  28. data/lib/god/conditions/http_response_code.rb +168 -0
  29. data/lib/god/conditions/lambda.rb +25 -0
  30. data/lib/god/conditions/memory_usage.rb +82 -0
  31. data/lib/god/conditions/process_exits.rb +72 -0
  32. data/lib/god/conditions/process_running.rb +74 -0
  33. data/lib/god/conditions/tries.rb +44 -0
  34. data/lib/god/configurable.rb +57 -0
  35. data/lib/god/contact.rb +114 -0
  36. data/lib/god/contacts/campfire.rb +121 -0
  37. data/lib/god/contacts/email.rb +136 -0
  38. data/lib/god/contacts/jabber.rb +75 -0
  39. data/lib/god/contacts/prowl.rb +57 -0
  40. data/lib/god/contacts/scout.rb +55 -0
  41. data/lib/god/contacts/twitter.rb +51 -0
  42. data/lib/god/contacts/webhook.rb +73 -0
  43. data/lib/god/dependency_graph.rb +41 -0
  44. data/lib/god/diagnostics.rb +37 -0
  45. data/lib/god/driver.rb +206 -0
  46. data/lib/god/errors.rb +24 -0
  47. data/lib/god/event_handler.rb +108 -0
  48. data/lib/god/event_handlers/dummy_handler.rb +13 -0
  49. data/lib/god/event_handlers/kqueue_handler.rb +17 -0
  50. data/lib/god/event_handlers/netlink_handler.rb +13 -0
  51. data/lib/god/logger.rb +109 -0
  52. data/lib/god/metric.rb +59 -0
  53. data/lib/god/process.rb +363 -0
  54. data/lib/god/registry.rb +32 -0
  55. data/lib/god/simple_logger.rb +59 -0
  56. data/lib/god/socket.rb +107 -0
  57. data/lib/god/sugar.rb +47 -0
  58. data/lib/god/sys_logger.rb +45 -0
  59. data/lib/god/system/portable_poller.rb +42 -0
  60. data/lib/god/system/process.rb +50 -0
  61. data/lib/god/system/slash_proc_poller.rb +92 -0
  62. data/lib/god/task.rb +503 -0
  63. data/lib/god/timeline.rb +25 -0
  64. data/lib/god/trigger.rb +43 -0
  65. data/lib/god/watch.rb +188 -0
  66. data/test/configs/child_events/child_events.god +44 -0
  67. data/test/configs/child_events/simple_server.rb +3 -0
  68. data/test/configs/child_polls/child_polls.god +37 -0
  69. data/test/configs/child_polls/simple_server.rb +12 -0
  70. data/test/configs/complex/complex.god +59 -0
  71. data/test/configs/complex/simple_server.rb +3 -0
  72. data/test/configs/contact/contact.god +108 -0
  73. data/test/configs/contact/simple_server.rb +3 -0
  74. data/test/configs/daemon_events/daemon_events.god +37 -0
  75. data/test/configs/daemon_events/simple_server.rb +8 -0
  76. data/test/configs/daemon_events/simple_server_stop.rb +11 -0
  77. data/test/configs/daemon_polls/daemon_polls.god +17 -0
  78. data/test/configs/daemon_polls/simple_server.rb +6 -0
  79. data/test/configs/degrading_lambda/degrading_lambda.god +31 -0
  80. data/test/configs/degrading_lambda/tcp_server.rb +15 -0
  81. data/test/configs/lifecycle/lifecycle.god +25 -0
  82. data/test/configs/matias/matias.god +50 -0
  83. data/test/configs/real.rb +59 -0
  84. data/test/configs/running_load/running_load.god +16 -0
  85. data/test/configs/stop_options/simple_server.rb +12 -0
  86. data/test/configs/stop_options/stop_options.god +39 -0
  87. data/test/configs/stress/simple_server.rb +3 -0
  88. data/test/configs/stress/stress.god +15 -0
  89. data/test/configs/task/logs/.placeholder +0 -0
  90. data/test/configs/task/task.god +26 -0
  91. data/test/configs/test.rb +61 -0
  92. data/test/helper.rb +141 -0
  93. data/test/suite.rb +6 -0
  94. data/test/test_behavior.rb +18 -0
  95. data/test/test_campfire.rb +23 -0
  96. data/test/test_condition.rb +50 -0
  97. data/test/test_conditions_disk_usage.rb +50 -0
  98. data/test/test_conditions_http_response_code.rb +109 -0
  99. data/test/test_conditions_process_running.rb +40 -0
  100. data/test/test_conditions_tries.rb +67 -0
  101. data/test/test_contact.rb +109 -0
  102. data/test/test_dependency_graph.rb +62 -0
  103. data/test/test_driver.rb +11 -0
  104. data/test/test_email.rb +34 -0
  105. data/test/test_event_handler.rb +80 -0
  106. data/test/test_god.rb +570 -0
  107. data/test/test_handlers_kqueue_handler.rb +16 -0
  108. data/test/test_jabber.rb +29 -0
  109. data/test/test_logger.rb +55 -0
  110. data/test/test_metric.rb +72 -0
  111. data/test/test_process.rb +247 -0
  112. data/test/test_prowl.rb +15 -0
  113. data/test/test_registry.rb +15 -0
  114. data/test/test_socket.rb +34 -0
  115. data/test/test_sugar.rb +42 -0
  116. data/test/test_system_portable_poller.rb +17 -0
  117. data/test/test_system_process.rb +30 -0
  118. data/test/test_task.rb +246 -0
  119. data/test/test_timeline.rb +37 -0
  120. data/test/test_trigger.rb +59 -0
  121. data/test/test_watch.rb +279 -0
  122. data/test/test_webhook.rb +15 -0
  123. metadata +362 -0
@@ -0,0 +1,25 @@
1
+ module God
2
+
3
+ class Timeline < Array
4
+ # Instantiate a new Timeline
5
+ # +max_size+ is the maximum size to which the timeline should grow
6
+ #
7
+ # Returns Timeline
8
+ def initialize(max_size)
9
+ super()
10
+ @max_size = max_size
11
+ end
12
+
13
+ # Push a value onto the Timeline
14
+ # +val+ is the value to push
15
+ #
16
+ # Returns Timeline
17
+ def push(val)
18
+ self.concat([val])
19
+ shift if size > @max_size
20
+ end
21
+
22
+ alias_method :<<, :push
23
+ end
24
+
25
+ end
@@ -0,0 +1,43 @@
1
+ module God
2
+
3
+ class Trigger
4
+
5
+ class << self
6
+ attr_accessor :triggers # {task.name => condition}
7
+ end
8
+
9
+ # init
10
+ self.triggers = {}
11
+ @mutex = Mutex.new
12
+
13
+ def self.register(condition)
14
+ @mutex.synchronize do
15
+ self.triggers[condition.watch.name] ||= []
16
+ self.triggers[condition.watch.name] << condition
17
+ end
18
+ end
19
+
20
+ def self.deregister(condition)
21
+ @mutex.synchronize do
22
+ self.triggers[condition.watch.name].delete(condition)
23
+ self.triggers.delete(condition.watch.name) if self.triggers[condition.watch.name].empty?
24
+ end
25
+ end
26
+
27
+ def self.broadcast(task, message, payload)
28
+ return unless self.triggers[task.name]
29
+
30
+ @mutex.synchronize do
31
+ self.triggers[task.name].each do |t|
32
+ t.process(message, payload)
33
+ end
34
+ end
35
+ end
36
+
37
+ def self.reset
38
+ self.triggers.clear
39
+ end
40
+
41
+ end
42
+
43
+ end
data/lib/god/watch.rb ADDED
@@ -0,0 +1,188 @@
1
+ require 'etc'
2
+ require 'forwardable'
3
+
4
+ module God
5
+
6
+ class Watch < Task
7
+ VALID_STATES = [:init, :up, :start, :restart]
8
+ INITIAL_STATE = :init
9
+
10
+ # config
11
+ attr_accessor :grace, :start_grace, :stop_grace, :restart_grace
12
+
13
+ extend Forwardable
14
+ def_delegators :@process, :name, :uid, :gid, :start, :stop, :restart, :dir,
15
+ :name=, :uid=, :gid=, :start=, :stop=, :restart=,
16
+ :dir=, :pid_file, :pid_file=, :log, :log=,
17
+ :log_cmd, :log_cmd=, :err_log, :err_log=,
18
+ :err_log_cmd, :err_log_cmd=, :alive?, :pid,
19
+ :unix_socket, :unix_socket=, :chroot, :chroot=,
20
+ :env, :env=, :signal, :stop_timeout=,
21
+ :stop_signal=
22
+ #
23
+ def initialize
24
+ super
25
+
26
+ @process = God::Process.new
27
+
28
+ # valid states
29
+ self.valid_states = VALID_STATES
30
+ self.initial_state = INITIAL_STATE
31
+
32
+ # no grace period by default
33
+ self.grace = self.start_grace = self.stop_grace = self.restart_grace = 0
34
+ end
35
+
36
+ def valid?
37
+ super && @process.valid?
38
+ end
39
+
40
+ ###########################################################################
41
+ #
42
+ # Behavior
43
+ #
44
+ ###########################################################################
45
+
46
+ def behavior(kind)
47
+ # create the behavior
48
+ begin
49
+ b = Behavior.generate(kind, self)
50
+ rescue NoSuchBehaviorError => e
51
+ abort e.message
52
+ end
53
+
54
+ # send to block so config can set attributes
55
+ yield(b) if block_given?
56
+
57
+ # abort if the Behavior is invalid, the Behavior will have printed
58
+ # out its own error messages by now
59
+ abort unless b.valid?
60
+
61
+ self.behaviors << b
62
+ end
63
+
64
+ ###########################################################################
65
+ #
66
+ # Simple mode
67
+ #
68
+ ###########################################################################
69
+
70
+ def start_if
71
+ self.transition(:up, :start) do |on|
72
+ yield(on)
73
+ end
74
+ end
75
+
76
+ def restart_if
77
+ self.transition(:up, :restart) do |on|
78
+ yield(on)
79
+ end
80
+ end
81
+
82
+ def stop_if
83
+ self.transition(:up, :stop) do |on|
84
+ yield(on)
85
+ end
86
+ end
87
+
88
+ ###########################################################################
89
+ #
90
+ # Lifecycle
91
+ #
92
+ ###########################################################################
93
+
94
+ # Enable monitoring
95
+ def monitor
96
+ # start monitoring at the first available of the init or up states
97
+ if !self.metrics[:init].empty?
98
+ self.move(:init)
99
+ else
100
+ self.move(:up)
101
+ end
102
+ end
103
+
104
+ ###########################################################################
105
+ #
106
+ # Actions
107
+ #
108
+ ###########################################################################
109
+
110
+ def action(a, c = nil)
111
+ if !self.driver.in_driver_context?
112
+ # called from outside Driver
113
+
114
+ # send an async message to Driver
115
+ self.driver.message(:action, [a, c])
116
+ else
117
+ # called from within Driver
118
+
119
+ case a
120
+ when :start
121
+ call_action(c, :start)
122
+ sleep(self.start_grace + self.grace)
123
+ when :restart
124
+ if self.restart
125
+ call_action(c, :restart)
126
+ else
127
+ action(:stop, c)
128
+ action(:start, c)
129
+ end
130
+ sleep(self.restart_grace + self.grace)
131
+ when :stop
132
+ call_action(c, :stop)
133
+ sleep(self.stop_grace + self.grace)
134
+ end
135
+ end
136
+
137
+ self
138
+ end
139
+
140
+ def call_action(condition, action)
141
+ # before
142
+ before_items = self.behaviors
143
+ before_items += [condition] if condition
144
+ before_items.each do |b|
145
+ info = b.send("before_#{action}")
146
+ if info
147
+ msg = "#{self.name} before_#{action}: #{info} (#{b.base_name})"
148
+ applog(self, :info, msg)
149
+ end
150
+ end
151
+
152
+ # log
153
+ if self.send(action)
154
+ msg = "#{self.name} #{action}: #{self.send(action).to_s}"
155
+ applog(self, :info, msg)
156
+ end
157
+
158
+ @process.call_action(action)
159
+
160
+ # after
161
+ after_items = self.behaviors
162
+ after_items += [condition] if condition
163
+ after_items.each do |b|
164
+ info = b.send("after_#{action}")
165
+ if info
166
+ msg = "#{self.name} after_#{action}: #{info} (#{b.base_name})"
167
+ applog(self, :info, msg)
168
+ end
169
+ end
170
+ end
171
+
172
+ ###########################################################################
173
+ #
174
+ # Registration
175
+ #
176
+ ###########################################################################
177
+
178
+ def register!
179
+ God.registry.add(@process)
180
+ end
181
+
182
+ def unregister!
183
+ God.registry.remove(@process)
184
+ super
185
+ end
186
+ end
187
+
188
+ end
@@ -0,0 +1,44 @@
1
+ God.watch do |w|
2
+ w.name = "child-events"
3
+ w.interval = 5.seconds
4
+ w.start = File.join(GOD_ROOT, *%w[test configs child_events simple_server.rb])
5
+ # w.log = File.join(GOD_ROOT, *%w[test configs child_events god.log])
6
+
7
+ # determine the state on startup
8
+ w.transition(:init, { true => :up, false => :start }) do |on|
9
+ on.condition(:process_running) do |c|
10
+ c.running = true
11
+ end
12
+ end
13
+
14
+ # determine when process has finished starting
15
+ w.transition([:start, :restart], :up) do |on|
16
+ on.condition(:process_running) do |c|
17
+ c.running = true
18
+ end
19
+
20
+ # failsafe
21
+ on.condition(:tries) do |c|
22
+ c.times = 2
23
+ c.transition = :start
24
+ end
25
+ end
26
+
27
+ # start if process is not running
28
+ w.transition(:up, :start) do |on|
29
+ on.condition(:process_exits)
30
+ end
31
+
32
+ # lifecycle
33
+ w.lifecycle do |on|
34
+ on.condition(:flapping) do |c|
35
+ c.to_state = [:start, :restart]
36
+ c.times = 5
37
+ c.within = 20.seconds
38
+ c.transition = :unmonitored
39
+ c.retry_in = 10.seconds
40
+ c.retry_times = 2
41
+ c.retry_within = 5.minutes
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,3 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ loop { puts 'server'; sleep 1 }
@@ -0,0 +1,37 @@
1
+ God.watch do |w|
2
+ w.name = 'child-polls'
3
+ w.start = File.join(GOD_ROOT, *%w[test configs child_polls simple_server.rb])
4
+ w.interval = 5
5
+ w.grace = 2
6
+
7
+ w.start_if do |start|
8
+ start.condition(:process_running) do |c|
9
+ c.running = false
10
+ end
11
+ end
12
+
13
+ w.restart_if do |restart|
14
+ restart.condition(:cpu_usage) do |c|
15
+ c.above = 30.percent
16
+ c.times = [3, 5]
17
+ end
18
+
19
+ restart.condition(:memory_usage) do |c|
20
+ c.above = 10.megabytes
21
+ c.times = [3, 5]
22
+ end
23
+ end
24
+
25
+ # lifecycle
26
+ w.lifecycle do |on|
27
+ on.condition(:flapping) do |c|
28
+ c.to_state = [:start, :restart]
29
+ c.times = 3
30
+ c.within = 60.seconds
31
+ c.transition = :unmonitored
32
+ c.retry_in = 10.seconds
33
+ c.retry_times = 2
34
+ c.retry_within = 5.minutes
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,12 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ data = ''
4
+
5
+ loop do
6
+ STDOUT.puts('server');
7
+ STDOUT.flush;
8
+
9
+ 100000.times { data << 'x' }
10
+
11
+ sleep 10
12
+ end
@@ -0,0 +1,59 @@
1
+ God.watch do |w|
2
+ w.name = "complex"
3
+ w.interval = 5.seconds
4
+ w.start = File.join(GOD_ROOT, *%w[test configs complex simple_server.rb])
5
+ # w.log = File.join(GOD_ROOT, *%w[test configs child_events god.log])
6
+
7
+ # determine the state on startup
8
+ w.transition(:init, { true => :up, false => :start }) do |on|
9
+ on.condition(:process_running) do |c|
10
+ c.running = true
11
+ end
12
+ end
13
+
14
+ # determine when process has finished starting
15
+ w.transition([:start, :restart], :up) do |on|
16
+ on.condition(:process_running) do |c|
17
+ c.running = true
18
+ end
19
+
20
+ # failsafe
21
+ on.condition(:tries) do |c|
22
+ c.times = 2
23
+ c.transition = :start
24
+ end
25
+ end
26
+
27
+ # start if process is not running
28
+ w.transition(:up, :start) do |on|
29
+ on.condition(:process_exits)
30
+ end
31
+
32
+ # restart if process is misbehaving
33
+ w.transition(:up, :restart) do |on|
34
+ on.condition(:complex) do |cc|
35
+ cc.and(:cpu_usage) do |c|
36
+ c.above = 0.percent
37
+ c.times = 1
38
+ end
39
+
40
+ cc.and(:memory_usage) do |c|
41
+ c.above = 0.megabytes
42
+ c.times = 3
43
+ end
44
+ end
45
+ end
46
+
47
+ # lifecycle
48
+ w.lifecycle do |on|
49
+ on.condition(:flapping) do |c|
50
+ c.to_state = [:start, :restart]
51
+ c.times = 5
52
+ c.within = 20.seconds
53
+ c.transition = :unmonitored
54
+ c.retry_in = 10.seconds
55
+ c.retry_times = 2
56
+ c.retry_within = 5.minutes
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,3 @@
1
+ #! /usr/bin/env ruby
2
+
3
+ loop { puts 'server'; sleep 1 }
@@ -0,0 +1,108 @@
1
+ # God::Contacts::Campfire.defaults do |d|
2
+ # d.subdomain = 'github'
3
+ # d.token = '9fb768e421975cc1c6ff3f4f8306f890cb46e24f'
4
+ # d.room = 'Notices'
5
+ # d.ssl = true
6
+ # end
7
+ #
8
+ # God.contact(:campfire) do |c|
9
+ # c.name = 'tom4'
10
+ # end
11
+
12
+ # God.contact(:email) do |c|
13
+ # c.name = 'tom'
14
+ # c.group = 'developers'
15
+ # c.to_email = 'tom@lepton.local'
16
+ # c.from_email = 'god@github.com'
17
+ # c.from_name = 'God'
18
+ # c.delivery_method = :sendmail
19
+ # end
20
+
21
+ # God.contact(:email) do |c|
22
+ # c.name = 'tom'
23
+ # c.group = 'developers'
24
+ # c.to_email = 'tom@mojombo.com'
25
+ # c.from_email = 'god@github.com'
26
+ # c.from_name = 'God'
27
+ # c.server_host = 'smtp.rs.github.com'
28
+ # end
29
+
30
+ # God.contact(:prowl) do |c|
31
+ # c.name = 'tom3'
32
+ # c.apikey = 'f0fc8e1f3121672686337a631527eac2f1b6031c'
33
+ # c.group = 'developers'
34
+ # end
35
+
36
+ # God.contact(:twitter) do |c|
37
+ # c.name = 'tom6'
38
+ # c.consumer_token = 'gOhjax6s0L3mLeaTtBWPw'
39
+ # c.consumer_secret = 'yz4gpAVXJHKxvsGK85tEyzQJ7o2FEy27H1KEWL75jfA'
40
+ # c.access_token = '17376380-qS391nCrgaP4HKXAmZtM38gB56xUXMhx1NYbjT6mQ'
41
+ # c.access_secret = 'uMwCDeU4OXlEBWFQBc3KwGyY8OdWCtAV0Jg5KVB0'
42
+ # end
43
+
44
+ # God.contact(:scout) do |c|
45
+ # c.name = 'tom5'
46
+ # c.client_key = '583a51b5-acbc-2421-a830-b6f3f8e4b04e'
47
+ # c.plugin_id = '230641'
48
+ # end
49
+
50
+ # God.contact(:webhook) do |c|
51
+ # c.name = 'tom'
52
+ # c.url = "http://www.postbin.org/wk7guh"
53
+ # end
54
+
55
+ # God.contact(:jabber) do |c|
56
+ # c.name = 'tom'
57
+ # c.host = 'talk.google.com'
58
+ # c.to_jid = 'mojombo@jabber.org'
59
+ # c.from_jid = 'mojombo@gmail.com'
60
+ # c.password = 'secret'
61
+ # end
62
+
63
+ God.watch do |w|
64
+ w.name = "contact"
65
+ w.interval = 5.seconds
66
+ w.start = "ruby " + File.join(File.dirname(__FILE__), *%w[simple_server.rb])
67
+ w.log = "/Users/tom/contact.log"
68
+
69
+ # determine the state on startup
70
+ w.transition(:init, { true => :up, false => :start }) do |on|
71
+ on.condition(:process_running) do |c|
72
+ c.running = true
73
+ end
74
+ end
75
+
76
+ # determine when process has finished starting
77
+ w.transition([:start, :restart], :up) do |on|
78
+ on.condition(:process_running) do |c|
79
+ c.running = true
80
+ end
81
+
82
+ # failsafe
83
+ on.condition(:tries) do |c|
84
+ c.times = 2
85
+ c.transition = :start
86
+ end
87
+ end
88
+
89
+ # start if process is not running
90
+ w.transition(:up, :start) do |on|
91
+ on.condition(:process_exits) do |c|
92
+ c.notify = {:contacts => ['tom'], :priority => 1, :category => 'product'}
93
+ end
94
+ end
95
+
96
+ # lifecycle
97
+ w.lifecycle do |on|
98
+ on.condition(:flapping) do |c|
99
+ c.to_state = [:start, :restart]
100
+ c.times = 5
101
+ c.within = 20.seconds
102
+ c.transition = :unmonitored
103
+ c.retry_in = 10.seconds
104
+ c.retry_times = 2
105
+ c.retry_within = 5.minutes
106
+ end
107
+ end
108
+ end