Class: Procodile::Process
- Inherits:
-
Object
- Object
- Procodile::Process
- Defined in:
- lib/procodile/process.rb
Constant Summary collapse
- MUTEX =
Mutex.new
Instance Attribute Summary collapse
-
#command ⇒ Object
Returns the value of attribute command.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#log_color ⇒ Object
Returns the value of attribute log_color.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
Returns the value of attribute options.
-
#removed ⇒ Object
Returns the value of attribute removed.
Instance Method Summary collapse
-
#create_instance(supervisor) ⇒ Object
Create a new instance.
-
#environment_variables ⇒ Object
Return all environment variables for this process.
-
#generate_instances(supervisor, quantity = self.quantity) ⇒ Object
Generate an array of new instances for this process (based on its quantity).
-
#get_instance_id ⇒ Object
Increase the instance index and return.
-
#initialize(config, name, command, environment, options = {}) ⇒ Process
constructor
A new instance of Process.
-
#log_path ⇒ Object
Return the path where log output for this process should be written to.
-
#max_respawns ⇒ Object
The maximum number of times this process can be respawned in the given period.
-
#proxy? ⇒ Boolean
Is this process enabled for proxying?.
-
#proxy_address ⇒ Object
Return the port for the proxy to listen on for this process type.
-
#proxy_port ⇒ Object
Return the port for the proxy to listen on for this process type.
-
#quantity ⇒ Object
How many instances of this process should be started.
-
#respawn_window ⇒ Object
The respawn window.
-
#restart_mode ⇒ Object
Defines how this process should be restarted.
-
#term_signal ⇒ Object
Return the signal to send to terminate the process.
-
#to_hash ⇒ Object
Return a hash.
Constructor Details
#initialize(config, name, command, environment, options = {}) ⇒ Process
Returns a new instance of Process.
15 16 17 18 19 20 21 22 23 |
# File 'lib/procodile/process.rb', line 15 def initialize(config, name, command, environment, = {}) @config = config @name = name @command = command @environment = environment = @log_color = 0 @instance_index = 0 end |
Instance Attribute Details
#command ⇒ Object
Returns the value of attribute command.
10 11 12 |
# File 'lib/procodile/process.rb', line 10 def command @command end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
8 9 10 |
# File 'lib/procodile/process.rb', line 8 def config @config end |
#log_color ⇒ Object
Returns the value of attribute log_color.
12 13 14 |
# File 'lib/procodile/process.rb', line 12 def log_color @log_color end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/procodile/process.rb', line 9 def name @name end |
#options ⇒ Object
Returns the value of attribute options.
11 12 13 |
# File 'lib/procodile/process.rb', line 11 def end |
#removed ⇒ Object
Returns the value of attribute removed.
13 14 15 |
# File 'lib/procodile/process.rb', line 13 def removed @removed end |
Instance Method Details
#create_instance(supervisor) ⇒ Object
Create a new instance
127 128 129 |
# File 'lib/procodile/process.rb', line 127 def create_instance(supervisor) Instance.new(supervisor, self, get_instance_id) end |
#environment_variables ⇒ Object
Return all environment variables for this process
38 39 40 41 42 43 44 45 |
# File 'lib/procodile/process.rb', line 38 def environment_variables global_variables = @config.environment_variables process_vars = @config.[@name] ? @config.[@name]['env'] || {} : {} process_local_vars = @config.[@name] ? @config.[@name]['env'] || {} : {} global_variables.merge(process_vars.merge(process_local_vars)).each_with_object({}) do |(key, value), hash| hash[key.to_s] = value.to_s end end |
#generate_instances(supervisor, quantity = self.quantity) ⇒ Object
Generate an array of new instances for this process (based on its quantity)
120 121 122 |
# File 'lib/procodile/process.rb', line 120 def generate_instances(supervisor, quantity = self.quantity) quantity.times.map { |i| create_instance(supervisor) } end |
#get_instance_id ⇒ Object
Increase the instance index and return
28 29 30 31 32 33 |
# File 'lib/procodile/process.rb', line 28 def get_instance_id MUTEX.synchronize do @instance_index = 0 if @instance_index == 10000 @instance_index += 1 end end |
#log_path ⇒ Object
Return the path where log output for this process should be written to. If none, output will be written to the supervisor log.
72 73 74 |
# File 'lib/procodile/process.rb', line 72 def log_path ['log_path'] ? File.(['log_path'], @config.root) : nil end |
#max_respawns ⇒ Object
The maximum number of times this process can be respawned in the given period
57 58 59 |
# File 'lib/procodile/process.rb', line 57 def max_respawns ['max_respawns'] ? ['max_respawns'].to_i : 5 end |
#proxy? ⇒ Boolean
Is this process enabled for proxying?
98 99 100 |
# File 'lib/procodile/process.rb', line 98 def proxy? .has_key?('proxy_port') end |
#proxy_address ⇒ Object
Return the port for the proxy to listen on for this process type
112 113 114 |
# File 'lib/procodile/process.rb', line 112 def proxy_address proxy? ? ['proxy_address'] || '127.0.0.1' : nil end |
#proxy_port ⇒ Object
Return the port for the proxy to listen on for this process type
105 106 107 |
# File 'lib/procodile/process.rb', line 105 def proxy_port proxy? ? ['proxy_port'].to_i : nil end |
#quantity ⇒ Object
How many instances of this process should be started
50 51 52 |
# File 'lib/procodile/process.rb', line 50 def quantity ['quantity'] || 1 end |
#respawn_window ⇒ Object
The respawn window. One hour by default.
64 65 66 |
# File 'lib/procodile/process.rb', line 64 def respawn_window ['respawn_window'] ? ['respawn_window'].to_i : 3600 end |
#restart_mode ⇒ Object
Defines how this process should be restarted
start-term = start new instances and send term to children usr1 = just send a usr1 signal to the current instance usr2 = just send a usr2 signal to the current instance term-start = stop the old instances, when no longer running, start a new one
91 92 93 |
# File 'lib/procodile/process.rb', line 91 def restart_mode ['restart_mode'] || 'term-start' end |
#term_signal ⇒ Object
Return the signal to send to terminate the process
79 80 81 |
# File 'lib/procodile/process.rb', line 79 def term_signal ['term_signal'] || 'TERM' end |
#to_hash ⇒ Object
Return a hash
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/procodile/process.rb', line 134 def to_hash { :name => self.name, :log_color => self.log_color, :quantity => self.quantity, :max_respawns => self.max_respawns, :respawn_window => self.respawn_window, :command => self.command, :restart_mode => self.restart_mode, :log_path => self.log_path, :removed => self.removed ? true : false, :proxy_port => proxy_port, :proxy_address => proxy_address } end |