Class: Cumuli::Spawner::ForemanProc

Inherits:
Object
  • Object
show all
Defined in:
lib/cumuli/spawner/foreman_proc.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#envObject (readonly)

Returns the value of attribute env.



4
5
6
# File 'lib/cumuli/spawner/foreman_proc.rb', line 4

def env
  @env
end

#log_dirObject (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

#pidObject (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

#commandObject



17
18
19
# File 'lib/cumuli/spawner/foreman_proc.rb', line 17

def command
  "foreman start"
end

#ensure_log_dir_and_fileObject



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_idObject



61
62
63
# File 'lib/cumuli/spawner/foreman_proc.rb', line 61

def group_id
  PS.new.root_pid
end

#kill_childrenObject



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_signalsObject



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_fileObject



21
22
23
# File 'lib/cumuli/spawner/foreman_proc.rb', line 21

def log_file
  "#{log_dir}/#{env}.log"
end

#startObject



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

Returns:

  • (Boolean)


50
51
52
# File 'lib/cumuli/spawner/foreman_proc.rb', line 50

def started?
  !!pid
end

#stopObject



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