Class: Procodile::Process

Inherits:
Object
  • Object
show all
Defined in:
lib/procodile/process.rb

Constant Summary collapse

MUTEX =
Mutex.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, name, command, options = {}) ⇒ Process

Returns a new instance of Process.



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

def initialize(config, name, command, options = {})
  @config = config
  @name = name
  @command = command
  @options = options
  @log_color = 0
  @instance_index = 0
end

Instance Attribute Details

#commandObject

Returns the value of attribute command.



10
11
12
# File 'lib/procodile/process.rb', line 10

def command
  @command
end

#configObject (readonly)

Returns the value of attribute config.



8
9
10
# File 'lib/procodile/process.rb', line 8

def config
  @config
end

#log_colorObject

Returns the value of attribute log_color.



12
13
14
# File 'lib/procodile/process.rb', line 12

def log_color
  @log_color
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/procodile/process.rb', line 9

def name
  @name
end

#optionsObject

Returns the value of attribute options.



11
12
13
# File 'lib/procodile/process.rb', line 11

def options
  @options
end

#removedObject

Returns the value of attribute removed.



13
14
15
# File 'lib/procodile/process.rb', line 13

def removed
  @removed
end

Instance Method Details

#allocate_port_fromObject

Return the first port that ports should be allocated from for this process



116
117
118
# File 'lib/procodile/process.rb', line 116

def allocate_port_from
  @options['allocate_port_from']
end

#correct_quantity?(quantity) ⇒ Boolean

Is the given quantity suitable for this process?

Returns:

  • (Boolean)


184
185
186
187
188
189
190
# File 'lib/procodile/process.rb', line 184

def correct_quantity?(quantity)
  if self.restart_mode == 'start-term'
    quantity >= self.quantity
  else
    self.quantity == quantity
  end
end

#create_instance(supervisor) ⇒ Object

Create a new instance



158
159
160
# File 'lib/procodile/process.rb', line 158

def create_instance(supervisor)
  Instance.new(supervisor, self, get_instance_id)
end

#default_log_file_nameObject

Return the defualt log file name



78
79
80
# File 'lib/procodile/process.rb', line 78

def default_log_file_name
  @options['log_file_name'] || "#{@name}.log"
end

#default_log_pathObject

Return the log path for this process if no log path is provided and split logs is enabled



86
87
88
89
90
91
92
# File 'lib/procodile/process.rb', line 86

def default_log_path
  if @config.log_root
    File.join(@config.log_root, default_log_file_name)
  else
    nil
  end
end

#environment_variablesObject

Return all environment variables for this process



37
38
39
40
41
42
43
44
# File 'lib/procodile/process.rb', line 37

def environment_variables
  global_variables = @config.environment_variables
  process_vars = @config.process_options[@name] ? @config.process_options[@name]['env'] || {} : {}
  process_local_vars = @config.local_process_options[@name] ? @config.local_process_options[@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)



151
152
153
# File 'lib/procodile/process.rb', line 151

def generate_instances(supervisor, quantity = self.quantity)
  quantity.times.map { |i| create_instance(supervisor) }
end

#get_instance_idObject

Increase the instance index and return



27
28
29
30
31
32
# File 'lib/procodile/process.rb', line 27

def get_instance_id
  MUTEX.synchronize do
    @instance_index = 0 if @instance_index == 10000
    @instance_index += 1
  end
end

#log_pathObject

Return the path where log output for this process should be written to. If none, output will be written to the supervisor log.



71
72
73
# File 'lib/procodile/process.rb', line 71

def log_path
  @options['log_path'] ? File.expand_path(@options['log_path'], @config.root) : default_log_path
end

#max_respawnsObject

The maximum number of times this process can be respawned in the given period



56
57
58
# File 'lib/procodile/process.rb', line 56

def max_respawns
  @options['max_respawns'] ? @options['max_respawns'].to_i : 5
end

#network_protocolObject

Return the network protocol for this process



144
145
146
# File 'lib/procodile/process.rb', line 144

def network_protocol
  @options['network_protocol'] || 'tcp'
end

#proxy?Boolean

Is this process enabled for proxying?

Returns:

  • (Boolean)


123
124
125
# File 'lib/procodile/process.rb', line 123

def proxy?
  @options.has_key?('proxy_port')
end

#proxy_addressObject

Return the port for the proxy to listen on for this process type



137
138
139
# File 'lib/procodile/process.rb', line 137

def proxy_address
  proxy? ? @options['proxy_address'] || '127.0.0.1' : nil
end

#proxy_portObject

Return the port for the proxy to listen on for this process type



130
131
132
# File 'lib/procodile/process.rb', line 130

def proxy_port
  proxy? ? @options['proxy_port'].to_i : nil
end

#quantityObject

How many instances of this process should be started



49
50
51
# File 'lib/procodile/process.rb', line 49

def quantity
  @options['quantity'] || 1
end

#respawn_windowObject

The respawn window. One hour by default.



63
64
65
# File 'lib/procodile/process.rb', line 63

def respawn_window
  @options['respawn_window'] ? @options['respawn_window'].to_i : 3600
end

#restart_modeObject

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



109
110
111
# File 'lib/procodile/process.rb', line 109

def restart_mode
  @options['restart_mode'] || 'term-start'
end

#term_signalObject

Return the signal to send to terminate the process



97
98
99
# File 'lib/procodile/process.rb', line 97

def term_signal
  @options['term_signal'] || 'TERM'
end

#to_hashObject

Return a hash



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/procodile/process.rb', line 165

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