Navigation Menu

Skip to content

Commit

Permalink
deleting pid files works
Browse files Browse the repository at this point in the history
  • Loading branch information
TwP committed May 30, 2015
1 parent 1f38485 commit 6e5506b
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 37 deletions.
54 changes: 44 additions & 10 deletions lib/servolux/pid_file.rb
Expand Up @@ -4,36 +4,70 @@ class Servolux::PidFile

attr_accessor :name
attr_accessor :mode
attr_accessor :path

#
# opts - The options Hash
# :name - the name of the PID file
# :pid - the numeric process ID
# :name - the name of the program
# :path - path to the PID file location
# :mode - file permissions mode
# :pid - the numeric process ID
#
def initialize( opts = {} )
@name = opts.fetch(:name, nil)
@pid = opts.fetch(:pid, nil)
@name = opts.fetch(:name, $0)
@path = opts.fetch(:path, nil)
@mode = opts.fetch(:mode, DEFAULT_MODE)
@pid = opts.fetch(:pid, nil)

yield self if block_given?
end

#
#
def write( pid: Process.pid )
def filename
fn = "#{name}.pid"
fn = File.join(path, fn) unless path.nil?
fn
end

#
#
def write( pid = Process.pid )
@pid ||= pid
File.open(name, 'w', mode) { |fd| fd.write(@pid.to_s) }
File.open(filename, 'w', mode) { |fd| fd.write(@pid.to_s) }
end

#
#
def delete
return unless exist?
return unless read_pid == Process.pid
File.delete filename
end

#
#
def delete!
return unless exist?
File.delete filename
end

#
#
def exist?
File.exist? filename
end

# Returns the numeric PID read from the file or `nil` if the file does not
# exist.
def pid
return @pid unless @pid.nil?
@pid =
if name && File.exists?(name)
Integer(File.read(pid_file).strip)
end
read_pid
end

#
#
def read_pid
Integer(File.read(filename).strip) if exist?
end
end
89 changes: 62 additions & 27 deletions spec/pid_file_spec.rb
Expand Up @@ -3,10 +3,7 @@
describe Servolux::PidFile do
before :all do
tmp = Tempfile.new "servolux-pid-file"
@path = tmp.path
tmp.unlink

@filename = "#@path/servolux-test.pid"
@path = tmp.path; tmp.unlink
FileUtils.mkdir @path
end

Expand All @@ -16,43 +13,81 @@

before :each do
FileUtils.rm_f Dir.glob("#@path/*.pid")
@pid_file = Servolux::PidFile.new(:name => @filename)
@pid_file = Servolux::PidFile.new(:name => "test", :path => @path)
@filename = @pid_file.filename
end

it "creates a PID file" do
expect(test(?e, @filename)).to be false
describe "creating" do
it "writes a PID file" do
expect(test(?e, @filename)).to be false

@pid_file.write(pid: 123456)
expect(test(?e, @filename)).to be true
@pid_file.write(123456)
expect(test(?e, @filename)).to be true

pid = Integer(File.read(@filename).strip)
expect(pid).to eq(123456)
end
pid = Integer(File.read(@filename).strip)
expect(pid).to eq(123456)
end

it "uses mode rw-r----- by default" do
expect(test(?e, @filename)).to be false

it "generates a PID file with mode rw-r----- by default" do
expect(test(?e, @filename)).to be false
@pid_file.write
expect(test(?e, @filename)).to be true

@pid_file.write
expect(test(?e, @filename)).to be true
mode = File.stat(@filename).mode & 0777
expect(mode).to eq(0640)
end

mode = File.stat(@filename).mode & 0777
expect(mode).to eq(0640)
it "uses the given mode" do
@pid_file.mode = 0400
expect(test(?e, @filename)).to be false

@pid_file.write
expect(test(?e, @filename)).to be true

mode = File.stat(@filename).mode & 0777
expect(mode).to eq(0400)
end
end

it "generates PID file with the specified permissions" do
@pid_file.mode = 0400
expect(test(?e, @filename)).to be false
describe "deleting" do
it "removes a PID file" do
expect(test(?e, @filename)).to be false
expect { @pid_file.delete }.not_to raise_error

@pid_file.write
expect(test(?e, @filename)).to be true

@pid_file.write
expect(test(?e, @filename)).to be true
@pid_file.delete
expect(test(?e, @filename)).to be false
end

mode = File.stat(@filename).mode & 0777
expect(mode).to eq(0400)
it "removes the PID file only from the same process" do
@pid_file.write(654321)
expect(test(?e, @filename)).to be true

@pid_file.delete
expect(test(?e, @filename)).to be true
end

it "can forcibly remove a PID file" do
@pid_file.write(135790)
expect(test(?e, @filename)).to be true

@pid_file.delete!
expect(test(?e, @filename)).to be false
end
end

it "removes a PID file"
it "returns the PID from the file" do
expect(@pid_file.pid).to be_nil

it "returns the PID from the file"
File.open(@filename, 'w') { |fd| fd.write(314159) }
expect(@pid_file.pid).to eq(314159)

File.delete(@filename)
expect(@pid_file.pid).to be_nil
end

it "sends a signal to the process"

Expand Down

0 comments on commit 6e5506b

Please sign in to comment.