public
Description: A very fast & simple Ruby web server
Homepage: http://code.macournoyer.com/thin/
Clone URL: git://github.com/macournoyer/thin.git
Search Repo:
Refactor cluster command running logic into Command class.
macournoyer (author)
Mon Jan 28 20:09:21 -0800 2008
commit  f3417196f32db106f44feecf6bf2b77621c5b994
tree    4e66c97b8e3953434d2b4121a9dd642349bfc7ca
parent  fe618685efcb35f251865703a8028376008a5b10
...
15
16
17
 
18
19
20
...
15
16
17
18
19
20
21
0
@@ -15,6 +15,7 @@
0
   SERVER = "#{NAME} #{VERSION::STRING} codename #{VERSION::CODENAME}".freeze
0
   
0
   autoload :Cluster, 'thin/cluster'
0
+ autoload :Command, 'thin/command'
0
   autoload :Connection, 'thin/connection'
0
   autoload :Daemonizable, 'thin/daemonizing'
0
   autoload :Logging, 'thin/logging'
...
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
 
119
120
121
...
100
101
102
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
104
105
106
0
@@ -100,22 +100,7 @@
0
         else
0
           cmd_options.merge!(:port => number)
0
         end
0
- shell_cmd = shellify(cmd, cmd_options)
0
- trace shell_cmd
0
- ouput = `#{shell_cmd}`.chomp
0
- log " " + ouput.gsub("\n", " \n") unless ouput.empty?
0
- end
0
-
0
- # Turn into a runnable shell command
0
- def shellify(cmd, options)
0
- shellified_options = options.inject([]) do |args, (name, value)|
0
- args << case value
0
- when NilClass
0
- when TrueClass then "--#{name}"
0
- else "--#{name.to_s.tr('_', '-')}=#{value.inspect}"
0
- end
0
- end
0
- "#{@script} #{cmd} #{shellified_options.compact.join(' ')}"
0
+ Command.run(cmd, cmd_options)
0
       end
0
       
0
       def with_each_server
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
0
@@ -1 +1,41 @@
0
+module Thin
0
+ # Run a command though the +thin+ command-line script.
0
+ class Command
0
+ include Logging
0
+
0
+ # Path to the +thin+ script used to control the servers.
0
+ # Leave this to default to use the one in the path.
0
+ attr_accessor :script
0
+
0
+ def initialize(name, options={})
0
+ @name = name
0
+ @options = options
0
+ @script = 'thin'
0
+ end
0
+
0
+ def self.run(*args)
0
+ new(*args).run
0
+ end
0
+
0
+ # Send the command to the +thin+ script
0
+ def run
0
+ shell_cmd = shellify
0
+ trace shell_cmd
0
+ ouput = `#{shell_cmd}`.chomp
0
+ log " " + ouput.gsub("\n", " \n") unless ouput.empty?
0
+ end
0
+
0
+ # Turn into a runnable shell command
0
+ def shellify
0
+ shellified_options = @options.inject([]) do |args, (name, value)|
0
+ args << case value
0
+ when NilClass
0
+ when TrueClass then "--#{name}"
0
+ else "--#{name.to_s.tr('_', '-')}=#{value.inspect}"
0
+ end
0
+ end
0
+ "#{@script} #{@name} #{shellified_options.compact.join(' ')}"
0
+ end
0
+ end
0
+end
...
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
 
 
 
 
 
42
43
 
44
45
46
47
48
49
50
51
 
 
 
 
52
53
 
54
 
 
 
 
 
55
56
57
58
59
60
61
...
82
83
84
85
86
87
88
89
90
 
 
 
 
91
92
 
93
94
95
96
97
98
99
100
 
 
 
 
101
102
 
103
 
 
 
 
 
 
104
105
106
107
...
127
128
129
130
131
132
133
134
 
135
136
137
 
 
 
 
 
138
...
26
27
28
 
 
 
 
 
 
 
 
 
 
 
 
 
29
30
31
32
33
34
 
35
36
37
 
 
 
 
 
 
38
39
40
41
42
 
43
44
45
46
47
48
49
50
51
52
53
54
55
56
...
77
78
79
 
 
 
 
 
 
80
81
82
83
84
 
85
86
87
 
 
 
 
 
 
88
89
90
91
92
 
93
94
95
96
97
98
99
100
101
102
103
104
...
124
125
126
 
 
 
 
 
127
128
129
130
131
132
133
134
135
136
0
@@ -26,32 +26,27 @@
0
     end
0
     calls.should == [3000, 3001, 3002]
0
   end
