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, 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, options = {})
  @config = config
  @name = name
  @command = command
  @environment = environment
  @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

#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_variablesObject

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.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)



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_idObject

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_pathObject

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
  @options['log_path'] ? File.expand_path(@options['log_path'], @config.root) : nil
end

#max_respawnsObject

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
  @options['max_respawns'] ? @options['max_respawns'].to_i : 5
end

#proxy?Boolean

Is this process enabled for proxying?

Returns:

  • (Boolean)


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

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

#proxy_addressObject

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? ? @options['proxy_address'] || '127.0.0.1' : nil
end

#proxy_portObject

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? ? @options['proxy_port'].to_i : nil
end

#quantityObject

How many instances of this process should be started



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

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

#respawn_windowObject

The respawn window. One hour by default.



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

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



91
92
93
# File 'lib/procodile/process.rb', line 91

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

#term_signalObject

 Return the signal to send to terminate the process



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

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

#to_hashObject

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