Class: Cumuli::Spawner::ForemanProc
- Inherits:
-
Object
- Object
- Cumuli::Spawner::ForemanProc
- Defined in:
- lib/cumuli/spawner/foreman_proc.rb
Instance Attribute Summary collapse
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#log_dir ⇒ Object
readonly
Returns the value of attribute log_dir.
-
#pid ⇒ Object
readonly
Returns the value of attribute pid.
Instance Method Summary collapse
- #command ⇒ Object
- #ensure_log_dir_and_file ⇒ Object
- #group_id ⇒ Object
-
#initialize(env, log_dir) ⇒ ForemanProc
constructor
A new instance of ForemanProc.
- #kill_children ⇒ Object
- #listen_for_signals ⇒ Object
- #log_file ⇒ Object
- #start ⇒ Object
- #started? ⇒ Boolean
- #stop ⇒ Object
Constructor Details
#initialize(env, log_dir) ⇒ ForemanProc
Returns a new instance of ForemanProc.
6 7 8 9 10 |
# File 'lib/cumuli/spawner/foreman_proc.rb', line 6 def initialize(env, log_dir) @env = env @log_dir = log_dir ensure_log_dir_and_file end |
Instance Attribute Details
#env ⇒ Object (readonly)
Returns the value of attribute env.
4 5 6 |
# File 'lib/cumuli/spawner/foreman_proc.rb', line 4 def env @env end |
#log_dir ⇒ Object (readonly)
Returns the value of attribute log_dir.
4 5 6 |
# File 'lib/cumuli/spawner/foreman_proc.rb', line 4 def log_dir @log_dir end |
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
4 5 6 |
# File 'lib/cumuli/spawner/foreman_proc.rb', line 4 def pid @pid end |
Instance Method Details
#command ⇒ Object
17 18 19 |
# File 'lib/cumuli/spawner/foreman_proc.rb', line 17 def command "foreman start" end |
#ensure_log_dir_and_file ⇒ Object
12 13 14 15 |
# File 'lib/cumuli/spawner/foreman_proc.rb', line 12 def ensure_log_dir_and_file FileUtils.mkdir_p(log_dir) FileUtils.touch(log_file) end |
#group_id ⇒ Object
61 62 63 |
# File 'lib/cumuli/spawner/foreman_proc.rb', line 61 def group_id PS.new.root_pid end |
#kill_children ⇒ Object
65 66 67 |
# File 'lib/cumuli/spawner/foreman_proc.rb', line 65 def kill_children Process.kill('INT', -group_id) # kills the forked group end |
#listen_for_signals ⇒ Object
41 42 43 44 45 46 47 48 |
# File 'lib/cumuli/spawner/foreman_proc.rb', line 41 def listen_for_signals Cumuli::App::SIGNALS.each do |signal| trap(signal) do puts "#{self.class}: trapped signal #{signal} in #{Process.pid} ... stopping" stop end end end |
#log_file ⇒ Object
21 22 23 |
# File 'lib/cumuli/spawner/foreman_proc.rb', line 21 def log_file "#{log_dir}/#{env}.log" end |
#start ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/cumuli/spawner/foreman_proc.rb', line 25 def start @pid = fork do spawn( { 'HEROKU_ENV' => env, 'RAILS_ENV' => env }, command, { out: $stdout.reopen(log_file), pgroup: true, # start a new process group } ) end end |
#started? ⇒ Boolean
50 51 52 |
# File 'lib/cumuli/spawner/foreman_proc.rb', line 50 def started? !!pid end |
#stop ⇒ Object
54 55 56 57 58 59 |
# File 'lib/cumuli/spawner/foreman_proc.rb', line 54 def stop return if @killed_it kill_children @killed_it = true @pid = nil end |