Class: RedUnicorn::Unicorn
- Inherits:
-
Object
- Object
- RedUnicorn::Unicorn
- Defined in:
- lib/red_unicorn/unicorn.rb
Instance Method Summary collapse
-
#add_child ⇒ Object
Add new worker process.
-
#halt ⇒ Object
Halts the current unicorn process.
-
#initialize(opts = {}) ⇒ Unicorn
constructor
- pid
- Path to unicorn PID file return_codes
- Hash of return codes (defined in executable bin file) exec_path
-
Path to unicorn executable Create a new instance of unicorn interactor.
-
#reload ⇒ Object
Reload unicorn configuration.
-
#remove_all_children ⇒ Object
Stops all worker processes but keeps the master process alive.
-
#remove_child ⇒ Object
Remove worker process.
-
#reopen_logs ⇒ Object
Reopen log files.
-
#restart ⇒ Object
Graceful restart.
-
#start ⇒ Object
Start a new unicorn process.
-
#status ⇒ Object
Return status of current process.
-
#stop ⇒ Object
Stop current unicorn process.
Constructor Details
#initialize(opts = {}) ⇒ Unicorn
- pid
-
Path to unicorn PID file
- return_codes
-
Hash of return codes (defined in executable bin file)
- exec_path
-
Path to unicorn executable
Create a new instance of unicorn interactor
20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/red_unicorn/unicorn.rb', line 20 def initialize(opts={}) @opts = { :pid => '/var/run/unicorn/unicorn.pid', :exec_path => '/var/www/shared/bundle/bin/unicorn_rails', :config_path => '/etc/unicorn/app.rb', :action_timeout => 30, :restart_grace => 8, :env => 'production', :kind => 'unicorn' }.merge(opts) check_exec_path end |
Instance Method Details
#add_child ⇒ Object
Add new worker process
94 95 96 97 98 |
# File 'lib/red_unicorn/unicorn.rb', line 94 def add_child process_is :running do Process.kill('TTIN', pid) end end |
#halt ⇒ Object
Halts the current unicorn process
48 49 50 51 52 |
# File 'lib/red_unicorn/unicorn.rb', line 48 def halt process_is :running do Process.kill('TERM', pid) end end |
#reload ⇒ Object
Reload unicorn configuration
87 88 89 90 91 |
# File 'lib/red_unicorn/unicorn.rb', line 87 def reload process_is :running do Process.kill('HUP', pid) end end |
#remove_all_children ⇒ Object
Stops all worker processes but keeps the master process alive
109 110 111 112 113 |
# File 'lib/red_unicorn/unicorn.rb', line 109 def remove_all_children process_is :running do Process.kill('WINCH', pid) end end |
#remove_child ⇒ Object
Remove worker process
101 102 103 104 105 |
# File 'lib/red_unicorn/unicorn.rb', line 101 def remove_child process_is :running do Process.kill('TTOU', pid) end end |
#reopen_logs ⇒ Object
Reopen log files
116 117 118 119 120 |
# File 'lib/red_unicorn/unicorn.rb', line 116 def reopen_logs process_is :running do Process.kill('USR1', pid) end end |
#restart ⇒ Object
Graceful restart
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/red_unicorn/unicorn.rb', line 55 def restart begin process_is :running do original_pid = pid Process.kill('USR2', pid) sleep(@opts[:restart_grace]) # give unicorn some breathing room waited = 0 until((File.exists?(@opts[:pid]) && is_running? && !child_pids(pid).empty?) || waited > @opts[:action_timeout]) sleep_start = Time.now.to_f sleep(0.2) waited += Time.now.to_f - sleep_start end if(pid == original_pid || waited > @opts[:action_timeout]) raise UnicornError.new 'Failed to start new process' end Process.kill('QUIT', original_pid) while(is_running?(original_pid) && waited < @opts[:action_timeout]) sleep_start = Time.now.to_f sleep(0.2) waited += Time.now.to_f - sleep_start end raise IsRunning.new 'Failed to stop original unicorn process' if is_running?(original_pid) raise NotRunning.new 'Failed to start new unicorn process' unless is_running? reopen_logs end rescue NotRunning puts "WARN: #{@opts[:kind]} not currently running. Starting." start end end |
#start ⇒ Object
Start a new unicorn process
34 35 36 37 38 |
# File 'lib/red_unicorn/unicorn.rb', line 34 def start process_is :stopped do %x{#{@opts[:exec_path]} #{format_options}} end end |
#status ⇒ Object
Return status of current process
123 124 125 126 127 |
# File 'lib/red_unicorn/unicorn.rb', line 123 def status process_is :running do puts '* unicorn is running' end end |
#stop ⇒ Object
Stop current unicorn process
41 42 43 44 45 |
# File 'lib/red_unicorn/unicorn.rb', line 41 def stop process_is :running do Process.kill('QUIT', pid) end end |