0
-
0
- it 'should shellify command' do
0
- out = @cluster.send(:shellify, :start, :port => 3000, :daemonize => true, :log => 'hi.log', :pid => nil)
0
- out.should include('--port=3000', '--daemonize', '--log="hi.log"', 'thin start --')
0
- out.should_not include('--pid=')
0
- end
0
-
0
- it 'should start on specified port' do
0
- @cluster.should_receive(:`) do |with|
0
- with.should include('thin start', '--daemonize', 'thin.3001.log', 'thin.3001.pid', '--port=3001')
0
- with.should_not include('--socket')
0
- ''
0
- end
0
+
0
+ it 'should start on each port' do
0
+ Command.should_receive(:run).with(:start, options_for_port(3000))
0
+ Command.should_receive(:run).with(:start, options_for_port(3001))
0
+ Command.should_receive(:run).with(:start, options_for_port(3002))
0
 
0
- @cluster.start_server 3001
0
+ @cluster.start
0
   end
0
 
0
- it 'should stop on specified port' do
0
- @cluster.should_receive(:`) do |with|
0
- with.should include('thin stop', '--daemonize', 'thin.3001.log', 'thin.3001.pid', '--port=3001')
0
- with.should_not include('--socket')
0
- ''
0
- end
0
+ it 'should stop on each port' do
0
+ Command.should_receive(:run).with(:stop, options_for_port(3000))
0
+ Command.should_receive(:run).with(:stop, options_for_port(3001))
0
+ Command.should_receive(:run).with(:stop, options_for_port(3002))
0
 
0
- @cluster.stop_server 3001
0
+ @cluster.stop
0
   end
0
+
0
+ private
0
+ def options_for_port(port)
0
+ { :daemonize => true, :log => "thin.#{port}.log", :timeout => 10, :address => "0.0.0.0", :port => port, :pid => "thin.#{port}.pid", :chdir => "./spec/rails_app" }
0
+ end
0
 end
0
 
0
 describe Cluster, "with UNIX socket" do
0
0
0
0
0
@@ -82,25 +77,27 @@
0
     calls.should == [0, 1, 2]
0
   end
0
   
0
- it 'should start specified server' do
0
- @cluster.should_receive(:`) do |with|
0
- with.should include('thin start', '--daemonize', 'thin.1.log', 'thin.1.pid', '--socket="/tmp/thin.1.sock"')
0
- with.should_not include('--port', '--address')
0
- ''
0
- end
0
+ it 'should start each server' do
0
+ Command.should_receive(:run).with(:start, options_for_socket(0))
0
+ Command.should_receive(:run).with(:start, options_for_socket(1))
0
+ Command.should_receive(:run).with(:start, options_for_socket(2))
0
 
0
- @cluster.start_server 1
0
+ @cluster.start
0
   end
0
 
0
- it 'should stop specified server' do
0
- @cluster.should_receive(:`) do |with|
0
- with.should include('thin stop', '--daemonize', 'thin.1.log', 'thin.1.pid', '--socket="/tmp/thin.1.sock"')
0
- with.should_not include('--port', '--address')
0
- ''
0
- end
0
+ it 'should stop each server' do
0
+ Command.should_receive(:run).with(:stop, options_for_socket(0))
0
+ Command.should_receive(:run).with(:stop, options_for_socket(1))
0
+ Command.should_receive(:run).with(:stop, options_for_socket(2))
0
 
0
- @cluster.stop_server 1
0
+ @cluster.stop
0
   end
0
+
0
+
0
+ private
0
+ def options_for_socket(number)
0
+ { :daemonize => true, :log => "thin.#{number}.log", :timeout => 10, :socket => "/tmp/thin.#{number}.sock", :pid => "thin.#{number}.pid", :chdir => "./spec/rails_app" }
0
+ end
0
 end
0
 
0
 describe Cluster, "controlling only one server" do
0
0
@@ -127,13 +124,14 @@
0
   end
0
   
0
   it "should start only specified server" do
0
- @cluster.should_receive(:`) do |with|
0
- with.should include('thin start', '--daemonize', 'thin.3001.log', 'thin.3001.pid', '--port=3001')
0
- with.should_not include('3000', '3002')
0
- ''
0
- end
0
+ Command.should_receive(:run).with(:start, options_for_port(3001))
0
 
0
     @cluster.start
0
   end
0
+
0
+ private
0
+ def options_for_port(port)
0
+ { :daemonize => true, :log => "thin.#{port}.log", :timeout => 10, :address => "0.0.0.0", :port => port, :pid => "thin.#{port}.pid", :chdir => "./spec/rails_app" }
0
+ end
0
 end
...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
0
@@ -1 +1,16 @@
0
+require File.dirname(__FILE__) + '/spec_helper'
0
+
0
+describe Command do
0
+ before do
0
+ @command = Command.new(:start, :port => 3000, :daemonize => true, :log => 'hi.log', :pid => nil)
0
+ @command.script = File.dirname(__FILE__) + '/../bin/thin'
0
+ @command.silent = true
0
+ end
0
+
0
+ it 'should shellify command' do
0
+ out = @command.shellify
0
+ out.should include('--port=3000', '--daemonize', '--log="hi.log"', 'thin start --')
0
+ out.should_not include('--pid=')
0
+ end
0
+end

Comments

    No one has commented yet.