bluepill 0.1.6 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +50 -1
  3. data/CONTRIBUTING.md +2 -2
  4. data/README.md +105 -8
  5. data/bin/bluepill +25 -30
  6. data/bluepill.gemspec +21 -28
  7. data/lib/bluepill/application/lifecycle.rb +166 -0
  8. data/lib/bluepill/application/listener.rb +124 -0
  9. data/lib/bluepill/application/previous_daemon.rb +81 -0
  10. data/lib/bluepill/application.rb +57 -254
  11. data/lib/bluepill/base_directory.rb +93 -0
  12. data/lib/bluepill/condition_watch.rb +134 -56
  13. data/lib/bluepill/controller/directory.rb +59 -0
  14. data/lib/bluepill/controller.rb +99 -94
  15. data/lib/bluepill/daemonize.rb +234 -0
  16. data/lib/bluepill/dsl/app_proxy.rb +30 -6
  17. data/lib/bluepill/dsl/process_factory/children.rb +71 -0
  18. data/lib/bluepill/dsl/process_factory/registry.rb +69 -0
  19. data/lib/bluepill/dsl/process_factory.rb +81 -92
  20. data/lib/bluepill/dsl/process_proxy.rb +109 -21
  21. data/lib/bluepill/group.rb +187 -78
  22. data/lib/bluepill/logger.rb +139 -48
  23. data/lib/bluepill/pid_file.rb +23 -0
  24. data/lib/bluepill/process/attributes.rb +379 -0
  25. data/lib/bluepill/process/children.rb +70 -0
  26. data/lib/bluepill/process/pid.rb +154 -0
  27. data/lib/bluepill/process/state_machine.rb +196 -0
  28. data/lib/bluepill/process/transition.rb +45 -0
  29. data/lib/bluepill/process/transition_callbacks.rb +101 -0
  30. data/lib/bluepill/process/user_commands.rb +71 -0
  31. data/lib/bluepill/process/watches.rb +141 -0
  32. data/lib/bluepill/process.rb +56 -653
  33. data/lib/bluepill/process_commands/execution.rb +89 -0
  34. data/lib/bluepill/process_commands/hooks.rb +186 -0
  35. data/lib/bluepill/process_commands/stop_signals.rb +114 -0
  36. data/lib/bluepill/process_commands.rb +104 -172
  37. data/lib/bluepill/process_conditions/always_true.rb +1 -1
  38. data/lib/bluepill/process_conditions/below_threshold.rb +36 -0
  39. data/lib/bluepill/process_conditions/cpu_usage.rb +1 -25
  40. data/lib/bluepill/process_conditions/file_time.rb +4 -5
  41. data/lib/bluepill/process_conditions/http.rb +52 -24
  42. data/lib/bluepill/process_conditions/mem_usage.rb +9 -22
  43. data/lib/bluepill/process_conditions/process_condition.rb +34 -3
  44. data/lib/bluepill/process_conditions/running_time.rb +1 -25
  45. data/lib/bluepill/process_conditions/zombie_process.rb +1 -1
  46. data/lib/bluepill/process_conditions.rb +3 -2
  47. data/lib/bluepill/process_journal/cleanup.rb +200 -0
  48. data/lib/bluepill/process_journal/lock.rb +106 -0
  49. data/lib/bluepill/process_journal.rb +126 -233
  50. data/lib/bluepill/process_statistics.rb +2 -2
  51. data/lib/bluepill/socket.rb +60 -20
  52. data/lib/bluepill/system/command_setup.rb +98 -0
  53. data/lib/bluepill/system/commands.rb +219 -248
  54. data/lib/bluepill/system/daemon.rb +161 -0
  55. data/lib/bluepill/system/ps.rb +85 -0
  56. data/lib/bluepill/system/ps_table.rb +163 -0
  57. data/lib/bluepill/system.rb +45 -126
  58. data/lib/bluepill/trigger.rb +52 -26
  59. data/lib/bluepill/triggers/flapping.rb +7 -15
  60. data/lib/bluepill/units.rb +126 -0
  61. data/lib/bluepill/util/inflections.rb +34 -0
  62. data/lib/bluepill/util/levenshtein.rb +53 -0
  63. data/lib/bluepill/util/rotational_array.rb +1 -1
  64. data/lib/bluepill/version.rb +13 -12
  65. data/lib/bluepill.rb +33 -36
  66. data/sig/bluepill/application.rbs +257 -0
  67. data/sig/bluepill/checks.rbs +203 -0
  68. data/sig/bluepill/dsl.rbs +126 -0
  69. data/sig/bluepill/logger.rbs +104 -0
  70. data/sig/bluepill/process.rbs +542 -0
  71. data/sig/bluepill/system.rbs +316 -0
  72. data/sig/bluepill.rbs +47 -873
  73. metadata +39 -45
