Class: RightScale::PidFile
Overview
Encapsulates an agent pid file A pid file contains three components:
- the PID of the process running the agent
- the port number that should be used to talk to the agent via the
command protocol
- the cookie used to authenticate a client talking to the agent via
the command protocol
Defined Under Namespace
Classes: AlreadyRunning
Instance Attribute Summary collapse
-
#cookie_file ⇒ Object
readonly
Returns the value of attribute cookie_file.
-
#identity ⇒ Object
readonly
Returns the value of attribute identity.
-
#pid_file ⇒ Object
readonly
Returns the value of attribute pid_file.
Instance Method Summary collapse
-
#check ⇒ Object
Check whether pid file can be created Delete any existing pid file if process is not running anymore.
-
#exists? ⇒ Boolean
Does pid file exist?.
-
#initialize(identity, pid_dir = nil) ⇒ PidFile
constructor
Initialize pid file location from agent identity and pid directory.
-
#read_pid ⇒ Object
Read pid file content Empty hash if pid file does not exist or content cannot be loaded.
-
#remove ⇒ Object
Delete pid file.
-
#set_command_options(options) ⇒ Object
Update associated command protocol port.
-
#to_s ⇒ Object
Human representation.
-
#write ⇒ Object
Write pid to pid file.
Constructor Details
#initialize(identity, pid_dir = nil) ⇒ PidFile
Initialize pid file location from agent identity and pid directory
39 40 41 42 43 44 |
# File 'lib/right_agent/pid_file.rb', line 39 def initialize(identity, pid_dir = nil) @identity = identity @pid_dir = File.normalize_path(pid_dir || AgentConfig.pid_dir) @pid_file = File.join(@pid_dir, "#{identity}.pid") @cookie_file = File.join(@pid_dir, "#{identity}.cookie") end |
Instance Attribute Details
#cookie_file ⇒ Object (readonly)
Returns the value of attribute cookie_file.
36 37 38 |
# File 'lib/right_agent/pid_file.rb', line 36 def @cookie_file end |
#identity ⇒ Object (readonly)
Returns the value of attribute identity.
36 37 38 |
# File 'lib/right_agent/pid_file.rb', line 36 def identity @identity end |
#pid_file ⇒ Object (readonly)
Returns the value of attribute pid_file.
36 37 38 |
# File 'lib/right_agent/pid_file.rb', line 36 def pid_file @pid_file end |
Instance Method Details
#check ⇒ Object
Check whether pid file can be created Delete any existing pid file if process is not running anymore
Return
- true
-
Always return true
Raise
- AlreadyRunning
-
If pid file already exists and process is running
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/right_agent/pid_file.rb', line 54 def check if pid = read_pid[:pid] if process_running?(pid) raise AlreadyRunning.new("#{@pid_file} already exists and process is running (pid: #{pid})") else Log.info("Removing stale pid file: #{@pid_file}") remove end end true end |
#exists? ⇒ Boolean
Does pid file exist?
Return
- true
-
If pid file exists
- false
-
Otherwise
132 133 134 |
# File 'lib/right_agent/pid_file.rb', line 132 def exists? File.exists?(@pid_file) end |
#read_pid ⇒ Object
Read pid file content Empty hash if pid file does not exist or content cannot be loaded
Return
- content(Hash)
-
Hash containing 3 keys :pid, :cookie and :port
115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/right_agent/pid_file.rb', line 115 def read_pid content = {} if exists? open(@pid_file,'r') { |f| content[:pid] = f.read.to_i } open(@cookie_file,'r') do |f| = (YAML.load(f.read) rescue {}) || {} content.merge!() end if File.readable?(@cookie_file) end content end |
#remove ⇒ Object
Delete pid file
Return
- true
-
Always return true
104 105 106 107 108 |
# File 'lib/right_agent/pid_file.rb', line 104 def remove File.delete(@pid_file) if exists? File.delete(@cookie_file) if File.exists?(@cookie_file) true end |
#set_command_options(options) ⇒ Object
90 91 92 93 94 95 96 97 98 |
# File 'lib/right_agent/pid_file.rb', line 90 def () content = { :listen_port => [:listen_port], :cookie => [:cookie] } # This is requried to preserve cookie value to be saved as string, # and not as escaped binary data content[:cookie].force_encoding('utf-8') open(@cookie_file,'w') { |f| f.write(YAML.dump(content)) } File.chmod(0600, @cookie_file) true end |
#to_s ⇒ Object
Human representation
Return
- path(String)
-
Path to pid file
140 141 142 |
# File 'lib/right_agent/pid_file.rb', line 140 def to_s path = @pid_file end |
#write ⇒ Object
Write pid to pid file
Return
- true
-
Always return true
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/right_agent/pid_file.rb', line 70 def write begin FileUtils.mkdir_p(@pid_dir) open(@pid_file,'w') { |f| f.write(Process.pid) } File.chmod(0644, @pid_file) rescue StandardError => e ErrorTracker.log(self, "Failed to create PID file", e, nil, :caller) raise end true end |