Sha256: ec29cfe7e8bcbf4cf79d22f30fb82e697d2b50e986da7c61a950a55b4f8e27be

Contents?: true

Size: 1.08 KB

Versions: 3

Compression:

Stored size: 1.08 KB

Contents

module Cumuli
  class App
    class ForemanProcess
      attr_reader :pid, :env, :log_dir

      def initialize(env, log_dir)
        @env = env
        @log_dir = log_dir
        ensure_log_dir_and_file
      end

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

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

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

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

      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

      def started?
        !!pid
      end

      def stop
        return if @killed_it
        Process.kill('INT', 0) # kills all processes in the group
        @killed_it = true
        @pid = nil
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cumuli-0.3.0 lib/cumuli/app/foreman_process.rb
cumuli-0.2.1 lib/cumuli/app/foreman_process.rb
cumuli-0.2.0 lib/cumuli/app/foreman_process.rb