Class: RedUnicorn::Unicorn

Inherits:
Object
  • Object
show all
Defined in:
lib/red_unicorn/unicorn.rb

Instance Method Summary collapse

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_childObject

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

#haltObject

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

#reloadObject

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_childrenObject

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_childObject

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_logsObject

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

#restartObject

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

#startObject

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

#statusObject

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

#stopObject

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