Class: RightScale::PidFile

Inherits:
Object show all
Defined in:
lib/right_agent/pid_file.rb

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

Instance Method Summary collapse

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

Returns the value of attribute cookie_file.



36
37
38
# File 'lib/right_agent/pid_file.rb', line 36

def cookie_file
  @cookie_file
end

#identityObject (readonly)

Returns the value of attribute identity.



36
37
38
# File 'lib/right_agent/pid_file.rb', line 36

def identity
  @identity
end

#pid_fileObject (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

#checkObject

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

Returns:

  • (Boolean)


129
130
131
# File 'lib/right_agent/pid_file.rb', line 129

def exists?
  File.exists?(@pid_file)
end

#read_pidObject

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



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/right_agent/pid_file.rb', line 112

def read_pid
  content = {}
  if exists?
    open(@pid_file,'r') { |f| content[:pid] = f.read.to_i }
    open(@cookie_file,'r') do |f|
      command_options = YAML.load(f.read) rescue {}
      content.merge!(command_options)
    end if File.exists?(@cookie_file)
  end
  content
end

#removeObject

Delete pid file

Return

true

Always return true



101
102
103
104
105
# File 'lib/right_agent/pid_file.rb', line 101

def remove
  File.delete(@pid_file) if exists?
  File.delete(@cookie_file) if File.exists?(@cookie_file)
  true
end

#set_command_options(options) ⇒ Object

Update associated command protocol port

Parameters

options(Integer)

Command protocol port to be used for this agent

options(String)

Cookie to be used together with command protocol

Return

true

Always return true



90
91
92
93
94
95
# File 'lib/right_agent/pid_file.rb', line 90

def set_command_options(options)
  content = { :listen_port => options[:listen_port], :cookie => options[:cookie] }
  open(@cookie_file,'w') { |f| f.write(YAML.dump(content)) }
  File.chmod(0600, @cookie_file)
  true
end

#to_sObject

Human representation

Return

path(String)

Path to pid file



137
138
139
# File 'lib/right_agent/pid_file.rb', line 137

def to_s
  path = @pid_file
end

#writeObject

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 Exception => e
    Log.error "Failed to create PID file: #{e.message}"
    raise
  end
  true
end