trying to get Heroku working

Andrew Cantino преди 10 години
родител
ревизия
b9e7d6d617
променени са 5 файла, в които са добавени 79 реда и са изтрити 12 реда
  1. 4 3
      Gemfile
  2. 15 0
      Gemfile.lock
  3. 9 7
      Procfile
  4. 2 2
      config/initializers/delayed_job.rb
  5. 49 0
      deployment/heroku/unicorn.rb

+ 4 - 3
Gemfile

@@ -22,7 +22,6 @@ else
22 22
   gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw]
23 23
 end
24 24
 
25
-gem 'mysql2', '~> 0.3.16'
26 25
 gem 'devise', '~> 3.2.4'
27 26
 gem 'kaminari', '~> 0.16.1'
28 27
 gem 'bootstrap-kaminari-views', '~> 0.0.3'
@@ -73,11 +72,13 @@ gem 'weibo_2', '~> 0.1.4'
73 72
 gem 'hipchat', '~> 1.2.0'
74 73
 gem 'xmpp4r',  '~> 0.5.6'
75 74
 gem 'slack-notifier', '~> 0.5.0'
76
-
77 75
 gem 'therubyracer', '~> 0.12.1'
78
-
79 76
 gem 'mqtt'
80 77
 
78
+gem 'mysql2', '~> 0.3.16'
79
+gem 'pg', group: :production
80
+gem 'unicorn', groups: [:development, :production]
81
+gem 'rails_12factor', group: :production
81 82
 
82 83
 group :development do
83 84
   gem 'binding_of_caller'

+ 15 - 0
Gemfile.lock

@@ -161,6 +161,7 @@ GEM
161 161
     kaminari (0.16.1)
162 162
       actionpack (>= 3.0.0)
163 163
       activesupport (>= 3.0.0)
164
+    kgio (2.8.0)
164 165
     kramdown (1.3.3)
165 166
     launchy (2.4.2)
166 167
       addressable (~> 2.3)
@@ -192,6 +193,7 @@ GEM
192 193
       multi_xml (~> 0.5)
193 194
       rack (~> 1.2)
194 195
     orm_adapter (0.5.0)
196
+    pg (0.17.1)
195 197
     polyglot (0.3.5)
196 198
     protected_attributes (1.0.8)
197 199
       activemodel (>= 4.0.1, < 5.0)
@@ -214,11 +216,17 @@ GEM
214 216
       bundler (>= 1.3.0, < 2.0)
215 217
       railties (= 4.1.4)
216 218
       sprockets-rails (~> 2.0)
219
+    rails_12factor (0.0.2)
220
+      rails_serve_static_assets
221
+      rails_stdout_logging
222
+    rails_serve_static_assets (0.0.2)
223
+    rails_stdout_logging (0.0.3)
217 224
     railties (4.1.4)
218 225
       actionpack (= 4.1.4)
219 226
       activesupport (= 4.1.4)
220 227
       rake (>= 0.8.7)
221 228
       thor (>= 0.18.1, < 2.0)
229
+    raindrops (0.11.0)
222 230
     rake (10.3.2)
223 231
     ref (1.0.5)
224 232
     rest-client (1.6.7)
@@ -317,6 +325,10 @@ GEM
317 325
     uglifier (2.5.1)
318 326
       execjs (>= 0.3.0)
319 327
       json (>= 1.8.0)
328
+    unicorn (4.6.3)
329
+      kgio (~> 2.6)
330
+      rack
331
+      raindrops (~> 0.7)
320 332
     uuid (2.3.7)
321 333
       macaddr (~> 1.0)
322 334
     uuidtools (2.1.4)
@@ -374,11 +386,13 @@ DEPENDENCIES
374 386
   mqtt
375 387
   mysql2 (~> 0.3.16)
376 388
   nokogiri (~> 1.6.1)
389
+  pg
377 390
   protected_attributes (~> 1.0.8)
378 391
   pry
379 392
   quiet_assets
380 393
   rack
381 394
   rails (= 4.1.4)
395
+  rails_12factor
382 396
   rr
383 397
   rspec (~> 2.14)
