Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ea5b794

Browse files
committedJun 26, 2014
replacing unicorn with puma, making it depend on .env config
removing duplicate gems from testing rebasing only use puma worker killer in prod
1 parent 2308fe3 commit ea5b794

File tree

7 files changed

+154
-58
lines changed

7 files changed

+154
-58
lines changed
 

‎.env.example

+7
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,10 @@ TWITTER_OAUTH_TOKEN=twitter_oauth_token
4242
TWITTER_REDIRECT_URL=http://localhost:3000/auth/twitter/callback
4343

4444
SESSION_SECRET=session_secret
45+
46+
47+
WEB_ROOT=/home/vagrant/web/
48+
WEB_MIN_CONCURRENCY=0
49+
WEB_MAX_CONCURRENCY=16
50+
WEB_WORKERS=8
51+
WEB_PORT=tcp://0.0.0.0:3000

‎Gemfile

+2-2
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ group :production do
178178
gem 'honeybadger'
179179
gem 'rails_stdout_logging'
180180
gem 'rails_12factor'
181-
gem 'unicorn'
182-
gem 'unicorn-worker-killer'
181+
gem 'puma'
182+
gem 'puma_worker_killer'
183183
gem 'newrelic_rpm'
184184
gem 'newrelic_resque_agent'
185185
gem 'le'

‎Gemfile.lock

+8-10
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ GEM
206206
rspec-instafail (~> 0.2.0)
207207
ruby-progressbar (~> 1.0)
208208
geocoder (1.1.4)
209+
get_process_mem (0.2.0)
209210
github-markdown (0.6.5)
210211
grackle (0.2.1)
211212
json
@@ -279,7 +280,6 @@ GEM
279280
kaminari (0.14.1)
280281
actionpack (>= 3.0.0)
281282
activesupport (>= 3.0.0)
282-
kgio (2.9.2)
283283
kramdown (1.3.3)
284284
launchy (2.4.2)
285285
addressable (~> 2.3)
@@ -405,6 +405,11 @@ GEM
405405
pubnub (0.1.9)
406406
em-http-request (>= 1.0.2)
407407
json
408+
puma (2.8.2)
409+
rack (>= 1.1, < 2.0)
410+
puma_worker_killer (0.0.3)
411+
get_process_mem (~> 0.1)
412+
puma (~> 2.7)
408413
querystring (0.1.0)
409414
quiet_assets (1.0.2)
410415
railties (>= 3.1, < 5.0)
@@ -445,7 +450,6 @@ GEM
445450
rake (>= 0.8.7)
446451
rdoc (~> 3.4)
447452
thor (>= 0.14.6, < 2.0)
448-
raindrops (0.13.0)
449453
rake (10.3.1)
450454
rb-fsevent (0.9.4)
451455
rb-inotify (0.9.3)
@@ -586,12 +590,6 @@ GEM
586590
uglifier (1.3.0)
587591
execjs (>= 0.3.0)
588592
multi_json (~> 1.0, >= 1.0.2)
589-
unicorn (4.8.2)
590-
kgio (~> 2.6)
591-
rack
592-
raindrops (~> 0.7)
593-
unicorn-worker-killer (0.4.2)
594-
unicorn (~> 4)
595593
vcr (2.8.0)
596594
vegas (0.1.11)
597595
rack (>= 1.0.0)
@@ -676,6 +674,8 @@ DEPENDENCIES
676674
omniauth-twitter (~> 0.0.16)
677675
pg
678676
pubnub (= 0.1.9)
677+
puma
678+
puma_worker_killer
679679
querystring
680680
quiet_assets
681681
rack-timeout
@@ -711,7 +711,5 @@ DEPENDENCIES
711711
tweet-button
712712
twitter
713713
uglifier (>= 1.0.3)
714-
unicorn
715-
unicorn-worker-killer
716714
vcr
717715
webmock (< 1.16)

‎Procfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
1+
web: bundle exec puma -c ./config/puma.rb
22
worker: env QUEUE=CRITICAL,HIGH,MEDIUM,LOW,LOWER bundle exec rake resque:work
33
scheduler: bundle exec rake resque:scheduler
44
refresher: env QUEUE=REFRESH bundle exec rake resque:work

‎config.ru

+7-12
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
if ENV['RAILS_ENV'] == 'production'
2-
require 'unicorn/worker_killer'
2+
require 'puma_worker_killer'
33

4-
max_request_min = 500
5-
max_request_max = 600
6-
7-
# Max requests per worker
8-
use Unicorn::WorkerKiller::MaxRequests, max_request_min, max_request_max
9-
10-
oom_min = (240) * (1024**2)
11-
oom_max = (260) * (1024**2)
12-
13-
# Max memory size (RSS) per worker
14-
use Unicorn::WorkerKiller::Oom, oom_min, oom_max
4+
PumaWorkerKiller.config do |config|
5+
config.ram = 260 # mb
6+
config.frequency = 15 # seconds
7+
config.percent_usage = 0.98
8+
end
9+
PumaWorkerKiller.start
1510
end
1611

1712
require ::File.expand_path('../config/environment', __FILE__)

