shotgun 0.7 → 0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,6 +4,7 @@ require 'optparse'
4
4
 
5
5
  env = ENV['RACK_ENV'] || 'development'
6
6
  browse = false
7
+ server = nil
7
8
  public_dir = 'public' if File.directory?('public')
8
9
  options = {:Port => 9393, :Host => '127.0.0.1', :AccessLog => [], :Path => '/'}
9
10
 
@@ -110,7 +111,9 @@ require 'rack/showexceptions'
110
111
 
111
112
  require 'shotgun'
112
113
 
114
+ require 'thin' if server.to_s.downcase == 'thin'
113
115
  server = Rack::Handler.get(server) || Rack::Handler.default
116
+
114
117
  app =
115
118
  Rack::Builder.new do
116
119
  # these middleware run in the master process.
@@ -145,6 +148,9 @@ downward = false
145
148
  end
146
149
  end
147
150
 
151
+ # load shotgun.rb in current working directory if it exists
152
+ Shotgun.preload
153
+
148
154
  base_url = "http://#{options[:Host]}:#{options[:Port]}#{options[:Path]}"
149
155
  puts "== Shotgun/#{server.to_s.sub(/Rack::Handler::/, '')} on #{base_url}"
150
156
  server.run app, options do |inst|
@@ -12,4 +12,33 @@ module Shotgun
12
12
  def self.enable_copy_on_write
13
13
  GC.copy_on_write_friendly = true if GC.respond_to?(:copy_on_write_friendly=)
14
14
  end
15
+
16
+ def self.preload(files=%w[./config/shotgun.rb ./shotgun.rb])
17
+ files.each do |preload_file|
18
+ if File.exist?(preload_file)
19
+ module_eval File.read(preload_file), preload_file
20
+ return preload_file
21
+ end
22
+ end
23
+ end
24
+
25
+ def self.before_fork(&block)
26
+ @before_fork ||= []
27
+ @before_fork << block if block
28
+ @before_fork
29
+ end
30
+
31
+ def self.after_fork(&block)
32
+ @after_fork ||= []
33
+ @after_fork << block if block
34
+ @after_fork
35
+ end
36
+
37
+ def self.before_fork!
38
+ before_fork.each { |block| block.call }
39
+ end
40
+
41
+ def self.after_fork!
42
+ after_fork.each { |block| block.call }
43
+ end
15
44
  end
@@ -22,9 +22,12 @@ module Shotgun
22
22
  @env = env
23
23
  @reader, @writer = IO.pipe
24
24
 
25
+ Shotgun.before_fork!
26
+
25
27
  if @child = fork
26
28
  proceed_as_parent
27
29
  else
30
+ Shotgun.after_fork!
28
31
  proceed_as_child
29
32
  end
30
33
  end
