Module: Bender::Process

Defined in:
lib/process.rb

Overview

Helper methods for working with the Bender process

Author

Michael Heijmans ([email protected])

Copyright

Copyright © 2013-2015 Michael Heijmans

License

MIT

Class Method Summary collapse

Class Method Details

.bender_pidObject

Returns the Bender pid


25
26
27
# File 'lib/process.rb', line 25

def self.bender_pid
  return $$
end

.pid_running?(pid) ⇒ Boolean

Note:

unlike Process#getpgid, this will return false if the pid is not in the procsess table

Returns true if the given `pid` appears to be running

Returns:

  • (Boolean)

14
15
16
17
18
19
20
21
22
# File 'lib/process.rb', line 14

def self.pid_running?(pid)
  pid = pid.to_i
  begin
    ::Process.getpgid( pid )
    return true
  rescue Errno::ESRCH
    return false
  end
end

.remove_pid(pid_file = nil) ⇒ Object

removes the pid file


58
59
60
61
62
63
64
# File 'lib/process.rb', line 58

def self.remove_pid(pid_file=nil)
  file = pid_file || Bender.config.pid_file
  return false unless File.exist?(file)
  File.delete(file)
  Bender.log.debug "removing pid file #{file}"
  return true
end

.set_process_nameObject

Note:

bender will appear as 'bender' when you run `ps`

sets the running process name in the process table


68
69
70
# File 'lib/process.rb', line 68

def self.set_process_name
  $0 = "bender"
end

.write_pid(pid_file = nil) ⇒ Object

Note:

handles a stale pid file and cleans it up

Writes the pid file for Bender or raises an exception if Bender is already running


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/process.rb', line 31

def self.write_pid(pid_file=nil)
  #write a pid file
  file = pid_file || Bender.config.pid_file
  #check for ophaned pid file and handle it
  if File.exist?(file)
    fh = File.new(file, "r")
    pid = fh.readlines[0]
    if pid_running?(pid)
      Bender.log.fatal("Bender is already running as pid:#{pid}")
      raise "Bender is already running as pid:#{pid}"
    else
      Bender.log.warn("found orphaned pid file... removing")
      puts "found orphaned pid file... removing"
      fh.close
      File.delete(file)
    end
  end
  fh = File.new(file, "w")
  mypid = $$
  Bender.log.debug "Writing pid file #{file} pid:#{mypid}"
  fh.print(mypid)
  fh.flush
  fh.close
  return true
end