Class: EventHub::Components::Pidfile

Inherits:
Object
  • Object
show all
Defined in:
lib/eventhub/components/pid_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_name) ⇒ Pidfile

Returns a new instance of Pidfile.



3
4
5
# File 'lib/eventhub/components/pid_file.rb', line 3

def initialize(file_name)
  @file_name = file_name
end

Instance Attribute Details

#file_nameObject (readonly)

Returns the value of attribute file_name.



2
3
4
# File 'lib/eventhub/components/pid_file.rb', line 2

def file_name
  @file_name
end

Instance Method Details

#deleteObject

Try to delete file, ignore all errors



15
16
17
18
19
# File 'lib/eventhub/components/pid_file.rb', line 15

def delete
  File.delete(file_name)
rescue
  # ignore
end

#readObject

Read the PID from the file



22
23
24
25
26
# File 'lib/eventhub/components/pid_file.rb', line 22

def read
  File.read(file_name)
rescue Errno::ENOENT
  # ignore, will return nil
end

#write(pid = Process.pid) ⇒ Object

write the pid to the file specified in the initializer defaults to Process.pid



9
10
11
12
# File 'lib/eventhub/components/pid_file.rb', line 9

def write(pid = Process.pid)
  FileUtils.makedirs(File.dirname(file_name))
  IO.write(file_name, pid.to_s)
end