@@ -0,0 +1,234 @@
1
+ # The parts of the daemons gem that bluepill uses, which bluepill used to
2
+ # get from blue-daemons, a fork of it. They're adapted from daemons'
3
+ # lib/daemons/daemonize.rb, which carries this notice:
4
+ #
5
+ # Copyright (c) 2005-2012 Thomas Uehlinger
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person
8
+ # obtaining a copy of this software and associated documentation
9
+ # files (the "Software"), to deal in the Software without
10
+ # restriction, including without limitation the rights to use,
11
+ # copy, modify, merge, publish, distribute, sublicense, and/or sell
12
+ # copies of the Software, and to permit persons to whom the
13
+ # Software is furnished to do so, subject to the following
14
+ # conditions:
15
+ #
16
+ # The above copyright notice and this permission notice shall be
17
+ # included in all copies or substantial portions of the Software.
18
+ #
19
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
21
+ # OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22
+ # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
23
+ # HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24
+ # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25
+ # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26
+ # OTHER DEALINGS IN THE SOFTWARE.
27
+
28
+ module Bluepill
29
+ # Turn the bluepill daemon, or a block, into a daemon process
30
+ #
31
+ # A daemon forks twice and starts a new session in between, so it has no
32
+ # controlling terminal and init adopts it. It also leaves the directory
33
+ # it started in, closes the files it inherited, and points its standard
34
+ # streams at /dev/null.
35
+ #
36
+ # @api private
37
+ module Daemonize
38
+ # Raised when a daemon can't detach from its controlling terminal
39
+ #
40
+ # @api private
41
+ class Error < RuntimeError; end
42
+
43
+ # The highest file descriptor that close_io closes
44
+ MAX_FD = 8191
45
+
46
+ # Fork, retrying every 5 seconds while the system is out of processes
47
+ #
48
+ # @return [Integer, nil] the child's pid in the parent, nil in the child
49
+ #
50
+ # @api private
51
+ def self.safefork
52
+ loop do
53
+ begin
54
+ return fork
55
+ rescue Errno::EWOULDBLOCK
56
+ sleep 5
57
+ end
58
+ end
59
+ end
60
+
61
+ # Run a block in a daemon, and return the daemon's pid
62
+ #
63
+ # The forked processes exit with Process.exit!, never exit. They're
64
+ # copies of the bluepill daemon made while it handles a command, so exit
65
+ # would unwind the daemon's own stack in the copy, run its ensure
66
+ # blocks against the socket it shares with the real daemon, and make
67
+ # `bluepill start` hang (https://github.com/bluepill-rb/bluepill/issues/1).
68
+ #
69
+ # @param block [Proc] what the daemon runs, which usually execs
70
+ # @param name [String, nil] the daemon's process title until then
71
+ # @return [Integer] the daemon's pid, in the calling process
72
+ #
73
+ # @api private
74
+ def self.call_as_daemon(block, name = nil)
75
+ rd, wr = IO.pipe
76
+
77
+ intermediate = safefork
78
+ return read_daemon_pid(intermediate, rd, wr) if intermediate
79
+
80
+ rd.close
81
+ run_daemon(block, name, wr)
82
+ end
83
+
84
+ # Read the daemon's pid, and reap the intermediate process that forked it
85
+ #
86
+ # @param intermediate [Integer] the intermediate process's pid
87
+ # @param reader [IO] the end of the pipe that the daemon writes its pid to
88
+ # @param writer [IO] the other end, which only the daemon uses
89
+ # @return [Integer] the daemon's pid, or 0 if it didn't report one
90
+ #
91
+ # @api private
92
+ def self.read_daemon_pid(intermediate, reader, writer)
93
+ writer.close
94
+ pid = reader.read.to_i
95
+ reader.close
96
+ ::Process.waitpid(intermediate)
97
+ pid
98
+ end
99
+
100
+ # Fork the daemon from the intermediate process, and run the block in it
101
+ #
102
+ # The intermediate process starts a new session and exits once it has
103
+ # forked. The daemon reports its pid, becomes a daemon, and runs the
104
+ # block.
105
+ #
106
+ # @param block [Proc] what the daemon runs
107
+ # @param name [String, nil] the daemon's process title
108
+ # @param writer [IO] where the daemon writes its pid
109
+ # @return [void] it never returns, since both processes exit
110
+ #
111
+ # @api private
112
+ def self.run_daemon(block, name, writer)
113
+ detach
114
+ if safefork
115
+ writer.close
116
+ ::Process.exit!(true)
117
+ end
118
+
119
+ writer.write(::Process.pid)
120
+ writer.close
121
+ become_daemon(name)
122
+ block.call
123
+ ::Process.exit!(true)
124
+ end
125
+
126
+ # Turn the calling process into a daemon
127
+ #
128
+ # The process that called it exits, so `bluepill load` returns, and the
129
+ # daemon carries on from here.
130
+ #
131
+ # @return [Integer] the daemon's session id
132
+ #
133
+ # @api private
134
+ def self.daemonize
135
+ exit if safefork
136
+
137
+ session = detach
138
+ ::Process.exit!(true) if safefork
139
+
140
+ become_daemon
141
+ session
142
+ end
143
+
144
+ # Start a new session, which leaves the controlling terminal
145
+ #
146
+ # @return [Integer] the new session's id
147
+ # @raise [Error] if there's no session to start
148
+ #
149
+ # @api private
150
+ def self.detach
151
+ session = ::Process.setsid
152
+ raise Error, "cannot detach from controlling terminal" unless session
153
+
154
+ # The session leader exits after the next fork, so the daemon can
155
+ # never get a controlling terminal, and it ignores the hangup that
156
+ # its exit sends
157
+ Signal.trap("SIGHUP", "IGNORE")
158
+ session
159
+ end
160
+
161
+ # Do what's left of becoming a daemon, in the process that stays
162
+ #
163
+ # @param name [String, nil] a new process title
164
+ # @return [void]
165
+ #
166
+ # @api private
167
+ def self.become_daemon(name = nil)
168
+ $0 = name if name
169
+ Dir.chdir("/") # don't keep the directory it started in in use
170
+ close_io
171
+ redirect_io
172
+ srand # so the daemon and its parent don't share random numbers
173
+ end
174
+
175
+ # Close every file the process inherited, except the standard streams
176
+ #
177
+ # @return [void]
178
+ #
179
+ # @api private
180
+ def self.close_io
181
+ ObjectSpace.each_object(IO) do |io|
182
+ next if [STDIN, STDOUT, STDERR].include?(io)
183
+
184
+ begin
185
+ io.close unless io.closed?
186
+ rescue
187
+ nil
188
+ end
189
+ end
190
+
191
+ # And the file descriptors that no IO object holds
192
+ close_fds
193
+ end
194
+
195
+ # Close the file descriptors above the standard streams' up to MAX_FD
196
+ #
197
+ # @return [void]
198
+ #
199
+ # @api private
200
+ def self.close_fds
201
+ (3..MAX_FD).each do |fd|
202
+ begin
203
+ IO.for_fd(fd).close
204
+ rescue
205
+ nil
206
+ end
207
+ end
208
+ end
209
+
210
+ # Point the standard streams at /dev/null
211
+ #
212
+ # @return [void]
213
+ #
214
+ # @api private
215
+ def self.redirect_io
216
+ quietly { STDIN.reopen(File::NULL) }
217
+ quietly { STDOUT.reopen(File::NULL) }
218
+ quietly { STDERR.reopen(STDOUT) }
219
+ STDERR.sync = true
220
+ end
221
+
222
+ # Run a block, and carry on if it raises
223
+ #
224
+ # @yield what to try, such as reopening a standard stream
225
+ # @return [void]
226
+ #
227
+ # @api private
228
+ def self.quietly
229
+ yield
230
+ rescue
231
+ nil
232
+ end
233
+ end
234
+ end
@@ -14,7 +14,7 @@ module Bluepill
14
14
  class AppProxy
