From ce728f6b7bd145922c6876df9a5f6481f2d1dfc3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jonne=20Ha=C3=9F?= <me@mrzyx.de>
Date: Fri, 21 Sep 2012 14:05:01 +0200
Subject: [PATCH] add possibility to embed a resque worker into the unicorn
 process

---
 config/defaults.yml         | 2 ++
 config/diaspora.yml.example | 6 +++++-
 config/unicorn.rb           | 8 ++++++++
 3 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/config/defaults.yml b/config/defaults.yml
index 6bdaaf74b9..cf65086eee 100644
--- a/config/defaults.yml
+++ b/config/defaults.yml
@@ -22,6 +22,8 @@ defaults:
       region:
     image_redirect_url:
     pubsub_server: 'https://pubsubhubbub.appspot.com/'
+    unicorn:
+      embed_resque_worker: false
   privacy:
     jquery_cdn: true
     google_analytics_key:
diff --git a/config/diaspora.yml.example b/config/diaspora.yml.example
index 83cbf65688..24a7c84ec9 100644
--- a/config/diaspora.yml.example
+++ b/config/diaspora.yml.example
@@ -99,7 +99,11 @@ configuration:
     ## Diaspora is only tested against this default pubsub server.
     ## You likely don't want to change this.
     #pubsub_server: 'https://pubsubhubbub.appspot.com/'
-  
+    
+    unicorn:
+      ## Embed a resque worker inside the unicorn process, useful for
+      ## minimal Heroku setups
+      #embed_resque_worker: true
   
   ## Settings probably affecting the privacy of your users 
   privacy:
diff --git a/config/unicorn.rb b/config/unicorn.rb
index 97a8d38895..0f998c0287 100644
--- a/config/unicorn.rb
+++ b/config/unicorn.rb
@@ -12,6 +12,8 @@ preload_app true
 # How long to wait before killing an unresponsive worker
 timeout 30
 
+@resque_pid = nil
+
 #pid '/var/run/diaspora/diaspora.pid'
 #listen '/var/run/diaspora/diaspora.sock', :backlog => 2048
 
@@ -29,6 +31,12 @@ before_fork do |server, worker|
   if !AppConfig.single_process_mode?
     Resque.redis.client.disconnect
   end
+  
+  if AppConfig.environment.unicorn.embed_resque_worker?
+    # Clean up Resque workers killed by previous deploys/restarts
+    Resque.workers.each { |w| w.unregister_worker }
+    @resque_pid ||= spawn('bundle exec rake resque:work QUEUES=*')
+  end
 
   old_pid = '/var/run/diaspora/diaspora.pid.oldbin'
   if File.exists?(old_pid) && server.pid != old_pid
-- 
GitLab