@@ -0,0 +1,197 @@
1
+ .\" generated with Ronn/v0.6.42
2
+ .\" http://github.com/rtomayko/ronn/tree/0.6.6-36-gb67d494
3
+ .
4
+ .TH "SHOTGUN" "1" "June 2010" "Shotgun 0.8" ""
5
+ .
6
+ .SH "NAME"
7
+ \fBshotgun\fR \- reloading rack development server
8
+ .
9
+ .SH "SYNOPSIS"
10
+ \fBshotgun\fR [\fIoptions\fR] [\fIrackup\-file\fR]
11
+ .
12
+ .SH "DESCRIPTION"
13
+ \fBShotgun\fR is a simple process\-per\-request Rack \fIhttp://rack\.rubyforge\.org/doc/README\.html\fR server designed for use in development environments\. Each time a request is received, \fBshotgun\fR forks, loads the \fIrackup\-file\fR, processes a single request and exits\. The result is application\-wide reloading of all configuration, source files, and templates without the need for complex application\-level reloading logic\.
14
+ .
15
+ .P
16
+ When no \fIrackup\-file\fR is given, \fBshotgun\fR uses the \fBconfig\.ru\fR file in the current working directory\.
17
+ .
18
+ .SH "OPTIONS"
19
+ Shotgun runs at \fBhttp://127\.0\.0\.1:9393\fR by default\. The following options control server behavior:
20
+ .
21
+ .TP
22
+ \fB\-E\fR, \fB\-\-env\fR=\fIenvironment\fR
23
+ Sets the \fBRACK_ENV\fR environment variable to \fIenvironment\fR and selects the default set of utility middleware\. When \fIenvironment\fR is \'development\', shotgun inserts the \fBRack::Lint\fR and \fBRack::CommonLogger\fR middleware components; when \fIenvironment\fR is \'production\' or \'deployed\', \fBRack::CommonLogger\fR is inserted; otherwise, no utility middleware are inserted\.
24
+ .
25
+ .TP
26
+ \fB\-P\fR, \fB\-\-public\fR=\fIpath\fR
27
+ Serve requests for static files that exist under \fIpath\fR from the shotgun master process without forking a worker process\. This option is automatically enabled when a \fB\./public\fR directory is detected, but can be configured explicitly for non\-conventional static file directory locations\.
28
+ .
29
+ .IP
30
+ Setting this option appropriately can severely improve overall page load times for applications with many static assets\.
31
+ .
32
+ .TP
33
+ \fB\-s\fR, \fB\-\-server\fR=\fImongrel\fR|\fIwebrick\fR|\fIthin\fR|\fIother\fR
34
+ The Rack server handler implementation used to serve requests\. Supported values include: \fBmongrel\fR, \fBwebrick\fR, and \fBthin\fR\. By default, shotgun first tries to use \fBmongrel\fR and falls back to \fBwebrick\fR if mongrel is not available\.
35
+ .
36
+ .TP
37
+ \fB\-o\fR, \fB\-\-host\fR=\fIaddr\fR
38
+ The hostname or address of the interface the HTTP server should bind to\. Default: \fB127\.0\.0\.1\fR\.
39
+ .
40
+ .TP
41
+ \fB\-p\fR, \fB\-\-port\fR=\fIport\fR
42
+ The port the HTTP server should bind to\. Default: \fB9393\fR\.
43
+ .
44
+ .TP
45
+ \fB\-O\fR, \fB\-\-browse\fR
46
+ Open browser at http://\fIhost\fR:\fIport\fR/ immediately after the server is started\.
47
+ .
48
+ .P
49
+ Ruby environment related options:
50
+ .
51
+ .TP
52
+ \fB\-r\fR, \fB\-\-require\fR \fIlibrary\fR
53
+ Require \fIlibrary\fR before loading the application and starting the server\. This can be used to load a portion of the application code in the master process so it doesn\'t need to be loaded in the child\. See \fIPRELOADING\fR][] for more information on this approach\.
54
+ .
55
+ .TP
56
+ \fB\-e\fR, \fB\-\-eval\fR \fIcommand\fR
57
+ Evaluate arbitrary \fIcommand\fR within the shotgun master Ruby interpreter\. \fIcommand\fR is evaluated as program arguments are parsed\. Multiple \fB\-e\fR arguments are allowed\.
58
+ .
59
+ .TP
60
+ \fB\-d\fR, \fB\-\-debug\fR
61
+ Turns on debug mode\. \fB$DEBUG\fR will be set \fBtrue\fR\.
62
+ .
63
+ .TP
64
+ \fB\-w\fR, \fB\-\-warn\fR
65
+ Enable verbose mode without printing version message at the beginning\. It sets the \fB$VERBOSE\fR variable to true\.
66
+ .
67
+ .TP
68
+ \fB\-I\fR, \fB\-\-include\fR \fIpath\fR
69
+ Add \fIpath\fR to the Ruby load path (\fB$LOAD_PATH\fR)\. May be used more than once\.
70
+ .
71
+ .P
72
+ Miscellaneous:
73
+ .
74
+ .TP
75
+ \fB\-h\fR, \fB\-\-help\fR
76
+ Show usage message and exit\.
77
+ .
78
+ .TP
79
+ \fB\-\-version\fR
80
+ Show the Rack version and exit\.
81
+ .
82
+ .SH "PRELOADING"
83
+ It\'s possible to load support libraries and portions of the application in the shotgun master process to reduce the amount of work that needs to be done for each request in worker processes\. There\'s two ways of accomplishing this: either by specifying one or more \fB\-\-require\fR (\fB\-r\fR) arguments or through the use of a \fBshotgun\.rb\fR file\.
84
+ .
85
+ .P
86
+ During start up, shotgun looks for a \fBshotgun\.rb\fR or \fBconfig/shotgun\.rb\fR file\. If either file is found, it\'s loaded into the shotgun master process\. Code at the top\-level of the \fBshotgun\.rb\fR is run once on startup, so just require whatever you want to preload\. It\'s also possible to register callbacks to run before each request in either the master or child worker process:
87
+ .
88
+ .TP
89
+ \fBafter_fork {\fR \fIstuff\fR \fB}\fR
90
+ Run \fIstuff\fR in the shotgun child worker process immediately after forking\. Any files or socket connections opened in the master process should be closed / re\-established by an \fBafter_fork\fR block\.
91
+ .
92
+ .TP
93
+ \fBbefore_fork {\fR \fIstuff\fR \fB}\fR
94
+ Run \fIstuff\fR in the shotgun master process on each request before forking the child worker process\. This is typically less useful than \fBafter_fork\fR, but provided for completeness\.
95
+ .
96
+ .P
97
+ Example \fBconfig/shotgun\.rb\fR file from the main github\.com rails project:
98
+ .
99
+ .IP "" 4
100
+ .
101
+ .nf
102
+
103
+ # make sure the load path includes RAILS_ROOT
104
+ ENV[\'RAILS_ROOT\'] ||= File\.expand_path(\'\.\./\.\.\', __FILE__)
105
+ $:\.unshift ENV[\'RAILS_ROOT\']
106
+
107
+ # bring in the base rails environment and some libraries
108
+ require \'config/environment\'
109
+ require \'google\-charts\'
110
+ require \'aws\-s3\'
111
+
112
+ # disable Rails\'s built in class reloading
113
+ Rails\.configuration\.cache_classes = true
114
+
115
+ # reset database and redis connections in workers
116
+ after_fork do
117
+ ActiveRecord::Base\.establish_connection
118
+ CHIMNEY\.client = $redis
119
+ end
120
+ .
121
+ .fi
122
+ .
123
+ .IP "" 0
124
+ .
125
+ .SH "INSTALLING"
126
+ Shotgun is distributed as a gem package at rubygems\.org:
127
+ .
128
+ .P
129
+ \fIhttp://rubygems\.org/gems/shotgun\fR
130
+ .
131
+ .br
132
+ \fBgem install shotgun\fR
133
+ .
134
+ .P
135
+ The \fBrack\fR package is required\. The \fBmongrel\fR package is recommended\.
136
+ .
137
+ .SH "CONTRIBUTING"
138
+ Fork and report issues at github\.com:
139
+ .
140
+ .P
141
+ \fIhttp://github\.com/rtomayko/shotgun/\fR
142
+ .
143
+ .br
144
+ \fBgit clone git://github\.com/rtomayko/shotgun\.git\fR
145
+ .
146
+ .SH "VERSION HISTORY"
147
+ .
148
+ .SS "Version 0\.8 (2010 June 24)"
149
+ .
150
+ .IP "\(bu" 4
151
+ \fIhttp://github\.com/rtomayko/shotgun/compare/0\.7\.\.\.0\.8\fR
152
+ .
153
+ .IP "\(bu" 4
154
+ Preloading support\. The \fBshotgun\.rb\fR or \fBconfig/shotgun\.rb\fR file is loaded at startup and may require libraries and register callbacks for fork events\. See the section on \fIPRELOADING\fR\.
155
+ .
156
+ .IP "\(bu" 4
157
+ Fix starting with the Thin handler (\fBshotgun \-s thin\fR)
158
+ .
159
+ .IP "\(bu" 4
160
+ Actually include the shotgun(1) roff manual\.
161
+ .
162
+ .IP "" 0
163
+ .
164
+ .SS "Version 0\.7 (2010 June 22)"
165
+ .
166
+ .IP "\(bu" 4
167
+ \fIhttp://github\.com/rtomayko/shotgun/compare/0\.6\.\.\.0\.7\fR
168
+ .
169
+ .IP "\(bu" 4
170
+ Static files now served from the shotgun master process, making shotgun tolerable for apps with many/unbundled static assets\.
171
+ .
172
+ .IP "\(bu" 4
173
+ Added \fB\-\-public\fR (\fB\-P\fR) for specifying a non\-standard root / public directory\.
174
+ .
175
+ .IP "\(bu" 4
176
+ Response bodies are now streamed over the master < worker pipe instead of being marshalled\. Improves performance with large response bodies, and reduces shotgun master process RES usage\.
177
+ .
178
+ .IP "\(bu" 4
179
+ GET /favicon\.ico requests are served an empty response by the shotgun master process\. Prevents the need to fork a worker process\.
180
+ .
181
+ .IP "\(bu" 4
182
+ \fBINT\fR, \fBTERM\fR, \fBQUIT\fR now properly trigger server shutdown\. The second \fBINT\fR, \fBTERM\fR, \fBQUIT\fR causes the master process to exit hard\.
183
+ .
184
+ .IP "\(bu" 4
185
+ Non \fB\.ru\fR config files (e\.g\., sinatra app files) may now define command line options in the same way as \fB\.ru\fR files: by including a \fB#\e \-p 5555 \.\.\.\fR line\.
186
+ .
187
+ .IP "" 0
188
+ .
189
+ .SS "Versions < 0\.7 (2009\-2010)"
190
+ .
191
+ .IP "\(bu" 4
192
+ \fIhttp://github\.com/rtomayko/shotgun/commits/0\.6\fR
193
+ .
194
+ .IP "" 0
195
+ .
196
+ .SH "SEE ALSO"
197
+ ruby(1)
@@ -57,10 +57,16 @@ control server behavior:
57
57
 
58
58
  Ruby environment related options:
59
59
 
60
+ * `-r`, `--require` <library>:
61
+ Require <library> before loading the application and starting the server.
62
+ This can be used to load a portion of the application code in the master
63
+ process so it doesn't need to be loaded in the child. See [PRELOADING]][]
64
+ for more information on this approach.
65
+
60
66
  * `-e`, `--eval` <command>:
61
- Evaluate arbitrary <command> within the Ruby interpreter. <command> is
62
- evaluated as program arguments are parsed. Multiple `-e` arguments are
63
- allowed.
67
+ Evaluate arbitrary <command> within the shotgun master Ruby interpreter.
68
+ <command> is evaluated as program arguments are parsed. Multiple `-e`
69
+ arguments are allowed.
64
70
 
65
71
  * `-d`, `--debug`:
66
72
  Turns on debug mode. `$DEBUG` will be set `true`.
@@ -72,9 +78,6 @@ Ruby environment related options:
72
78
  * `-I`, `--include` <path>:
73
79
  Add <path> to the Ruby load path (`$LOAD_PATH`). May be used more than once.
74
80
 
75
- * `-r`, `--require` <library>:
76
- Require <library> before loading the application and starting the server.
77
-
78
81
  Miscellaneous:
79
82
 
80
83
  * `-h`, `--help`:
@@ -83,6 +86,50 @@ Miscellaneous:
83
86
  * `--version`:
84
87
  Show the Rack version and exit.
85
88
 
89
+ ## PRELOADING
90
+
91
+ It's possible to load support libraries and portions of the application in the
92
+ shotgun master process to reduce the amount of work that needs to be done for
93
+ each request in worker processes. There's two ways of accomplishing this: either
94
+ by specifying one or more `--require` (`-r`) arguments or through the use of a
95
+ `shotgun.rb` file.
96
+
97
+ During start up, shotgun looks for a `shotgun.rb` or `config/shotgun.rb` file.
98
+ If either file is found, it's loaded into the shotgun master process. Code at
99
+ the top-level of the `shotgun.rb` is run once on startup, so just require
100
+ whatever you want to preload. It's also possible to register callbacks to run
101
+ before each request in either the master or child worker process:
102
+
103
+ * `after_fork {` <stuff> `}`:
104
+ Run <stuff> in the shotgun child worker process immediately after forking.
105
+ Any files or socket connections opened in the master process should be
106
+ closed / re-established by an `after_fork` block.
107
+
108
+ * `before_fork {` <stuff> `}`:
109
+ Run <stuff> in the shotgun master process on each request before forking
110
+ the child worker process. This is typically less useful than `after_fork`,
111
+ but provided for completeness.
112
+
113
+ Example `config/shotgun.rb` file from the main github.com rails project:
114
+
115
+ # make sure the load path includes RAILS_ROOT
116
+ ENV['RAILS_ROOT'] ||= File.expand_path('../..', __FILE__)
117
+ $:.unshift ENV['RAILS_ROOT']
118
+
119
+ # bring in the base rails environment and some libraries
120
+ require 'config/environment'
121
+ require 'google-charts'
122
+ require 'aws-s3'
123
+
124
+ # disable Rails's built in class reloading
125
+ Rails.configuration.cache_classes = true
126
+
127
+ # reset database and redis connections in workers
128
+ after_fork do
129
+ ActiveRecord::Base.establish_connection
130
+ CHIMNEY.client = $redis
131
+ end
132
+
86
133
  ## INSTALLING
87
134
 
88
135
  Shotgun is distributed as a gem package at rubygems.org:
@@ -101,9 +148,21 @@ Fork and report issues at github.com:
101
148
 
102
149
  ## VERSION HISTORY
103
150
 
104
- ### Version 0.7 (unreleased)
151
+ ### Version 0.8 (2010 June 24)
152
+
153
+ * <http://github.com/rtomayko/shotgun/compare/0.7...0.8>
154
+
155
+ * Preloading support. The `shotgun.rb` or `config/shotgun.rb` file is
156
+ loaded at startup and may require libraries and register callbacks
157
+ for fork events. See the section on [PRELOADING][].
158
+
159
+ * Fix starting with the Thin handler (`shotgun -s thin`)
160
+
161
+ * Actually include the shotgun(1) roff manual.
162
+
163
+ ### Version 0.7 (2010 June 22)
105
164
 
106
- * <http://github.com/rtomayko/shotgun/compare/0.6...master>
165
+ * <http://github.com/rtomayko/shotgun/compare/0.6...0.7>
107
166
 
108
167
  * Static files now served from the shotgun master process, making
109
168
  shotgun tolerable for apps with many/unbundled static assets.
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'shotgun'
3
- s.version = '0.7'
4
- s.date = '2010-06-22'
3
+ s.version = '0.8'
4
+ s.date = '2010-06-23'
5
5
 
6
6
  s.description = "reloading rack development server"
7
7
  s.summary = s.description
@@ -19,6 +19,7 @@ Gem::Specification.new do |s|
19
19
  lib/shotgun/loader.rb
20
20
  lib/shotgun/static.rb
21
21
  man/index.txt
22
+ man/shotgun.1
22
23
  man/shotgun.1.ronn
23
24
  shotgun.gemspec
24
25
  test/big.ru
@@ -6,7 +6,7 @@ class SlowResponse
6
6
  def each
7
7
  yield "<pre>"
8
8
  10.times do
9
- yield('.' * 10) + "\n")
9
+ yield(('.' * 10) + "\n")
10
10
  sleep 0.5
11
11
  end
12
12
  yield "</pre>"
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shotgun
3
3
  version: !ruby/object:Gem::Version
4
- hash: 5
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 7
9
- version: "0.7"
8
+ - 8
9
+ version: "0.8"
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ryan Tomayko
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-06-22 00:00:00 -07:00
17
+ date: 2010-06-23 00:00:00 -07:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -50,6 +50,7 @@ files:
50
50
  - lib/shotgun/loader.rb
51
51
  - lib/shotgun/static.rb
52
52
  - man/index.txt
53
+ - man/shotgun.1
53
54
  - man/shotgun.1.ronn
54
55
  - shotgun.gemspec
55
56
  - test/big.ru