15
15
  # The attributes that every process of the application gets, unless the
16
16
  # process sets its own
17
- APP_ATTRIBUTES = [:working_dir, :uid, :gid, :environment, :auto_start].freeze
17
+ APP_ATTRIBUTES = %i[working_dir uid gid supplementary_groups environment auto_start notify_command].freeze
18
18
 
19
19
  # The directory that the application's processes start in
20
20
  #
@@ -46,6 +46,19 @@ module Bluepill
46
46
  # @api public
47
47
  attr_accessor :gid
48
48
 
49
+ # More groups that the application's processes run with
50
+ #
51
+ # The processes already get every group that their uid belongs to. These
52
+ # are added to those.
53
+ #
54
+ # @example
55
+ # app.supplementary_groups = ['rvm', 'ssl-cert']
56
+ #
57
+ # @return [Array<String>, nil] the groups' names
58
+ #
59
+ # @api public
60
+ attr_accessor :supplementary_groups
61
+
49
62
  # The environment variables that the application's processes get
50
63
  #
51
64
  # @example
@@ -67,6 +80,19 @@ module Bluepill
67
80
  # @api public
68
81
  attr_accessor :auto_start
69
82
 
83
+ # The command the application's processes run on every change of state
84
+ #
85
+ # A process can set its own. See Process#notify_command for the
86
+ # environment variables it gets.
87
+ #
88
+ # @example
89
+ # app.notify_command = '/usr/local/bin/alert'
90
+ #
91
+ # @return [String, nil] the command, without a shell
92
+ #
93
+ # @api public
94
+ attr_accessor :notify_command
95
+
70
96
  # Return the application that the config describes
