Class: IptablesWeb::Cli::PidFile

Inherits:
Object
  • Object
show all
Defined in:
lib/iptables_web/cli/pid_file.rb

Defined Under Namespace

Classes: AlreadyLaunched, AnotherLaunched, PidFileException

Instance Method Summary collapse

Constructor Details

#initialize(pidfile_path) ⇒ PidFile

Returns a new instance of PidFile.



5
6
7
# File 'lib/iptables_web/cli/pid_file.rb', line 5

def initialize(pidfile_path)
  @pidfile = pidfile_path
end

Instance Method Details

#another_exist?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/iptables_web/cli/pid_file.rb', line 33

def another_exist?
  process_exist? && other?
end

#createObject

Raises:



9
10
11
12
13
14
15
16
17
# File 'lib/iptables_web/cli/pid_file.rb', line 9

def create
  raise AnotherLaunched.new("Another process with #{pid} already launched!") if another_exist?
  logged_say("Create pidfile #{self} for pid #{Process.pid}")
  logged_say("Grab pidfile #{self} for pid #{Process.pid} due process #{pid} is down.") if other?
  File.open(@pidfile, 'w') do |file|
    file.write(Process.pid)
  end
  pid
end

#deleteObject

Raises:



19
20
21
22
23
# File 'lib/iptables_web/cli/pid_file.rb', line 19

def delete
  raise AnotherLaunched.new("Delete error. Another process with #{pid} already launched!") if another_exist?
  logged_say("Delete pidfile #{self} for pid #{pid}")
  File.unlink(@pidfile) if exist?
end

#exist?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/iptables_web/cli/pid_file.rb', line 47

def exist?
  ::File.exists?(@pidfile)
end

#other?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/iptables_web/cli/pid_file.rb', line 37

def other?
  pid > 0 && Process.pid != pid
end

#pidObject



25
26
27
28
29
30
31
# File 'lib/iptables_web/cli/pid_file.rb', line 25

def pid
  if exist?
    File.read(@pidfile).to_i
  else
    0
  end
end

#process_exist?Boolean

Returns:

  • (Boolean)


41
42
43
44
45
# File 'lib/iptables_web/cli/pid_file.rb', line 41

def process_exist?
  pid > 0 && Process.kill(0, pid)
rescue Errno::ESRCH
  false
end

#to_sObject



51
52
53
# File 'lib/iptables_web/cli/pid_file.rb', line 51

def to_s
  @pidfile
end