Class: Pigeon::Pidfile

Inherits:
Object
  • Object
show all
Defined in:
lib/pigeon/pidfile.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Pidfile

Instance Methods =====================================================



12
13
14
15
16
# File 'lib/pigeon/pidfile.rb', line 12

def initialize(path)
  @path = path
  
  @path += '.pid' unless (@path.match(/\./))
end

Instance Attribute Details

#pathObject (readonly)

Properties ===========================================================



6
7
8
# File 'lib/pigeon/pidfile.rb', line 6

def path
  @path
end

Instance Method Details

#create!(pid = nil) ⇒ Object



36
37
38
39
40
# File 'lib/pigeon/pidfile.rb', line 36

def create!(pid = nil)
  open(@path, 'w') do |fh|
    fh.puts(pid || $$)
  end
end

#exist?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/pigeon/pidfile.rb', line 48

def exist?
  File.exist?(@path)
end

#remove!Object



42
43
44
45
46
# File 'lib/pigeon/pidfile.rb', line 42

def remove!
  return unless (exist?)

  File.unlink(@path)
end

#runningObject



22
23
24
25
26
27
28
# File 'lib/pigeon/pidfile.rb', line 22

def running
  pid = self.saved_pid

  (pid and Process.kill(0, pid)) ? pid : nil
rescue Errno::ESRCH
  nil
end

#running?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/pigeon/pidfile.rb', line 18

def running?
  !!self.running
end

#saved_pidObject



30
31
32
33
34
# File 'lib/pigeon/pidfile.rb', line 30

def saved_pid
  File.read(@path).to_i
rescue Errno::ENOENT
  nil
end