Sha256: c50366bccd0a600197a27d3ab1db6dbfce6a9dab0fdc50677e48e74850742d20

Contents?: true

Size: 1.14 KB

Versions: 25

Compression:

Stored size: 1.14 KB

Contents

# -*- encoding: utf-8 -*-

module Lolcommits
  module CLI
    # Helper class for forking lolcommits process to the background (or not).
    class ProcessRunner
      # Initializes a new process runner.
      #
      # @param config [Lolcommits::Configuration]
      def initialize(config)
        @configuration = config
      end

      # Forks the lolcommits process if requested.
      #
      # Writes the PID of the lolcommits process to the filesystem when
      # backgrounded, for monitoring purposes.
      #
      # @param please [Boolean] whether or not to fork lolcommits process
      # @yield the main event loop for lolcommits
      def fork_me?(please, &block)
        if please
          $stdout.sync = true
          write_pid fork {
            yield block
            delete_pid
          }
        else
          yield block
        end
      end

      private

      def write_pid(pid)
        File.open(pid_file, 'w') { |f| f.write(pid) }
      end

      def delete_pid
        File.delete(pid_file) if File.exist?(pid_file)
      end

      def pid_file
        File.join(@configuration.loldir, 'lolcommits.pid')
      end
    end
  end
end

Version data entries

25 entries across 25 versions & 2 rubygems

Version Path
lolcommits-0.9.2 lib/lolcommits/cli/process_runner.rb
lolcommits-0.9.2.pre1 lib/lolcommits/cli/process_runner.rb
lolcommits-0.9.1 lib/lolcommits/cli/process_runner.rb
lolcommits-0.9.1.pre1 lib/lolcommits/cli/process_runner.rb
lolcommits-0.9.0 lib/lolcommits/cli/process_runner.rb
lolcommits-0.8.1 lib/lolcommits/cli/process_runner.rb
lolcommits-0.8.0 lib/lolcommits/cli/process_runner.rb
lolcommits-0.7.0 lib/lolcommits/cli/process_runner.rb
lolcommits-0.6.7 lib/lolcommits/cli/process_runner.rb
lolcommits-0.6.6 lib/lolcommits/cli/process_runner.rb
lolcommits-0.6.6.pre1 lib/lolcommits/cli/process_runner.rb
snapgit-0.6.5 lib/lolcommits/cli/process_runner.rb
lolcommits-0.6.5 lib/lolcommits/cli/process_runner.rb
snapgit-0.6.4 lib/lolcommits/cli/process_runner.rb
lolcommits-0.6.4 lib/lolcommits/cli/process_runner.rb
lolcommits-0.6.3 lib/lolcommits/cli/process_runner.rb
lolcommits-0.6.3.pre1 lib/lolcommits/cli/process_runner.rb
lolcommits-0.6.2 lib/lolcommits/cli/process_runner.rb
lolcommits-0.6.2.pre1 lib/lolcommits/cli/process_runner.rb
lolcommits-0.6.1 lib/lolcommits/cli/process_runner.rb