Skip to content

Commit 99b600d

Browse files
committed
Add monitor command for running loops under a supervisor
1 parent 4a3646e commit 99b600d

File tree

5 files changed

+42
-5
lines changed

5 files changed

+42
-5
lines changed

README.rdoc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ This is it! To manage your loop, just run one of the following commands:
7575

7676
$ ./script/loops start
7777

78+
* To run all enabled non-disabled loops with logging mirrored to console (this method does not
79+
create any pid files and never releases control of the terminal, so it is the perfect way of running
80+
loops under the control of a supervisor like supervisord/runit/upstart/systemd):
81+
82+
$ ./script/loops monitor
83+
7884
* To run all enabled loops in background:
7985

8086
$ ./script/loops start -d
@@ -83,6 +89,10 @@ This is it! To manage your loop, just run one of the following commands:
8389

8490
$ ./script/loops start hello_world -d
8591

92+
* To run specific loop in foreground with logging mirrored to console:
93+
94+
$ ./script/loops monitor hello_world
95+
8696
* To see all possible options:
8797

8898
$ ./script/loops help

lib/loops/cli.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class CLI
1818
register_command :start
1919
register_command :stop
2020
register_command :stats
21+
register_command :monitor
2122

2223
# @return [Array<String>]
2324
# The +Array+ of (unparsed) command-line options.

lib/loops/cli/options.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,10 @@ def start_engine!
295295
start Start all loops except ones marked with disabled:true in config
296296
start loop1 [loop2] Start only loops specified
297297
stop Stop daemonized loops monitor
298+
monitor Start and monitor all enabled loops
299+
(use this with supervisord, runit, upstart or systemd)
300+
monitor loop1 [loop2] Start and monitor only loops specified
301+
(use this with supervisord, runit, upstart or systemd)
298302
stats Print loops memory statistics
299303
debug loop Debug specified loop
300304
help Show this message

lib/loops/commands/monitor_command.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Loops::Commands::MonitorCommand < Loops::Command
2+
def execute
3+
# Mirror logging to console
4+
Loops.logger.write_to_console = true
5+
6+
# Set process name
7+
$0 = "loops monitor: #{options[:args].join(' ') rescue 'all'}\0"
8+
9+
# Start loops and let the monitor process take over
10+
puts "Starting loops in monitor mode..."
11+
engine.start_loops!(options[:args])
12+
puts "Monitoring loop is finished, exiting now..."
13+
end
14+
end

lib/loops/engine.rb

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ def initialize
99
load_config
1010
end
1111

12+
#-------------------------------------------------------------------------------------------------
1213
def load_config
1314
# load and parse with erb
1415
raw_config = File.read(Loops.config_file)
@@ -25,11 +26,14 @@ def load_config
2526
Loops.logger.colorful_logs = @global_config['colorful_logs'] || @global_config['colourful_logs']
2627
end
2728

29+
#-------------------------------------------------------------------------------------------------
2830
def start_loops!(loops_to_start = [])
29-
@running_loops = []
31+
enabled_loops = []
32+
33+
# Initialize process manager
3034
@pm = Loops::ProcessManager.new(global_config, Loops.logger)
3135

32-
# Start all loops
36+
# Start all enabled loops
3337
loops_config.each do |name, loop_config|
3438
# Do not load the loop if it is disabled
3539
next if loop_config['disabled']
@@ -44,22 +48,26 @@ def start_loops!(loops_to_start = [])
4448

4549
# Start the loop
4650
start_loop(name, klass, loop_config)
47-
@running_loops << name
51+
enabled_loops << name
4852
end
4953

5054
# Do not continue if there is nothing to run
51-
if @running_loops.empty?
55+
if enabled_loops.empty?
5256
puts 'WARNING: No loops to run! Exiting...'
5357
return
5458
end
5559

56-
# Start monitoring loop
60+
# Set up signals to shut down when received an external signal to stop
5761
setup_signals
62+
63+
# Start process monitoring loop
5864
@pm.monitor_workers
5965

66+
# Done, exiting now
6067
info 'Loops are stopped now!'
6168
end
6269

70+
#-------------------------------------------------------------------------------------------------
6371
def debug_loop!(loop_name)
6472
@pm = Loops::ProcessManager.new(global_config, Loops.logger)
6573
loop_config = loops_config[loop_name] || {}

0 commit comments

Comments
 (0)