about summary refs log tree commit homepage
path: root/lib
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2009-10-03 13:24:26 -0700
committerEric Wong <normalperson@yhbt.net>2009-10-03 13:24:26 -0700
commit8edb6c7e5aeed022f224f163dddf02bc4468da91 (patch)
tree7458e070fa0922fefa06a4cd5ee1b18cc072548b /lib
parent05e96fe3a3487eac65721dde3655269226269d9b (diff)
downloadrainbows-8edb6c7e5aeed022f224f163dddf02bc4468da91.tar.gz
This allows the server to be configured by doing something like
this inside an existing Unicorn configuration file:

  Rainbows! do
    use :Revactor
    worker_connections 50
  end

This should make it obvious we're using Rainbows-only features.
Diffstat (limited to 'lib')
-rw-r--r--lib/rainbows.rb6
-rw-r--r--lib/rainbows/configurator.rb45
-rw-r--r--lib/rainbows/http_server.rb46
3 files changed, 35 insertions, 62 deletions
diff --git a/lib/rainbows.rb b/lib/rainbows.rb
index cb08ca3..f79c1e8 100644
--- a/lib/rainbows.rb
+++ b/lib/rainbows.rb
@@ -1,10 +1,14 @@
 # -*- encoding: binary -*-
 require 'unicorn'
 
+def Rainbows!(&block)
+  block_given? or raise ArgumentError, "Rainbows! requires a block"
+  Rainbows::HttpServer.setup(block)
+end
+
 module Rainbows
 
   require 'rainbows/const'
-  require 'rainbows/configurator'
   require 'rainbows/http_server'
   require 'rainbows/http_response'
 
diff --git a/lib/rainbows/configurator.rb b/lib/rainbows/configurator.rb
deleted file mode 100644
index 1d25f9e..0000000
--- a/lib/rainbows/configurator.rb
+++ /dev/null
@@ -1,45 +0,0 @@
-# -*- encoding: binary -*-
-module Rainbows
-
-  class Configurator < ::Unicorn::Configurator
-
-    # configures rainbows
-    def rainbows(&block)
-      block_given? or raise ArgumentError, "rainbows requires a block"
-      instance_eval(&block)
-    end
-
-  private
-
-    def use(model)
-      assert_in_rainbows
-      begin
-        model = Rainbows.const_get(model)
-      rescue NameError
-        raise ArgumentError, "concurrency model #{model.inspect} not supported"
-      end
-
-      Module === model or
-        raise ArgumentError, "concurrency model #{model.inspect} not supported"
-      set[:use] = model
-    end
-
-    def worker_connections(nr)
-      assert_in_rainbows
-      (Integer === nr && nr > 0) || nr.nil? or
-        raise ArgumentError, "worker_connections must be an Integer or nil"
-      set[:worker_connections] = nr
-    end
-
-  private
-
-    def assert_in_rainbows # :nodoc:
-      c = caller
-      c.grep(/`rainbows'\z/).empty? and
-        raise ArgumentError,
-             "#{%r!`(\w+)'\z!.match(c.first)[1]} must be called in `rainbows'"
-    end
-
-  end
-
-end
diff --git a/lib/rainbows/http_server.rb b/lib/rainbows/http_server.rb
index 3c2c035..8ed8fa2 100644
--- a/lib/rainbows/http_server.rb
+++ b/lib/rainbows/http_server.rb
@@ -5,29 +5,43 @@ module Rainbows
   class HttpServer < ::Unicorn::HttpServer
     include Rainbows
 
-    attr_accessor :worker_connections
-    attr_reader :use
+    @@instance = nil
+
+    class << self
+      def setup(block)
+        @@instance.instance_eval(&block)
+      end
+    end
 
     def initialize(app, options)
-      self.app = app
-      self.reexec_pid = 0
-      self.init_listeners = options[:listeners] ? options[:listeners].dup : []
-      self.config = Configurator.new(options.merge(:use_defaults => true))
-      self.listener_opts = {}
-      config.commit!(self, :skip => [:listeners, :pid])
-
-      defined?(@use) or
-        self.use = Rainbows.const_get(:ThreadPool)
-      defined?(@worker_connections) or
-        @worker_connections = 4
-
-      #self.orig_app = app
+      @@instance = self
+      rv = super(app, options)
+      defined?(@use) or use(:ThreadPool)
+      defined?(@worker_connections) or worker_connections(4)
+      rv
     end
 
-    def use=(model)
+    def use(*args)
+      return @use if args.empty?
+      model = begin
+        Rainbows.const_get(args.first)
+      rescue NameError
+        raise ArgumentError, "concurrency model #{model.inspect} not supported"
+      end
+
+      Module === model or
+        raise ArgumentError, "concurrency model #{model.inspect} not supported"
       extend(@use = model)
     end
 
+    def worker_connections(*args)
+      return @worker_connections if args.empty?
+      nr = args.first
+      (Integer === nr && nr > 0) || nr.nil? or
+        raise ArgumentError, "worker_connections must be an Integer or nil"
+      @worker_connections = nr
+    end
+
   end
 
 end