Class: ProcMan::Process

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Process

Initializes the new process by receiving it’s name



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

def initialize(name)
  @name = name
  @constraints = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, &block) ⇒ Object

Stores and calls different process actions for this process. If there is no block provided and we don’t have a method it will return false otherwise it will store or call the action as appropriate.



69
70
71
72
73
74
75
76
77
# File 'lib/proc_man/process.rb', line 69

def method_missing(method, &block)
  if block_given?
    instance_variable_set("@#{method}", block)
  elsif block = instance_variable_get("@#{method}")
    block.call
  else
    return false
  end
end

Instance Attribute Details

#optionsObject

Returns a hash of options which the user has specified



18
19
20
# File 'lib/proc_man/process.rb', line 18

def options
  @options ||= {}
end

Instance Method Details

#constraint(hash = {}) ⇒ Object

Sets a constraint for this process



44
45
46
# File 'lib/proc_man/process.rb', line 44

def constraint(hash = {})
  @constraints << Constraint.new(self, hash)
end

#defined_method?(name) ⇒ Boolean

Specifies whether or not the provided method has been defined in the Procfile for this process.

Returns:

  • (Boolean)


62
63
64
# File 'lib/proc_man/process.rb', line 62

def defined_method?(name)
  instance_variable_get("@#{name}").is_a?(Proc)
end

#environmentObject

Returns the current environment



23
24
25
# File 'lib/proc_man/process.rb', line 23

def environment
  self.options[:environment] || self.options[:e] || self.options[:env] || 'development'
end

#execute?Boolean

Specifies whether or not actions for this process should be executed in the current context.

Returns:

  • (Boolean)


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

def execute?
  (@constraints.empty? || @constraints.any?(&:matches?)) &&
  (self.processes_to_execute.nil? || self.processes_to_execute.split(',').include?(self.name.to_s))
end

#hostObject

Returns the current hostname of the machine executing this action



33
34
35
# File 'lib/proc_man/process.rb', line 33

def host
  @host ||= `hostname -f`.strip
end

#manualObject

Returns whether this process is explicitly listed



38
39
40
41
# File 'lib/proc_man/process.rb', line 38

def manual
  return @manual unless @manual.nil?
  @manual = self.options[:processes] && self.options[:processes].split(',').include?(self.name.to_s)
end

#nameObject

Returns the name of the process



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

def name
  @name
end

#processes_to_executeObject

Return all processes which should execute



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

def processes_to_execute
  (self.options[:processes] || self.options[:p])
end

#rbg(options = {}) ⇒ Object

A shortcut method for defining a set of RBG processes



85
86
87
88
89
90
# File 'lib/proc_man/process.rb', line 85

def rbg(options = {})
  options[:config_file] ||= "Rbgfile"
  start     { run("rbg start -c #{root}/#{options[:config_file]} -E #{environment}") }
  stop      { run("rbg stop -c #{root}/#{options[:config_file]} -E #{environment}") }
  restart   { run("rbg restart -c #{root}/#{options[:config_file]} -E #{environment}") }
end

#rootObject

Returns the current root directory path



28
29
30
# File 'lib/proc_man/process.rb', line 28

def root
  @root ||= self.options[:root] || self.options[:r] || File.expand_path('./')
end

#run(command) ⇒ Object



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

def run(command)
  puts "      \e[36m#{command}\e[0m"
  system(command)
end

#unicorn(options = {}) ⇒ Object

A shortcut method for defining a unicorn-like process



93
94
95
96
97
98
99
100
# File 'lib/proc_man/process.rb', line 93

def unicorn(options = {})
  options[:name]            ||= 'unicorn'
  options[:config_file]     ||= "config/#{options[:name]}.rb"
  options[:pid_path]        ||= "log/#{options[:name]}.pid"
  start     { run("bundle exec #{options[:name]} -D -E #{environment} -c #{root}/#{options[:config_file]}") }
  stop      { run("kill `cat #{root}/#{options[:pid_path]}`") if File.exist?(options[:pid_path]) }
  restart   { run("kill -USR2 `cat #{root}/#{options[:pid_path]}`") if File.exist?(options[:pid_path]) }
end