Class: Cumuli::App::ForemanProcess

Inherits:
Object
  • Object
show all
Defined in:
lib/cumuli/app/foreman_process.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, log_dir) ⇒ ForemanProcess

Returns a new instance of ForemanProcess.



6
7
8
9
10
# File 'lib/cumuli/app/foreman_process.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/app/foreman_process.rb', line 4

def env
  @env
end

#log_dirObject (readonly)

Returns the value of attribute log_dir.



4
5
6
# File 'lib/cumuli/app/foreman_process.rb', line 4

def log_dir
  @log_dir
end

#pidObject (readonly)

Returns the value of attribute pid.



4
5
6
# File 'lib/cumuli/app/foreman_process.rb', line 4

def pid
  @pid
end

Instance Method Details

#commandObject



17
18
19
# File 'lib/cumuli/app/foreman_process.rb', line 17

def command
  "HEROKU_ENV=#{env} RAILS_ENV=#{env} foreman start"
end

#ensure_log_dir_and_fileObject



12
13
14
15
# File 'lib/cumuli/app/foreman_process.rb', line 12

def ensure_log_dir_and_file
  FileUtils.mkdir_p(log_dir)
  FileUtils.touch(log_file)
end

#kill_childrenObject



53
54
55
56
57
58
# File 'lib/cumuli/app/foreman_process.rb', line 53

def kill_children
  pids = PS.new.tree(pid)
  pids.reverse.each do |p|
    Process.kill("KILL", p)
  end
end

#listen_for_signalsObject



33
34
35
36
37
38
39
40
# File 'lib/cumuli/app/foreman_process.rb', line 33

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/app/foreman_process.rb', line 21

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

#startObject



25
26
27
28
29
30
31
# File 'lib/cumuli/app/foreman_process.rb', line 25

def start
  @pid = fork do
    listen_for_signals
    $stdout.reopen(log_file)
    exec(command)
  end
end

#started?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/cumuli/app/foreman_process.rb', line 42

def started?
  !!pid
end

#stopObject



46
47
48
49
50
51
# File 'lib/cumuli/app/foreman_process.rb', line 46

def stop
  return if @killed_it
  kill_children
  @killed_it = true
  @pid = nil
end