Sha256: 4dd66b326721f8b9d3cc91a37398154aa948fffb6671be45c0f36d4010602abf

Contents?: true

Size: 894 Bytes

Versions: 15

Compression:

Stored size: 894 Bytes

Contents

require 'fileutils'

module Sanford

  class PIDFile
    attr_reader :path

    def initialize(path)
      @path = (path || '/dev/null').to_s
    end

    def pid
      pid = File.read(@path).strip
      pid && !pid.empty? ? pid.to_i : raise('no pid in file')
    rescue StandardError => exception
      error = InvalidError.new("A PID couldn't be read from #{@path.inspect}")
      error.set_backtrace(exception.backtrace)
      raise error
    end

    def write
      FileUtils.mkdir_p(File.dirname(@path))
      File.open(@path, 'w'){ |f| f.puts ::Process.pid }
    rescue StandardError => exception
      error = InvalidError.new("Can't write pid to file #{@path.inspect}")
      error.set_backtrace(exception.backtrace)
      raise error
    end

    def remove
      FileUtils.rm_f(@path)
    end

    def to_s
      @path
    end

    InvalidError = Class.new(RuntimeError)

  end

end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
sanford-0.19.1 lib/sanford/pid_file.rb
sanford-0.19.0 lib/sanford/pid_file.rb
sanford-0.18.2 lib/sanford/pid_file.rb
sanford-0.18.1 lib/sanford/pid_file.rb
sanford-0.18.0 lib/sanford/pid_file.rb
sanford-0.17.0 lib/sanford/pid_file.rb
sanford-0.16.1 lib/sanford/pid_file.rb
sanford-0.16.0 lib/sanford/pid_file.rb
sanford-0.15.1 lib/sanford/pid_file.rb
sanford-0.15.0 lib/sanford/pid_file.rb
sanford-0.14.0 lib/sanford/pid_file.rb
sanford-0.13.0 lib/sanford/pid_file.rb
sanford-0.12.0 lib/sanford/pid_file.rb
sanford-0.11.1 lib/sanford/pid_file.rb
sanford-0.11.0 lib/sanford/pid_file.rb