bluepill 0.1.5 → 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 +57 -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 +59 -231
  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 -869
  73. metadata +39 -45
@@ -0,0 +1,196 @@
1
+ module Bluepill
2
+ class Process
3
+ # Moves a Process between its states
4
+ #
5
+ # A process is unmonitored, up, down, starting, stopping or restarting.
6
+ # Ticks and the start, stop, unmonitor and restart events move it, and
7
+ # TransitionCallbacks run on the way.
8
+ #
9
+ # @api private
10
+ module StateMachine
11
+ # Where a tick moves a process from each state: to the first state if
12
+ # it's running, or to the second if it isn't
13
+ #
14
+ # Unmonitored is the one state a tick doesn't change. Down means bluepill
15
+ # knows the process isn't running, and unmonitored means it doesn't
16
+ # care. A process that's stopping and still running failed to stop, so
17
+ # a tick shows it as up.
18
+ TICKS = {
19
+ starting: %i[up down],
20
+ up: %i[up down],
21
+ stopping: %i[up down],
22
+ down: %i[up starting],
23
+ restarting: %i[up down]
24
+ }.freeze
25
+
26
+ # Where every other event moves a process from each state it can leave
27
+ TRANSITIONS = {
28
+ start: {unmonitored: :starting, down: :starting},
29
+ stop: {up: :stopping},
30
+ unmonitor: {
31
+ unmonitored: :unmonitored, up: :unmonitored, down: :unmonitored,
32
+ starting: :unmonitored, stopping: :unmonitored, restarting: :unmonitored
33
+ },
34
+ restart: {up: :restarting, down: :restarting}
35
+ }.freeze
36
+
37
+ # Every state a process can be in, since unmonitor leaves any of them
38
+ STATES = TRANSITIONS.fetch(:unmonitor).keys.freeze
39
+
40
+ # The transition from each state to itself, made once, so a tick that
41
+ # leaves the state alone makes no new object
42
+ LOOPBACKS = Hash[STATES.zip(STATES.collect { |state| Transition.new(state, state).freeze })].freeze
43
+
44
+ # Return the process's state, such as :up
45
+ #
46
+ # @return [Symbol]
47
+ #
48
+ # @api private
49
+ attr_accessor :state
50
+
51
+ # Return whether the process is unmonitored
52
+ #
53
+ # @return [Boolean]
54
+ #
55
+ # @api private
56
+ def unmonitored?
57
+ state.equal?(:unmonitored)
58
+ end
59
+
60
+ # Return whether the process is up
61
+ #
62
+ # @return [Boolean]
63
+ #
64
+ # @api private
65
+ def up?
66
+ state.equal?(:up)
67
+ end
68
+
69
+ # Return whether the process is down
70
+ #
71
+ # @return [Boolean]
72
+ #
73
+ # @api private
74
+ def down?
75
+ state.equal?(:down)
76
+ end
77
+
78
+ # Return whether the process is starting
79
+ #
80
+ # @return [Boolean]
81
+ #
82
+ # @api private
83
+ def starting?
84
+ state.equal?(:starting)
85
+ end
86
+
87
+ # Return whether the process is stopping
88
+ #
89
+ # @return [Boolean]
90
+ #
91
+ # @api private
92
+ def stopping?
93
+ state.equal?(:stopping)
94
+ end
95
+
96
+ # Return whether the process is restarting
97
+ #
98
+ # @return [Boolean]
99
+ #
100
+ # @api private
101
+ def restarting?
102
+ state.equal?(:restarting)
103
+ end
104
+
105
+ # Start the process, if it's unmonitored or down
106
+ #
107
+ # @return [Boolean] whether the process moved to starting
108
+ #
109
+ # @api private
110
+ def start
111
+ fire_event(:start)
112
+ end
113
+
114
+ # Stop the process, if it's up
115
+ #
116
+ # @return [Boolean] whether the process moved to stopping
117
+ #
118
+ # @api private
119
+ def stop
120
+ fire_event(:stop)
121
+ end
122
+
123
+ # Stop monitoring the process, from any state
124
+ #
125
+ # @return [Boolean] whether the process moved to unmonitored
126
+ #
127
+ # @api private
128
+ def unmonitor
129
+ fire_event(:unmonitor)
130
+ end
131
+
132
+ # Restart the process, if it's up or down
133
+ #
134
+ # @return [Boolean] whether the process moved to restarting
135
+ #
136
+ # @api private
137
+ def restart
138
+ fire_event(:restart)
139
+ end
140
+
141
+ # Move the process to the state an event leads to from its state
142
+ #
143
+ # The before callbacks see the old state, and can throw :halt to cancel
144
+ # the move. The after callbacks see the new one.
145
+ #
146
+ # @param [Symbol] event :tick, :start, :stop, :unmonitor or :restart
147
+ #
148
+ # @return [Boolean] whether the process moved, false when the event
149
+ # doesn't lead anywhere from its state or a callback halted it
150
+ #
151
+ # @api private
152
+ def fire_event(event)
153
+ from = state
154
+ to = event.equal?(:tick) ? tick_target(from) : TRANSITIONS.fetch(event)[from]
155
+ return false unless to
156
+
157
+ transition = from.equal?(to) ? LOOPBACKS.fetch(from) : Transition.new(from, to)
158
+ return false unless catch(:halt) { before_transition(transition) }
159
+
160
+ self.state = to
161
+ after_transition(transition)
162
+ true
163
+ end
164
+
165
+ # Return where a tick moves the process from a state
166
+ #
167
+ # @param [Symbol] from the process's state
168
+ #
169
+ # @return [Symbol, nil] the next state, or nil for unmonitored
170
+ #
171
+ # @api private
172
+ def tick_target(from)
173
+ targets = TICKS[from]
174
+ return unless targets
175
+
176
+ process_running? ? targets.first : targets.last
177
+ end
178
+
179
+ # Set the state to up, down or unmonitored when bluepill loads
180
+ #
181
+ # A running process is up. Otherwise it's down, so that it starts, unless
182
+ # auto_start is false.
183
+ #
184
+ # @return [void]
185
+ #
186
+ # @api private
187
+ def determine_initial_state
188
+ if process_running?(true)
189
+ self.state = :up
190
+ return
191
+ end
192
+ self.state = auto_start.equal?(false) ? :unmonitored : :down # we need to check for false value
193
+ end
194
+ end
195
+ end
196
+ end
@@ -0,0 +1,45 @@
1
+ module Bluepill
2
+ class Process
3
+ # A process's move from one state to another, as triggers and the
4
+ # transition callbacks see it
5
+ #
6
+ # @api private
7
+ class Transition
8
+ # Return the state the process is leaving
9
+ #
10
+ # @return [Symbol]
11
+ #
12
+ # @api private
13
+ attr_reader :from_name
14
+
15
+ # Return the state the process is going to
16
+ #
17
+ # @return [Symbol]
18
+ #
19
+ # @api private
20
+ attr_reader :to_name
21
+
22
+ # Create a transition between two states
23
+ #
24
+ # @param [Symbol] from_name the state the process is leaving
25
+ # @param [Symbol] to_name the state the process is going to
26
+ #
27
+ # @return [void]
28
+ #
29
+ # @api private
30
+ def initialize(from_name, to_name)
31
+ @from_name = from_name
32
+ @to_name = to_name
33
+ end
34
+
35
+ # Return whether the process stays in the same state
36
+ #
37
+ # @return [Boolean]
38
+ #
39
+ # @api private
40
+ def loopback?
41
+ from_name.equal?(to_name)
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,101 @@
1
+ module Bluepill
2
+ class Process
3
+ # Runs what a Process does on its way from one state to another
4
+ #
5
+ # Before a transition, the triggers hear of it. After it, the process is
6
+ # started, stopped or restarted for the state it reached, its watches
7
+ # forget what they read, and its hooks run.
8
+ #
9
+ # @api private
10
+ module TransitionCallbacks
11
+ # The states that a start or restart moves a process through on its
12
+ # way up, after which post_start_command runs
13
+ STARTS = %i[starting restarting].freeze
14
+
15
+ # Run the callbacks before a transition
16
+ #
17
+ # They tell the triggers, and clean up the threads of a stop on the way
18
+ # out of stopping.
19
+ #
20
+ # @param [Transition] transition the move about to happen
21
+ #
22
+ # @return [true] so fire_event knows no callback halted
23
+ #
24
+ # @api private
25
+ def before_transition(transition)
26
+ notify_triggers(transition)
27
+ clean_threads if transition.from_name.equal?(:stopping)
28
+ true
29
+ end
30
+
31
+ # Run the callbacks after a transition
32
+ #
33
+ # They start, stop or restart the process for the state it reached, and
34
+ # then record the transition. Then they run post_start_command when a
35
+ # start or restart brought the process up, and notify_command when its
36
+ # state changed, in that order, as state_machine ran them.
37
+ #
38
+ # @param [Transition] transition the move that happened
39
+ #
40
+ # @return [void]
41
+ #
42
+ # @api private
43
+ def after_transition(transition)
44
+ case transition.to_name
45
+ when :starting then start_process
46
+ when :stopping then stop_process
47
+ when :restarting then restart_process
48
+ end
49
+ record_transition(transition)
50
+ post_start_process if transition.to_name.equal?(:up) && STARTS.include?(transition.from_name)
51
+ notify_transition(transition)
52
+ end
53
+
54
+ # Tell every trigger about a transition
55
+ #
56
+ # @param [#from_name, #to_name, #loopback?] transition the transition
57
+ #
58
+ # @return [void]
59
+ #
60
+ # @raise [StandardError] what a trigger raised, after logging it
61
+ #
62
+ # @api private
63
+ def notify_triggers(transition)
64
+ triggers.each do |trigger|
65
+ begin
66
+ trigger.notify(transition)
67
+ rescue => e
68
+ logger.err e.backtrace
69
+ raise
70
+ end
71
+ end
72
+ end
73
+
74
+ # Clear the watches' history after the process changes state
75
+ #
76
+ # A process that's monitoring its children also clears their list, so it
77
+ # finds them again. A loopback, such as up to up, changes nothing.
78
+ #
79
+ # @param [#from_name, #to_name, #loopback?] transition the transition
80
+ #
81
+ # @return [void]
82
+ #
83
+ # @api private
84
+ def record_transition(transition)
85
+ return if transition.loopback?
86
+
87
+ @transitioned = true
88
+
89
+ # When a process changes state, we should clear the memory of all the watches
90
+ watches.clear_history!
91
+
92
+ # Also, when a process changes state, we should re-populate its child list
93
+ if monitor_children?
94
+ logger.warning "Clearing child list"
95
+ children.clear
96
+ end
97
+ logger.info "Going from #{transition.from_name} => #{transition.to_name}"
98
+ end
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,71 @@
1
+ module Bluepill
2
+ class Process
3
+ # Carries out the commands a user gives a Process from the command line
4
+ #
5
+ # @api private
6
+ module UserCommands
7
+ # Carry out a command from the command line
8
+ #
9
+ # @param [Symbol] cmd :start, :stop, :restart or :unmonitor
10
+ #
11
+ # @return [void]
12
+ #
13
+ # @api private
14
+ def handle_user_command(cmd)
15
+ case cmd
16
+ when :start then user_start
17
+ when :stop then user_stop
18
+ when :restart then user_restart
19
+ when :unmonitor then user_unmonitor
20
+ end
21
+ end
22
+
23
+ # Start the process, unless it's already running
24
+ #
25
+ # @return [void]
26
+ #
27
+ # @api private
28
+ def user_start
29
+ if process_running?(true)
30
+ logger.warning("Refusing to re-run start command on an already running process.")
31
+ else
32
+ dispatch!(:start, "user initiated")
33
+ end
34
+ end
35
+
36
+ # Stop the process, and stop monitoring it
37
+ #
38
+ # @return [void]
39
+ #
40
+ # @api private
41
+ def user_stop
42
+ stop_process
43
+ dispatch!(:unmonitor, "user initiated")
44
+ end
45
+
46
+ # Restart the process
47
+ #
48
+ # @return [void]
49
+ #
50
+ # @api private
51
+ def user_restart
52
+ # Through the state machine, like a check's restart, so the process
53
+ # shows as restarting and post_start_command runs once it's up. A
54
+ # state restart can't leave, such as unmonitored, restarts it directly.
55
+ restart_process unless dispatch!(:restart, "user initiated")
56
+ end
57
+
58
+ # Stop monitoring the process
59
+ #
60
+ # @return [void]
61
+ #
62
+ # @api private
63
+ def user_unmonitor
64
+ # When the user issues an unmonitor cmd, reset any triggers so that
65
+ # scheduled events gets cleared
66
+ triggers.each(&:reset!)
67
+ dispatch!(:unmonitor, "user initiated")
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,141 @@
1
+ module Bluepill
2
+ class Process
3
+ # The checks that run on a Process every interval, such as its memory
4
+ # usage, and the events they ask it for
5
+ #
6
+ # Each watch runs on a thread of its own when it's due, and the events it
7
+ # fires, such as a restart, go back to the process to dispatch.
8
+ #
9
+ # @api private
10
+ class Watches
11
+ include Enumerable
12
+
13
+ no_events = [] #: Array[[Symbol, String]]
14
+
15
+ # What run returns when no watch is due, made once, since most ticks
16
+ # have none
17
+ NO_EVENTS = no_events.freeze
18
+
19
+ # Return the logger the watches write to
20
+ #
21
+ # @return [Logger, nil]
22
+ #
23
+ # @api private
24
+ attr_reader :logger
25
+
26
+ # Create an empty set of watches
27
+ #
28
+ # @return [void]
29
+ #
30
+ # @api private
31
+ def initialize
32
+ @watches = []
33
+ end
34
+
35
+ # Add a watch for a check, with the watches' logger
36
+ #
37
+ # @param [Symbol] name the check's name, such as :mem_usage
38
+ # @param [Hash{Symbol => Object}] options the check's options
39
+ #
40
+ # @return [Watches] the watches
41
+ #
42
+ # @api private
43
+ def add(name, options)
44
+ self << ConditionWatch.new(name, options.merge(logger: logger))
45
+ end
46
+
47
+ # Add a watch
48
+ #
49
+ # @param [ConditionWatch] watch the watch
50
+ #
51
+ # @return [Watches] the watches
52
+ #
53
+ # @api private
54
+ def <<(watch)
55
+ @watches << watch
56
+ self
57
+ end
58
+
59
+ # Yield each watch
60
+ #
61
+ # @yieldparam [ConditionWatch] watch a watch
62
+ #
63
+ # @return [Array<ConditionWatch>] the watches
64
+ #
65
+ # @api private
66
+ def each(&block)
67
+ @watches.each(&block)
68
+ end
69
+
70
+ # Set the logger, for the watches and for any added later
71
+ #
72
+ # @param [Logger] logger the logger to use
73
+ #
74
+ # @return [void]
75
+ #
76
+ # @api private
77
+ def logger=(logger)
78
+ @logger = logger
79
+ each { |watch| watch.logger = logger }
80
+ end
81
+
82
+ # Forget what the watches have read, as when the process changes state
83
+ #
84
+ # @return [void]
85
+ #
86
+ # @api private
87
+ def clear_history!
88
+ each(&:clear_history!)
89
+ end
90
+
91
+ # Run every watch that's due, and return the events they fire
92
+ #
93
+ # Each watch runs on a thread of its own, so a slow one doesn't hold up
94
+ # the others. One that isn't due would do nothing, so it gets no
95
+ # thread.
96
+ #
97
+ # @param [Integer] now the time, in epoch seconds
98
+ #
99
+ # @yieldreturn [Integer, nil] the process's pid, read on each watch's
100
+ # thread
101
+ #
102
+ # @return [Array<(Symbol, String)>] each event, with the watch that
103
+ # fired it as the reason
104
+ #
105
+ # @api private
106
+ def run(now)
107
+ threads = [] #: Array[[ConditionWatch, Thread]]
108
+ each do |watch|
109
+ threads << [watch, Thread.new { watch.run(yield, now) }] if watch.due?(now)
110
+ end
111
+ # Most ticks have no watch due, and nothing to wait for
112
+ threads.empty? ? NO_EVENTS : events(threads)
113
+ end
114
+
115
+ private
116
+
117
+ # Wait for the watches' threads, and return the events they fired
118
+ #
119
+ # Each watch that fired events is logged with them.
120
+ #
121
+ # @param [Array<(ConditionWatch, Thread)>] threads each watch that ran,
122
+ # with its thread
123
+ #
124
+ # @return [Array<(Symbol, String)>] each event, with the watch that
125
+ # fired it as the reason
126
+ #
127
+ # @api private
128
+ def events(threads)
129
+ threads.each_with_object([]) do |(watch, thread), events|
130
+ fired = thread.value #: Array[Symbol]
131
+ next if fired.empty?
132
+
133
+ logger.info "#{watch.name} dispatched: #{fired.join(",")}"
134
+ fired.each do |event|
135
+ events << [event, watch.to_s]
136
+ end
137
+ end
138
+ end
139
+ end
140
+ end
141
+ end