unicorn Ruby/Rack server user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
From: Aman Gupta <aman@tmm1.net>
To: mongrel-unicorn@rubyforge.org
Cc: Aman Gupta <aman@tmm1.net>
Subject: [PATCH] preload_app can take an optional block for warmup
Date: Fri, 20 Sep 2013 14:40:56 -0700	[thread overview]
Message-ID: <1379713256-23933-1-git-send-email-aman@tmm1.net> (raw)

---
 lib/unicorn/configurator.rb    | 19 ++++++++++++++++---
 lib/unicorn/http_server.rb     |  3 +++
 test/unit/test_configurator.rb |  8 ++++++++
 3 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/lib/unicorn/configurator.rb b/lib/unicorn/configurator.rb
index 0d0eac7..a0ae576 100644
--- a/lib/unicorn/configurator.rb
+++ b/lib/unicorn/configurator.rb
@@ -441,9 +441,22 @@ class Unicorn::Configurator
   # by properly deploying all required code and dependencies.
   # Using preload_app=true means any application load error will
   # cause the master process to exit with an error.
-
-  def preload_app(bool)
-    set_bool(:preload_app, bool)
+  #
+  # preload_app can also take an optional block. This block will be invoked
+  # with the rack application and can be used to "warm up" the application
+  # before deployment:
+  #
+  #  preload_app do |app|
+  #    client = Rack::MockRequest.new(app)
+  #    client.get('/')
+  #  end
+  #
+  def preload_app(bool=nil, &block)
+    if block_given? || bool.respond_to?(:call)
+      set_hook(:preload_app, block_given? ? block : bool, 1)
+    else
+      set_bool(:preload_app, bool)
+    end
   end
 
   # Toggles making \env[\"rack.input\"] rewindable.
diff --git a/lib/unicorn/http_server.rb b/lib/unicorn/http_server.rb
index bed24d0..d749a92 100644
--- a/lib/unicorn/http_server.rb
+++ b/lib/unicorn/http_server.rb
@@ -721,6 +721,9 @@ class Unicorn::HttpServer
         Gem.refresh
       end
       self.app = app.call
+      if preload_app.respond_to?(:call)
+        preload_app[app]
+      end
     end
   end
 
diff --git a/test/unit/test_configurator.rb b/test/unit/test_configurator.rb
index 1298f0e..8a1a68e 100644
--- a/test/unit/test_configurator.rb
+++ b/test/unit/test_configurator.rb
@@ -172,4 +172,12 @@ class TestConfigurator < Test::Unit::TestCase
     end
   end
 
+  def test_preload_app
+    test_struct = TestStruct.new
+    [ true, false, proc { |a| }, Proc.new { |a| }, lambda { |a| } ].each do |my_proc|
+      Unicorn::Configurator.new(:preload_app => my_proc).commit!(test_struct)
+      assert_equal my_proc, test_struct.preload_app
+    end
+  end
+
 end
-- 
1.8.3.4

_______________________________________________
Unicorn mailing list - mongrel-unicorn@rubyforge.org
http://rubyforge.org/mailman/listinfo/mongrel-unicorn
Do not quote signatures (like this one) or top post when replying

             reply	other threads:[~2013-09-20 21:49 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-09-20 21:40 Aman Gupta [this message]
2013-09-21  8:49 ` [PATCH] preload_app can take an optional block for warmup Eric Wong
2013-09-21 23:10   ` Aman Gupta
2013-09-23 10:58     ` Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://yhbt.net/unicorn/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1379713256-23933-1-git-send-email-aman@tmm1.net \
    --to=aman@tmm1.net \
    --cc=mongrel-unicorn@rubyforge.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhbt.net/unicorn.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).