71
97
  #
72
98
  # @return [Application] the application
@@ -84,6 +110,7 @@ module Bluepill
84
110
  # @api private
85
111
  def initialize(app_name, options)
86
112
  @app = Application.new(app_name.to_s, options)
113
+ @registry = ProcessFactory::Registry.new
87
114
  end
88
115
 
89
116
  # Define a process of the application
@@ -112,11 +139,8 @@ module Bluepill
112
139
  attributes = {} #: Hash[Symbol, untyped]
113
140
  APP_ATTRIBUTES.each { |a| attributes[a] = public_send(a) }
114
141
 
115
- process_factory = ProcessFactory.new(attributes, process_block)
116
-
117
- process = process_factory.create_process(process_name, @app.pids_dir)
118
- group = process_factory.attributes.delete(:group)
119
-
142
+ process_factory = ProcessFactory.new(attributes, process_block, @registry)
143
+ process, group = process_factory.create_process(process_name, @app.pids_dir)
120
144
  @app.add_process(process, group)
121
145
  end
122
146
  end
@@ -0,0 +1,71 @@
1
+ module Bluepill
2
+ class ProcessFactory
3
+ # Create the processes of the children that a process's factory finds
4
+ #
5
+ # A process that monitors its children gets a factory of its own, with
6
+ # the process block that configures each child. Process asks it for a
7
+ # child's process when it finds a new child.
8
+ #
9
+ # @api private
10
+ module Children
11
+ # Create the process of a child that bluepill found
12
+ #
13
+ # The child gets its parent's grace times, the parent's user and groups,
14
+ # so its commands run as the parent's do, and its state from whether
15
+ # it's running.
16
+ #
17
+ # @param [String] name the child's name
18
+ # @param [Integer] pid the child's pid
19
+ # @param [Logger] logger the child's logger
20
+ #
21
+ # @return [Process] the child's process
22
+ #
23
+ # @api private
24
+ def create_child_process(name, pid, logger)
25
+ attributes = inherited_attributes
26
+ attributes[:actual_pid] = pid
27
+ attributes[:logger] = logger
28
+
29
+ child = ProcessProxy.new(name, attributes, @process_block)
30
+ validate_child_process! child
31
+ process = child.to_process
32
+
33
+ process.determine_initial_state
34
+ process
35
+ end
36
+
37
+ protected
38
+
39
+ # Return the attributes a child gets from its parent
40
+ #
41
+ # These are the grace times, user and groups that the parent sets.
42
+ #
43
+ # @return [Hash{Symbol => Object}] the attributes
44
+ #
45
+ # @api private
46
+ def inherited_attributes
47
+ attributes = {} #: Hash[Symbol, untyped]
48
+ # A grace time the parent leaves unset keeps the Process default
49
+ %i[start_grace_time stop_grace_time restart_grace_time uid gid supplementary_groups].each do |a|
50
+ attributes[a] = @attributes.fetch(a) if @attributes.key?(a)
51
+ end
52
+ attributes
53
+ end
54
+
55
+ # Exit unless a monitored child has a stop_command
56
+ #
57
+ # @param [ProcessProxy] child the child's proxy
58
+ #
59
+ # @return [void]
60
+ #
61
+ # @api private
62
+ def validate_child_process!(child)
63
+ return if child.attributes.key?(:stop_command)
64
+
65
+ $stderr.puts "Config Error: Invalid child process monitor for #{child.name}"
66
+ $stderr.puts "You must specify a stop command to monitor child processes."
67
+ exit(6)
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,69 @@
1
+ module Bluepill
2
+ class ProcessFactory
3
+ # The process names and pid files that an application's config has used
4
+ #
5
+ # Each {AppProxy} keeps one and hands it to the factory of each of its
6
+ # processes, so the factories can catch two processes with the same name
7
+ # in a group, or the same pid file. An application that loads again
8
+ # starts with an empty registry.
9
+ #
10
+ # @api private
11
+ class Registry
12
+ # Create an empty registry
13
+ #
14
+ # @return [void]
15
+ #
16
+ # @api private
17
+ def initialize
18
+ # Only the keys matter
19
+ @process_keys = {} #: Hash[Array[String?], nil]
20
+ @pid_files = {} #: Hash[String, nil]
21
+ end
22
+
23
+ # Add a process's name in its group, unless it's there already
24
+ #
25
+ # A process name may contain ':', so the key keeps the group and the
26
+ # name apart. Like Application#add_process, it treats Symbols and
27
+ # Strings alike.
28
+ #
29
+ # @param [#to_s, nil] group the process's group, or nil for none
30
+ # @param [#to_s] name the process's name
31
+ #
32
+ # @return [Boolean] true for a new name, false for one already added
33
+ #
34
+ # @api private
35
+ def add_process?(group, name)
36
+ add?(@process_keys, [group, name].collect { |part| part && part.to_s })
37
+ end
38
+
39
+ # Add a pid file, unless it's there already
40
+ #
41
+ # @param [String] pid_file the pid file
42
+ #
43
+ # @return [Boolean] true for a new pid file, false for one already
44
+ # added
45
+ #
46
+ # @api private
47
+ def add_pid_file?(pid_file)
48
+ add?(@pid_files, pid_file)
49
+ end
50
+
51
+ private
52
+
53
+ # Add a key to a hash, unless it's there already
54
+ #
55
+ # @param [Hash{Object => nil}] keys the keys added so far
56
+ # @param [Object] key the key to add
57
+ #
58
+ # @return [Boolean] true for a new key, false for one already added
59
+ #
60
+ # @api private
61
+ def add?(keys, key)
62
+ return false if keys.key?(key)
63
+
64
+ keys[key] = nil
65
+ true
66
+ end
67
+ end
68
+ end
69
+ end