Sha256: 342edf1204675e66af4b1dbec6eaf087fc9eee6de5a68eccaebdf605ede671cb

Contents?: true

Size: 1.12 KB

Versions: 4

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

require 'fileutils'

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.write(pid_file, pid)
      end

      def delete_pid
        FileUtils.rm_f(pid_file)
      end

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
lolcommits-0.17.2 lib/lolcommits/cli/process_runner.rb
lolcommits-0.17.1 lib/lolcommits/cli/process_runner.rb
lolcommits-0.17.0 lib/lolcommits/cli/process_runner.rb
lolcommits-0.16.5 lib/lolcommits/cli/process_runner.rb