‎config/puma.rb

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#!/usr/bin/env puma
2+
require 'dotenv'
3+
Dotenv.load
4+
root = ENV['WEB_ROOT'] || "/home/vagrant/web/"
5+
# The directory to operate out of.
6+
#
7+
# The default is the current directory.
8+
#
9+
10+
# Set the environment in which the rack's app will run. The value must be a string.
11+
#
12+
# The default is “development”. (pass with environment variable, -e)
13+
#
14+
# environment "development"
15+
16+
# Daemonize the server into the background. Highly suggest that
17+
# this be combined with “pidfile” and “stdout_redirect”.
18+
#
19+
# The default is “false”.
20+
#
21+
daemonize true
22+
# daemonize false
23+
24+
# Store the pid of the server in the file at “path”.
25+
#
26+
pidfile "#{root}tmp/pids/puma.pid"
27+
28+
# Use “path” as the file to store the server info state. This is
29+
# used by “pumactl” to query and control the server.
30+
#
31+
#state_path '/Users/justinraines/Code/flu-vaccine-map/tmp/pids/puma.state'
32+
33+
# Redirect STDOUT and STDERR to files specified. The 3rd parameter
34+
# (“append”) specifies whether the output is appended, the default is
35+
# “false”.
36+
#
37+
stdout_redirect "#{root}log/puma.stdout.log", "#{root}log/puma.stderr.log", true
38+
# stdout_redirect '/u/apps/lolcat/log/stdout', '/u/apps/lolcat/log/stderr', true
39+
40+
# Disable request logging.
41+
#
42+
# The default is “false”.
43+
#
44+
# quiet
45+
46+
# Configure “min” to be the minimum number of threads to use to answer
47+
# requests and “max” the maximum.
48+
#
49+
# The default is “0, 16”.
50+
#
51+
threads ENV['WEB_MIN_CONCURRENCY'] || 0, ENV['WEB_MAX_CONCURRENCY'] || 16
52+
53+
# Bind the server to “url”. “tcp://”, “unix://” and “ssl://” are the only
54+
# accepted protocols.
55+
#
56+
# The default is “tcp://0.0.0.0:9292”.
57+
#
58+
bind ENV['WEB_PORT'] || "tcp://0.0.0.0:3000"
59+
60+
# bind 'unix:///var/run/puma.sock'
61+
# bind 'unix:///var/run/puma.sock?umask=0777'
62+
# bind 'ssl://127.0.0.1:9292?key=path_to_key&cert=path_to_cert'
63+
64+
# Instead of “bind 'ssl://127.0.0.1:9292?key=path_to_key&cert=path_to_cert'” you
65+
# can also use the “ssl_bind” option.
66+
#
67+
# ssl_bind '127.0.0.1', '9292', { key: path_to_key, cert: path_to_cert }
68+
69+
# Code to run before doing a restart. This code should
70+
# close log files, database connections, etc.
71+
#
72+
# This can be called multiple times to add code each time.
73+
#
74+
on_restart do
75+
defined?(ActiveRecord::Base) and
76+
ActiveRecord::Base.connection.disconnect!
77+
78+
if defined?(Resque)
79+
Resque.redis.quit
80+
Rails.logger.info('Disconnected from Redis')
81+
end
82+
end
83+
84+
# Command to use to restart puma. This should be just how to
85+
# load puma itself (ie. 'ruby -Ilib bin/puma'), not the arguments
86+
# to puma, as those are the same as the original process.
87+
#
88+
# restart_command '/u/app/lolcat/bin/restart_puma'
89+
90+
# === Cluster mode ===
91+
92+
# How many worker processes to run.
93+
#
94+
# The default is “0”.
95+
#
96+
workers ENV['WEB_WORKERS'] || 4
97+
98+
# Code to run when a worker boots to setup the process before booting
99+
# the app.
100+
#
101+
# This can be called multiple times to add hooks.
102+
#
103+
on_worker_boot do
104+
defined?(ActiveRecord::Base) and
105+
ActiveRecord::Base.establish_connection(Rails.application.config.database_configuration[Rails.env])
106+
107+
if defined?(Resque)
108+
Resque.redis = ENV['REDIS_URL']
109+
Rails.logger.info('Connected to Redis')
110+
end
111+
end
112+
113+
# Preload app to make use of ruby 2.0 features
114+
preload_app!
115+
116+
# === Puma control rack application ===
117+
118+
# Start the puma control rack application on “url”. This application can
119+
# be communicated with to control the main server. Additionally, you can
120+
# provide an authentication token, so all requests to the control server
121+
# will need to include that token as a query parameter. This allows for
122+
# simple authentication.
123+
#
124+
# Check out https://github.com/puma/puma/blob/master/lib/puma/app/status.rb
125+
# to see what the app has available.
126+
#
127+
# activate_control_app 'unix:///var/run/pumactl.sock'
128+
# activate_control_app 'unix:///var/run/pumactl.sock', { auth_token: '12345' }
129+
# activate_control_app 'unix:///var/run/pumactl.sock', { no_token: true }

‎config/unicorn.rb

-33
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.