Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add god support (lol).

  • Loading branch information...
commit 62d842f4e8fe0e074ac3bbadf2b1cfa9d7e0f70a 1 parent 9e64d19
Rafael García Gallego rafaelgg authored
Showing with 99 additions and 14 deletions.
  1. +3 −0  Gemfile
  2. +16 −14 Gemfile.lock
  3. +54 −0 config/resque.god
  4. +26 −0 config/stale.god
3  Gemfile
View
@@ -49,6 +49,9 @@ end
# Deploy with Capistrano
gem 'capistrano'
+# Use god for its own purpose
+gem 'god'
+
# Use exception notification
gem 'exception_notification'
30 Gemfile.lock
View
@@ -125,7 +125,7 @@ GEM
actionmailer (>= 3.0.4)
execjs (1.3.0)
multi_json (~> 1.0)
- factory_girl (2.6.3)
+ factory_girl (2.6.4)
activesupport (>= 2.3.9)
faraday (0.7.6)
addressable (~> 2.2)
@@ -139,6 +139,7 @@ GEM
actionpack (>= 2.3.7)
activesupport (>= 2.3.7)
i18n (~> 0.4)
+ god (0.12.1)
has_scope (0.5.1)
hashie (1.2.0)
highline (1.6.11)
@@ -172,7 +173,7 @@ GEM
carrierwave (>= 0.5.8)
foreigner (>= 0.9.1)
rails (> 3.0.0)
- mime-types (1.17.2)
+ mime-types (1.18)
modernizr-rails (2.0.6)
multi_json (1.1.0)
multipart-post (1.1.5)
@@ -248,7 +249,7 @@ GEM
redis (2.2.2)
redis-namespace (1.0.3)
redis (< 3.0.0)
- responders (0.8.0)
+ responders (0.9.0)
railties (~> 3.1)
resque (1.20.0)
multi_json (~> 1.0)
@@ -259,23 +260,23 @@ GEM
mime-types (>= 1.16)
riddle (1.5.1)
rmagick (2.13.1)
- rspec (2.8.0)
- rspec-core (~> 2.8.0)
- rspec-expectations (~> 2.8.0)
- rspec-mocks (~> 2.8.0)
- rspec-core (2.8.0)
- rspec-expectations (2.8.0)
- diff-lcs (~> 1.1.2)
- rspec-mocks (2.8.0)
- rspec-rails (2.8.1)
+ rspec (2.9.0)
+ rspec-core (~> 2.9.0)
+ rspec-expectations (~> 2.9.0)
+ rspec-mocks (~> 2.9.0)
+ rspec-core (2.9.0)
+ rspec-expectations (2.9.0)
+ diff-lcs (~> 1.1.3)
+ rspec-mocks (2.9.0)
+ rspec-rails (2.9.0)
actionpack (>= 3.0)
activesupport (>= 3.0)
railties (>= 3.0)
- rspec (~> 2.8.0)
+ rspec (~> 2.9.0)
ruby-imagespec (0.2.0)
rubyzip (0.9.6.1)
sass (3.1.15)
- sass-rails (3.2.4)
+ sass-rails (3.2.5)
railties (~> 3.2.0)
sass (>= 3.1.10)
tilt (~> 1.3)
@@ -326,6 +327,7 @@ DEPENDENCIES
exception_notification
factory_girl
forgery
+ god
jquery-rails
mysql2
pg
54 config/resque.god
View
@@ -0,0 +1,54 @@
+rails_env = ENV['RAILS_ENV'] || "production"
+rails_root = ENV['RAILS_ROOT'] || "/u/apps/vish/current"
+num_workers = 1
+
+num_workers.times do |num|
+ God.watch do |w|
+ w.dir = "#{rails_root}"
+ w.name = "resque-worker-#{num}"
+ w.group = 'resque'
+ w.interval = 5.minutes
+ w.env = {"QUEUE"=>"*", "RAILS_ENV"=>rails_env}
+ w.start = "bundle exec rake -f #{rails_root}/Rakefile environment resque:work"
+
+ w.uid = 'www-data'
+ w.gid = 'www-data'
+
+ # restart if memory gets too high
+ w.transition(:up, :restart) do |on|
+ on.condition(:memory_usage) do |c|
+ c.above = 100.megabytes
+ c.times = 2
+ end
+ end
+
+ # determine the state on startup
+ w.transition(:init, { true => :up, false => :start }) do |on|
+ on.condition(:process_running) do |c|
+ c.running = true
+ end
+ end
+
+ # determine when process has finished starting
+ w.transition([:start, :restart], :up) do |on|
+ on.condition(:process_running) do |c|
+ c.running = true
+ c.interval = 5.seconds
+ end
+
+ # failsafe
+ on.condition(:tries) do |c|
+ c.times = 5
+ c.transition = :start
+ c.interval = 5.seconds
+ end
+ end
+
+ # start if process is not running
+ w.transition(:up, :start) do |on|
+ on.condition(:process_running) do |c|
+ c.running = false
+ end
+ end
+ end
+end
26 config/stale.god
View
@@ -0,0 +1,26 @@
+# This will ride alongside god and kill any rogue stale worker
+# processes. Their sacrifice is for the greater good.
+
+WORKER_TIMEOUT = 60 * 10 # 10 minutes
+
+Thread.new do
+ loop do
+ begin
+ `ps -e -o pid,command | grep [r]esque`.split("\n").each do |line|
+ parts = line.split(' ')
+ next if parts[-2] != "at"
+ started = parts[-1].to_i
+ elapsed = Time.now - Time.at(started)
+
+ if elapsed >= WORKER_TIMEOUT
+ ::Process.kill('USR1', parts[0].to_i)
+ end
+ end
+ rescue
+ # don't die because of stupid exceptions
+ nil
+ end
+
+ sleep 30
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.