384 398
   rspec-rails (~> 2.14)
@@ -395,6 +409,7 @@ DEPENDENCIES
395 409
   typhoeus (~> 0.6.3)
396 410
   tzinfo-data
397 411
   uglifier (>= 1.3.0)
412
+  unicorn
398 413
   vcr
399 414
   webmock (~> 1.17.4)
400 415
   weibo_2 (~> 0.1.4)

+ 9 - 7
Procfile

@@ -1,13 +1,15 @@
1 1
 # Procfile for development using the new threaded worker (scheduler, twitter stream and delayed job)
2
-web: bundle exec rails server
3
-jobs: bundle exec rails runner bin/threaded.rb
2
+# web: bundle exec rails server
3
+# jobs: bundle exec rails runner bin/threaded.rb
4
+
5
+web: bundle exec unicorn -p $PORT -c ./deployment/heroku/unicorn.rb
4 6
 
5 7
 # Possible Profile configuration for production:
6 8
 # web: bundle exec unicorn -c config/unicorn/production.rb
7 9
 # jobs: bundle exec rails runner bin/threaded.rb
8 10
 
9
-# Old version with seperate processes (use this if you have issues with the threaded version)
10
-#web: bundle exec rails server
11
-#schedule: bundle exec rails runner bin/schedule.rb
12
-#twitter: bundle exec rails runner bin/twitter_stream.rb
13
-#dj: bundle exec script/delayed_job run
11
+# Old version with seperate processes for each component (use this if you have issues with the threaded version)
12
+# web: bundle exec rails server
13
+# schedule: bundle exec rails runner bin/schedule.rb
14
+# twitter: bundle exec rails runner bin/twitter_stream.rb
15
+# dj: bundle exec script/delayed_job run

+ 2 - 2
config/initializers/delayed_job.rb

@@ -5,5 +5,5 @@ Delayed::Worker.read_ahead = 5
5 5
 Delayed::Worker.default_priority = 10
6 6
 Delayed::Worker.delay_jobs = !Rails.env.test?
7 7
 
8
-Delayed::Worker.logger = Logger.new(Rails.root.join('log', 'delayed_job.log'))
9
-Delayed::Worker.logger.level = Logger::DEBUG
8
+# Delayed::Worker.logger = Logger.new(Rails.root.join('log', 'delayed_job.log'))
9
+# Delayed::Worker.logger.level = Logger::DEBUG

+ 49 - 0
deployment/heroku/unicorn.rb

@@ -0,0 +1,49 @@
1
+require "net/http"
2
+
3
+worker_processes Integer(ENV["WEB_CONCURRENCY"] || 2)
4
+timeout 15
5
+preload_app true
6
+
7
+# Note that this will only work correctly when running Heroku with ONE web worker.
8
+# If you want to run more than one, use the standard Huginn Procfile instead with separate web and job entries.
9
+Thread.new do
10
+  worker_pid = nil
11
+  while true
12
+    if worker_pid.nil?
13
+      worker_pid = spawn("bundle exec rails runner bin/threaded.rb")
14
+      puts "New threaded worker PID: #{worker_pid}"
15
+    end
16
+
17
+    sleep 30
18
+
19
+    if ENV['PING_URL']
20
+      Net::HTTP.get_response(URI(ENV['PING_URL']))
21
+    end
22
+
23
+    begin
24
+      Process.getpgid worker_pid
25
+    rescue Errno::ESRCH
26
+      # No longer running
27
+      worker_pid = nil
28
+    end
29
+  end
30
+end
31
+
32
+before_fork do |server, worker|
33
+  Signal.trap 'TERM' do
34
+    puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
35
+    Process.kill 'QUIT', Process.pid
36
+  end
37
+
38
+  defined?(ActiveRecord::Base) and
39
+    ActiveRecord::Base.connection.disconnect!
40
+end
41
+
42
+after_fork do |server, worker|
43
+  Signal.trap 'TERM' do
44
+    puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to send QUIT'
45
+  end
46
+
47
+  defined?(ActiveRecord::Base) and
48
+    ActiveRecord::Base.establish_connection
49
+end