Class: ProcMan::Process
- Inherits:
-
Object
- Object
- ProcMan::Process
- Defined in:
- lib/proc_man/process.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns a hash of options which the user has specified.
Instance Method Summary collapse
-
#constraint(hash = {}) ⇒ Object
Sets a constraint for this process.
-
#defined_method?(name) ⇒ Boolean
Specifies whether or not the provided method has been defined in the Procfile for this process.
-
#environment ⇒ Object
Returns the current environment.
-
#execute? ⇒ Boolean
Specifies whether or not actions for this process should be executed in the current context.
-
#host ⇒ Object
Returns the current hostname of the machine executing this action.
-
#initialize(name) ⇒ Process
constructor
Initializes the new process by receiving it’s name.
-
#manual ⇒ Object
Returns whether this process is explicitly listed.
-
#method_missing(method, &block) ⇒ Object
Stores and calls different process actions for this process.
-
#name ⇒ Object
Returns the name of the process.
-
#processes_to_execute ⇒ Object
Return all processes which should execute.
-
#rbg(options = {}) ⇒ Object
A shortcut method for defining a set of RBG processes.
-
#root ⇒ Object
Returns the current root directory path.
- #run(command) ⇒ Object
-
#unicorn(options = {}) ⇒ Object
A shortcut method for defining a unicorn-like process.
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
#options ⇒ Object
Returns a hash of options which the user has specified
18 19 20 |
# File 'lib/proc_man/process.rb', line 18 def @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.
62 63 64 |
# File 'lib/proc_man/process.rb', line 62 def defined_method?(name) instance_variable_get("@#{name}").is_a?(Proc) end |
#environment ⇒ Object
Returns the current environment
23 24 25 |
# File 'lib/proc_man/process.rb', line 23 def environment self.[:environment] || self.[:e] || self.[:env] || 'development' end |
#execute? ⇒ Boolean
Specifies whether or not actions for this process should be executed in the current context.
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 |
#host ⇒ Object
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 |
#manual ⇒ Object
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.[:processes] && self.[:processes].split(',').include?(self.name.to_s) end |
#name ⇒ Object
Returns the name of the process
13 14 15 |
# File 'lib/proc_man/process.rb', line 13 def name @name end |
#processes_to_execute ⇒ Object
Return all processes which should execute
49 50 51 |
# File 'lib/proc_man/process.rb', line 49 def processes_to_execute (self.[:processes] || self.[: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( = {}) [:config_file] ||= "Rbgfile" start { run("rbg start -c #{root}/#{[:config_file]} -E #{environment}") } stop { run("rbg stop -c #{root}/#{[:config_file]} -E #{environment}") } restart { run("rbg restart -c #{root}/#{[:config_file]} -E #{environment}") } end |
#root ⇒ Object
Returns the current root directory path
28 29 30 |
# File 'lib/proc_man/process.rb', line 28 def root @root ||= self.[:root] || self.[:r] || File.('./') 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( = {}) [:name] ||= 'unicorn' [:config_file] ||= "config/#{[:name]}.rb" [:pid_path] ||= "log/#{[:name]}.pid" start { run("bundle exec #{[:name]} -D -E #{environment} -c #{root}/#{[:config_file]}") } stop { run("kill `cat #{root}/#{[:pid_path]}`") if File.exist?([:pid_path]) } restart { run("kill -USR2 `cat #{root}/#{[:pid_path]}`") if File.exist?([:pid_path]) } end |