preplay_unicorn 4.3.1.2.gc450

Sign up to get free protection for your applications and to get access to all the features.
Files changed (259) hide show
  1. data/.CHANGELOG.old +25 -0
  2. data/.document +29 -0
  3. data/.gitignore +24 -0
  4. data/.mailmap +26 -0
  5. data/.manifest +258 -0
  6. data/.wrongdoc.yml +10 -0
  7. data/Application_Timeouts +77 -0
  8. data/CONTRIBUTORS +35 -0
  9. data/COPYING +674 -0
  10. data/ChangeLog +3839 -0
  11. data/DESIGN +97 -0
  12. data/Documentation/.gitignore +5 -0
  13. data/Documentation/GNUmakefile +30 -0
  14. data/Documentation/unicorn.1.txt +174 -0
  15. data/Documentation/unicorn_rails.1.txt +175 -0
  16. data/FAQ +53 -0
  17. data/GIT-VERSION-FILE +1 -0
  18. data/GIT-VERSION-GEN +40 -0
  19. data/GNUmakefile +294 -0
  20. data/HACKING +134 -0
  21. data/ISSUES +36 -0
  22. data/KNOWN_ISSUES +79 -0
  23. data/LATEST +15 -0
  24. data/LICENSE +64 -0
  25. data/Links +56 -0
  26. data/NEWS +1886 -0
  27. data/PHILOSOPHY +145 -0
  28. data/README +149 -0
  29. data/Rakefile +97 -0
  30. data/SIGNALS +114 -0
  31. data/Sandbox +96 -0
  32. data/TODO +5 -0
  33. data/TUNING +98 -0
  34. data/bin/unicorn +121 -0
  35. data/bin/unicorn_rails +209 -0
  36. data/examples/big_app_gc.rb +2 -0
  37. data/examples/echo.ru +27 -0
  38. data/examples/git.ru +13 -0
  39. data/examples/init.sh +74 -0
  40. data/examples/logger_mp_safe.rb +25 -0
  41. data/examples/logrotate.conf +29 -0
  42. data/examples/nginx.conf +156 -0
  43. data/examples/unicorn.conf.minimal.rb +13 -0
  44. data/examples/unicorn.conf.rb +94 -0
  45. data/ext/unicorn_http/CFLAGS +13 -0
  46. data/ext/unicorn_http/c_util.h +124 -0
  47. data/ext/unicorn_http/common_field_optimization.h +111 -0
  48. data/ext/unicorn_http/ext_help.h +86 -0
  49. data/ext/unicorn_http/extconf.rb +10 -0
  50. data/ext/unicorn_http/global_variables.h +97 -0
  51. data/ext/unicorn_http/httpdate.c +82 -0
  52. data/ext/unicorn_http/unicorn_http.c +4031 -0
  53. data/ext/unicorn_http/unicorn_http.rl +1036 -0
  54. data/ext/unicorn_http/unicorn_http_common.rl +76 -0
  55. data/lib/unicorn/app/exec_cgi.rb +154 -0
  56. data/lib/unicorn/app/inetd.rb +109 -0
  57. data/lib/unicorn/app/old_rails/static.rb +59 -0
  58. data/lib/unicorn/app/old_rails.rb +35 -0
  59. data/lib/unicorn/cgi_wrapper.rb +147 -0
  60. data/lib/unicorn/configurator.rb +641 -0
  61. data/lib/unicorn/const.rb +40 -0
  62. data/lib/unicorn/http_request.rb +77 -0
  63. data/lib/unicorn/http_response.rb +45 -0
  64. data/lib/unicorn/http_server.rb +748 -0
  65. data/lib/unicorn/launcher.rb +62 -0
  66. data/lib/unicorn/oob_gc.rb +71 -0
  67. data/lib/unicorn/preread_input.rb +33 -0
  68. data/lib/unicorn/socket_helper.rb +208 -0
  69. data/lib/unicorn/ssl_client.rb +11 -0
  70. data/lib/unicorn/ssl_configurator.rb +104 -0
  71. data/lib/unicorn/ssl_server.rb +42 -0
  72. data/lib/unicorn/stream_input.rb +149 -0
  73. data/lib/unicorn/tee_input.rb +126 -0
  74. data/lib/unicorn/tmpio.rb +29 -0
  75. data/lib/unicorn/util.rb +68 -0
  76. data/lib/unicorn/worker.rb +88 -0
  77. data/lib/unicorn.rb +107 -0
  78. data/local.mk.sample +59 -0
  79. data/man/man1/unicorn.1 +201 -0
  80. data/man/man1/unicorn_rails.1 +208 -0
  81. data/preplay_unicorn.gemspec +44 -0
  82. data/script/isolate_for_tests +50 -0
  83. data/setup.rb +1586 -0
  84. data/t/.gitignore +5 -0
  85. data/t/GNUmakefile +82 -0
  86. data/t/README +42 -0
  87. data/t/bin/content-md5-put +36 -0
  88. data/t/bin/sha1sum.rb +17 -0
  89. data/t/bin/unused_listen +40 -0
  90. data/t/bin/utee +12 -0
  91. data/t/broken-app.ru +12 -0
  92. data/t/detach.ru +11 -0
  93. data/t/env.ru +3 -0
  94. data/t/heartbeat-timeout.ru +12 -0
  95. data/t/my-tap-lib.sh +201 -0
  96. data/t/oob_gc.ru +21 -0
  97. data/t/oob_gc_path.ru +21 -0
  98. data/t/pid.ru +3 -0
  99. data/t/preread_input.ru +17 -0
  100. data/t/rack-input-tests.ru +21 -0
  101. data/t/rails3-app/.gitignore +4 -0
  102. data/t/rails3-app/Gemfile +26 -0
  103. data/t/rails3-app/Rakefile +10 -0
  104. data/t/rails3-app/app/controllers/application_controller.rb +4 -0
  105. data/t/rails3-app/app/helpers/application_helper.rb +2 -0
  106. data/t/rails3-app/app/views/layouts/application.html.erb +14 -0
  107. data/t/rails3-app/config/application.rb +46 -0
  108. data/t/rails3-app/config/boot.rb +6 -0
  109. data/t/rails3-app/config/database.yml +22 -0
  110. data/t/rails3-app/config/environment.rb +5 -0
  111. data/t/rails3-app/config/environments/development.rb +19 -0
  112. data/t/rails3-app/config/environments/production.rb +42 -0
  113. data/t/rails3-app/config/environments/test.rb +32 -0
  114. data/t/rails3-app/config/initializers/backtrace_silencers.rb +7 -0
  115. data/t/rails3-app/config/initializers/inflections.rb +10 -0
  116. data/t/rails3-app/config/initializers/mime_types.rb +5 -0
  117. data/t/rails3-app/config/initializers/secret_token.rb +7 -0
  118. data/t/rails3-app/config/initializers/session_store.rb +8 -0
  119. data/t/rails3-app/config/locales/en.yml +5 -0
  120. data/t/rails3-app/config/routes.rb +58 -0
  121. data/t/rails3-app/config.ru +4 -0
  122. data/t/rails3-app/db/seeds.rb +7 -0
  123. data/t/rails3-app/doc/README_FOR_APP +2 -0
  124. data/t/rails3-app/lib/tasks/.gitkeep +0 -0
  125. data/t/rails3-app/public/404.html +1 -0
  126. data/t/rails3-app/public/500.html +1 -0
  127. data/t/rails3-app/public/x.txt +1 -0
  128. data/t/rails3-app/script/rails +9 -0
  129. data/t/rails3-app/test/performance/browsing_test.rb +9 -0
  130. data/t/rails3-app/test/test_helper.rb +13 -0
  131. data/t/rails3-app/vendor/plugins/.gitkeep +0 -0
  132. data/t/sslgen.sh +71 -0
  133. data/t/t0000-http-basic.sh +50 -0
  134. data/t/t0001-reload-bad-config.sh +53 -0
  135. data/t/t0002-config-conflict.sh +49 -0
  136. data/t/t0002-parser-error.sh +94 -0
  137. data/t/t0003-working_directory.sh +51 -0
  138. data/t/t0004-heartbeat-timeout.sh +69 -0
  139. data/t/t0004-working_directory_broken.sh +24 -0
  140. data/t/t0005-working_directory_app.rb.sh +37 -0
  141. data/t/t0006-reopen-logs.sh +83 -0
  142. data/t/t0006.ru +13 -0
  143. data/t/t0007-working_directory_no_embed_cli.sh +44 -0
  144. data/t/t0008-back_out_of_upgrade.sh +110 -0
  145. data/t/t0009-broken-app.sh +56 -0
  146. data/t/t0009-winch_ttin.sh +59 -0
  147. data/t/t0010-reap-logging.sh +55 -0
  148. data/t/t0011-active-unix-socket.sh +79 -0
  149. data/t/t0012-reload-empty-config.sh +85 -0
  150. data/t/t0013-rewindable-input-false.sh +24 -0
  151. data/t/t0013.ru +12 -0
  152. data/t/t0014-rewindable-input-true.sh +24 -0
  153. data/t/t0014.ru +12 -0
  154. data/t/t0015-configurator-internals.sh +25 -0
  155. data/t/t0016-trust-x-forwarded-false.sh +30 -0
  156. data/t/t0017-trust-x-forwarded-true.sh +30 -0
  157. data/t/t0018-write-on-close.sh +23 -0
  158. data/t/t0019-max_header_len.sh +49 -0
  159. data/t/t0020-at_exit-handler.sh +49 -0
  160. data/t/t0021-process_detach.sh +29 -0
  161. data/t/t0100-rack-input-tests.sh +124 -0
  162. data/t/t0116-client_body_buffer_size.sh +80 -0
  163. data/t/t0116.ru +16 -0
  164. data/t/t0300-rails3-basic.sh +28 -0
  165. data/t/t0301-rails3-missing-config-ru.sh +33 -0
  166. data/t/t0302-rails3-alt-working_directory.sh +32 -0
  167. data/t/t0303-rails3-alt-working_directory_config.ru.sh +56 -0
  168. data/t/t0304-rails3-alt-working_directory_no_embed_cli.sh +52 -0
  169. data/t/t0600-https-server-basic.sh +48 -0
  170. data/t/t9000-preread-input.sh +48 -0
  171. data/t/t9001-oob_gc.sh +47 -0
  172. data/t/t9002-oob_gc-path.sh +75 -0
  173. data/t/test-lib.sh +113 -0
  174. data/t/test-rails3.sh +27 -0
  175. data/t/write-on-close.ru +11 -0
  176. data/test/aggregate.rb +15 -0
  177. data/test/benchmark/README +50 -0
  178. data/test/benchmark/dd.ru +18 -0
  179. data/test/benchmark/stack.ru +8 -0
  180. data/test/exec/README +5 -0
  181. data/test/exec/test_exec.rb +1055 -0
  182. data/test/rails/app-1.2.3/.gitignore +2 -0
  183. data/test/rails/app-1.2.3/Rakefile +7 -0
  184. data/test/rails/app-1.2.3/app/controllers/application.rb +6 -0
  185. data/test/rails/app-1.2.3/app/controllers/foo_controller.rb +36 -0
  186. data/test/rails/app-1.2.3/app/helpers/application_helper.rb +4 -0
  187. data/test/rails/app-1.2.3/config/boot.rb +11 -0
  188. data/test/rails/app-1.2.3/config/database.yml +12 -0
  189. data/test/rails/app-1.2.3/config/environment.rb +13 -0
  190. data/test/rails/app-1.2.3/config/environments/development.rb +9 -0
  191. data/test/rails/app-1.2.3/config/environments/production.rb +5 -0
  192. data/test/rails/app-1.2.3/config/routes.rb +6 -0
  193. data/test/rails/app-1.2.3/db/.gitignore +0 -0
  194. data/test/rails/app-1.2.3/log/.gitignore +1 -0
  195. data/test/rails/app-1.2.3/public/404.html +1 -0
  196. data/test/rails/app-1.2.3/public/500.html +1 -0
  197. data/test/rails/app-2.0.2/.gitignore +2 -0
  198. data/test/rails/app-2.0.2/Rakefile +7 -0
  199. data/test/rails/app-2.0.2/app/controllers/application.rb +4 -0
  200. data/test/rails/app-2.0.2/app/controllers/foo_controller.rb +36 -0
  201. data/test/rails/app-2.0.2/app/helpers/application_helper.rb +4 -0
  202. data/test/rails/app-2.0.2/config/boot.rb +11 -0
  203. data/test/rails/app-2.0.2/config/database.yml +12 -0
  204. data/test/rails/app-2.0.2/config/environment.rb +17 -0
  205. data/test/rails/app-2.0.2/config/environments/development.rb +8 -0
  206. data/test/rails/app-2.0.2/config/environments/production.rb +5 -0
  207. data/test/rails/app-2.0.2/config/routes.rb +6 -0
  208. data/test/rails/app-2.0.2/db/.gitignore +0 -0
  209. data/test/rails/app-2.0.2/log/.gitignore +1 -0
  210. data/test/rails/app-2.0.2/public/404.html +1 -0
  211. data/test/rails/app-2.0.2/public/500.html +1 -0
  212. data/test/rails/app-2.1.2/.gitignore +2 -0
  213. data/test/rails/app-2.1.2/Rakefile +7 -0
  214. data/test/rails/app-2.1.2/app/controllers/application.rb +4 -0
  215. data/test/rails/app-2.1.2/app/controllers/foo_controller.rb +36 -0
  216. data/test/rails/app-2.1.2/app/helpers/application_helper.rb +4 -0
  217. data/test/rails/app-2.1.2/config/boot.rb +111 -0
  218. data/test/rails/app-2.1.2/config/database.yml +12 -0
  219. data/test/rails/app-2.1.2/config/environment.rb +17 -0
  220. data/test/rails/app-2.1.2/config/environments/development.rb +7 -0
  221. data/test/rails/app-2.1.2/config/environments/production.rb +5 -0
  222. data/test/rails/app-2.1.2/config/routes.rb +6 -0
  223. data/test/rails/app-2.1.2/db/.gitignore +0 -0
  224. data/test/rails/app-2.1.2/log/.gitignore +1 -0
  225. data/test/rails/app-2.1.2/public/404.html +1 -0
  226. data/test/rails/app-2.1.2/public/500.html +1 -0
  227. data/test/rails/app-2.2.2/.gitignore +2 -0
  228. data/test/rails/app-2.2.2/Rakefile +7 -0
  229. data/test/rails/app-2.2.2/app/controllers/application.rb +4 -0
  230. data/test/rails/app-2.2.2/app/controllers/foo_controller.rb +36 -0
  231. data/test/rails/app-2.2.2/app/helpers/application_helper.rb +4 -0
  232. data/test/rails/app-2.2.2/config/boot.rb +111 -0
  233. data/test/rails/app-2.2.2/config/database.yml +12 -0
  234. data/test/rails/app-2.2.2/config/environment.rb +17 -0
  235. data/test/rails/app-2.2.2/config/environments/development.rb +7 -0
  236. data/test/rails/app-2.2.2/config/environments/production.rb +5 -0
  237. data/test/rails/app-2.2.2/config/routes.rb +6 -0
  238. data/test/rails/app-2.2.2/db/.gitignore +0 -0
  239. data/test/rails/app-2.2.2/log/.gitignore +1 -0
  240. data/test/rails/app-2.2.2/public/404.html +1 -0
  241. data/test/rails/app-2.2.2/public/500.html +1 -0
  242. data/test/rails/test_rails.rb +287 -0
  243. data/test/test_helper.rb +300 -0
  244. data/test/unit/test_configurator.rb +158 -0
  245. data/test/unit/test_droplet.rb +28 -0
  246. data/test/unit/test_http_parser.rb +860 -0
  247. data/test/unit/test_http_parser_ng.rb +716 -0
  248. data/test/unit/test_http_parser_xftrust.rb +38 -0
  249. data/test/unit/test_request.rb +197 -0
  250. data/test/unit/test_response.rb +99 -0
  251. data/test/unit/test_server.rb +289 -0
  252. data/test/unit/test_signals.rb +207 -0
  253. data/test/unit/test_sni_hostnames.rb +47 -0
  254. data/test/unit/test_socket_helper.rb +192 -0
  255. data/test/unit/test_stream_input.rb +204 -0
  256. data/test/unit/test_tee_input.rb +296 -0
  257. data/test/unit/test_upload.rb +306 -0
  258. data/test/unit/test_util.rb +100 -0
  259. metadata +428 -0
data/TUNING ADDED
@@ -0,0 +1,98 @@
1
+ = Tuning \Unicorn
2
+
3
+ \Unicorn performance is generally as good as a (mostly) Ruby web server
4
+ can provide. Most often the performance bottleneck is in the web
5
+ application running on Unicorn rather than Unicorn itself.
6
+
7
+ == \Unicorn Configuration
8
+
9
+ See Unicorn::Configurator for details on the config file format.
10
+ +worker_processes+ is the most-commonly needed tuning parameter.
11
+
12
+ === Unicorn::Configurator#worker_processes
13
+
14
+ * worker_processes should be scaled to the number of processes your
15
+ backend system(s) can support. DO NOT scale it to the number of
16
+ external network clients your application expects to be serving.
17
+ \Unicorn is NOT for serving slow clients, that is the job of nginx.
18
+
19
+ * worker_processes should be *at* *least* the number of CPU cores on
20
+ a dedicated server. If your application has occasionally slow
21
+ responses that are /not/ CPU-intensive, you may increase this to
22
+ workaround those inefficiencies.
23
+
24
+ * worker_processes may be increased for Unicorn::OobGC users to provide
25
+ more consistent response times.
26
+
27
+ * Never, ever, increase worker_processes to the point where the system
28
+ runs out of physical memory and hits swap. Production servers should
29
+ never see heavy swap activity.
30
+
31
+ === Unicorn::Configurator#listen Options
32
+
33
+ * Setting a very low value for the :backlog parameter in "listen"
34
+ directives can allow failover to happen more quickly if your
35
+ cluster is configured for it.
36
+
37
+ * If you're doing extremely simple benchmarks and getting connection
38
+ errors under high request rates, increasing your :backlog parameter
39
+ above the already-generous default of 1024 can help avoid connection
40
+ errors. Keep in mind this is not recommended for real traffic if
41
+ you have another machine to failover to (see above).
42
+
43
+ * :rcvbuf and :sndbuf parameters generally do not need to be set for TCP
44
+ listeners under Linux 2.6 because auto-tuning is enabled. UNIX domain
45
+ sockets do not have auto-tuning buffer sizes; so increasing those will
46
+ allow syscalls and task switches to be saved for larger requests
47
+ and responses. If your app only generates small responses or expects
48
+ small requests, you may shrink the buffer sizes to save memory, too.
49
+
50
+ * Having socket buffers too large can also be detrimental or have
51
+ little effect. Huge buffers can put more pressure on the allocator
52
+ and may also thrash CPU caches, cancelling out performance gains
53
+ one would normally expect.
54
+
55
+ * UNIX domain sockets are slightly faster than TCP sockets, but only
56
+ work if nginx is on the same machine.
57
+
58
+ == Other \Unicorn settings
59
+
60
+ * Setting "preload_app true" can allow copy-on-write-friendly GC to
61
+ be used to save memory. It will probably not work out of the box with
62
+ applications that open sockets or perform random I/O on files.
63
+ Databases like TokyoCabinet use concurrency-safe pread()/pwrite()
64
+ functions for safe sharing of database file descriptors across
65
+ processes.
66
+
67
+ * On POSIX-compliant filesystems, it is safe for multiple threads or
68
+ processes to append to one log file as long as all the processes are
69
+ have them unbuffered (File#sync = true) or they are
70
+ record(line)-buffered in userspace before any writes.
71
+
72
+ == Kernel Parameters (Linux sysctl)
73
+
74
+ WARNING: Do not change system parameters unless you know what you're doing!
75
+
76
+ * net.core.rmem_max and net.core.wmem_max can increase the allowed
77
+ size of :rcvbuf and :sndbuf respectively. This is mostly only useful
78
+ for UNIX domain sockets which do not have auto-tuning buffer sizes.
79
+
80
+ * For load testing/benchmarking with UNIX domain sockets, you should
81
+ consider increasing net.core.somaxconn or else nginx will start
82
+ failing to connect under heavy load. You may also consider setting
83
+ a higher :backlog to listen on as noted earlier.
84
+
85
+ * If you're running out of local ports, consider lowering
86
+ net.ipv4.tcp_fin_timeout to 20-30 (default: 60 seconds). Also
87
+ consider widening the usable port range by changing
88
+ net.ipv4.ip_local_port_range.
89
+
90
+ * Setting net.ipv4.tcp_timestamps=1 will also allow setting
91
+ net.ipv4.tcp_tw_reuse=1 and net.ipv4.tcp_tw_recycle=1, which along
92
+ with the above settings can slow down port exhaustion. Not all
93
+ networks are compatible with these settings, check with your friendly
94
+ network administrator before changing these.
95
+
96
+ * Increasing the MTU size can reduce framing overhead for larger
97
+ transfers. One often-overlooked detail is that the loopback
98
+ device (usually "lo") can have its MTU increased, too.
data/bin/unicorn ADDED
@@ -0,0 +1,121 @@
1
+ #!/this/will/be/overwritten/or/wrapped/anyways/do/not/worry/ruby
2
+ # -*- encoding: binary -*-
3
+ require 'unicorn/launcher'
4
+ require 'optparse'
5
+
6
+ ENV["RACK_ENV"] ||= "development"
7
+ rackup_opts = Unicorn::Configurator::RACKUP
8
+ options = rackup_opts[:options]
9
+
10
+ op = OptionParser.new("", 24, ' ') do |opts|
11
+ cmd = File.basename($0)
12
+ opts.banner = "Usage: #{cmd} " \
13
+ "[ruby options] [#{cmd} options] [rackup config file]"
14
+ opts.separator "Ruby options:"
15
+
16
+ lineno = 1
17
+ opts.on("-e", "--eval LINE", "evaluate a LINE of code") do |line|
18
+ eval line, TOPLEVEL_BINDING, "-e", lineno
19
+ lineno += 1
20
+ end
21
+
22
+ opts.on("-d", "--debug", "set debugging flags (set $DEBUG to true)") do
23
+ $DEBUG = true
24
+ end
25
+
26
+ opts.on("-w", "--warn", "turn warnings on for your script") do
27
+ $-w = true
28
+ end
29
+
30
+ opts.on("-I", "--include PATH",
31
+ "specify $LOAD_PATH (may be used more than once)") do |path|
32
+ $LOAD_PATH.unshift(*path.split(/:/))
33
+ end
34
+
35
+ opts.on("-r", "--require LIBRARY",
36
+ "require the library, before executing your script") do |library|
37
+ require library
38
+ end
39
+
40
+ opts.separator "#{cmd} options:"
41
+
42
+ # some of these switches exist for rackup command-line compatibility,
43
+
44
+ opts.on("-o", "--host HOST",
45
+ "listen on HOST (default: #{Unicorn::Const::DEFAULT_HOST})") do |h|
46
+ rackup_opts[:host] = h
47
+ rackup_opts[:set_listener] = true
48
+ end
49
+
50
+ opts.on("-p", "--port PORT",
51
+ "use PORT (default: #{Unicorn::Const::DEFAULT_PORT})") do |p|
52
+ rackup_opts[:port] = p.to_i
53
+ rackup_opts[:set_listener] = true
54
+ end
55
+
56
+ opts.on("-E", "--env RACK_ENV",
57
+ "use RACK_ENV for defaults (default: development)") do |e|
58
+ ENV["RACK_ENV"] = e
59
+ end
60
+
61
+ opts.on("-D", "--daemonize", "run daemonized in the background") do |d|
62
+ rackup_opts[:daemonize] = !!d
63
+ end
64
+
65
+ opts.on("-P", "--pid FILE", "DEPRECATED") do |f|
66
+ warn %q{Use of --pid/-P is strongly discouraged}
67
+ warn %q{Use the 'pid' directive in the Unicorn config file instead}
68
+ options[:pid] = f
69
+ end
70
+
71
+ opts.on("-s", "--server SERVER",
72
+ "this flag only exists for compatibility") do |s|
73
+ warn "-s/--server only exists for compatibility with rackup"
74
+ end
75
+
76
+ # Unicorn-specific stuff
77
+ opts.on("-l", "--listen {HOST:PORT|PATH}",
78
+ "listen on HOST:PORT or PATH",
79
+ "this may be specified multiple times",
80
+ "(default: #{Unicorn::Const::DEFAULT_LISTEN})") do |address|
81
+ options[:listeners] << address
82
+ end
83
+
84
+ opts.on("-c", "--config-file FILE", "Unicorn-specific config file") do |f|
85
+ options[:config_file] = f
86
+ end
87
+
88
+ # I'm avoiding Unicorn-specific config options on the command-line.
89
+ # IMNSHO, config options on the command-line are redundant given
90
+ # config files and make things unnecessarily complicated with multiple
91
+ # places to look for a config option.
92
+
93
+ opts.separator "Common options:"
94
+
95
+ opts.on_tail("-h", "--help", "Show this message") do
96
+ puts opts.to_s.gsub(/^.*DEPRECATED.*$/s, '')
97
+ exit
98
+ end
99
+
100
+ opts.on_tail("-v", "--version", "Show version") do
101
+ puts "#{cmd} v#{Unicorn::Const::UNICORN_VERSION}"
102
+ exit
103
+ end
104
+
105
+ opts.parse! ARGV
106
+ end
107
+
108
+ app = Unicorn.builder(ARGV[0] || 'config.ru', op)
109
+ op = nil
110
+
111
+ if $DEBUG
112
+ require 'pp'
113
+ pp({
114
+ :unicorn_options => options,
115
+ :app => app,
116
+ :daemonize => rackup_opts[:daemonize],
117
+ })
118
+ end
119
+
120
+ Unicorn::Launcher.daemonize!(options) if rackup_opts[:daemonize]
121
+ Unicorn::HttpServer.new(app, options).start.join
data/bin/unicorn_rails ADDED
@@ -0,0 +1,209 @@
1
+ #!/this/will/be/overwritten/or/wrapped/anyways/do/not/worry/ruby
2
+ # -*- encoding: binary -*-
3
+ require 'unicorn/launcher'
4
+ require 'optparse'
5
+ require 'fileutils'
6
+
7
+ ENV['RAILS_ENV'] ||= "development"
8
+ rackup_opts = Unicorn::Configurator::RACKUP
9
+ options = rackup_opts[:options]
10
+
11
+ op = OptionParser.new("", 24, ' ') do |opts|
12
+ cmd = File.basename($0)
13
+ opts.banner = "Usage: #{cmd} " \
14
+ "[ruby options] [#{cmd} options] [rackup config file]"
15
+ opts.separator "Ruby options:"
16
+
17
+ lineno = 1
18
+ opts.on("-e", "--eval LINE", "evaluate a LINE of code") do |line|
19
+ eval line, TOPLEVEL_BINDING, "-e", lineno
20
+ lineno += 1
21
+ end
22
+
23
+ opts.on("-d", "--debug", "set debugging flags (set $DEBUG to true)") do
24
+ $DEBUG = true
25
+ end
26
+
27
+ opts.on("-w", "--warn", "turn warnings on for your script") do
28
+ $-w = true
29
+ end
30
+
31
+ opts.on("-I", "--include PATH",
32
+ "specify $LOAD_PATH (may be used more than once)") do |path|
33
+ $LOAD_PATH.unshift(*path.split(/:/))
34
+ end
35
+
36
+ opts.on("-r", "--require LIBRARY",
37
+ "require the library, before executing your script") do |library|
38
+ require library
39
+ end
40
+
41
+ opts.separator "#{cmd} options:"
42
+
43
+ # some of these switches exist for rackup command-line compatibility,
44
+
45
+ opts.on("-o", "--host HOST",
46
+ "listen on HOST (default: #{Unicorn::Const::DEFAULT_HOST})") do |h|
47
+ rackup_opts[:host] = h
48
+ rackup_opts[:set_listener] = true
49
+ end
50
+
51
+ opts.on("-p", "--port PORT",
52
+ "use PORT (default: #{Unicorn::Const::DEFAULT_PORT})") do |p|
53
+ rackup_opts[:port] = p.to_i
54
+ rackup_opts[:set_listener] = true
55
+ end
56
+
57
+ opts.on("-E", "--env RAILS_ENV",
58
+ "use RAILS_ENV for defaults (default: development)") do |e|
59
+ ENV['RAILS_ENV'] = e
60
+ end
61
+
62
+ opts.on("-D", "--daemonize", "run daemonized in the background") do |d|
63
+ rackup_opts[:daemonize] = !!d
64
+ end
65
+
66
+ # Unicorn-specific stuff
67
+ opts.on("-l", "--listen {HOST:PORT|PATH}",
68
+ "listen on HOST:PORT or PATH",
69
+ "this may be specified multiple times",
70
+ "(default: #{Unicorn::Const::DEFAULT_LISTEN})") do |address|
71
+ options[:listeners] << address
72
+ end
73
+
74
+ opts.on("-c", "--config-file FILE", "Unicorn-specific config file") do |f|
75
+ options[:config_file] = f
76
+ end
77
+
78
+ opts.on("-P PATH", "DEPRECATED") do |v|
79
+ warn %q{Use of -P is ambiguous and discouraged}
80
+ warn %q{Use --path or RAILS_RELATIVE_URL_ROOT instead}
81
+ ENV['RAILS_RELATIVE_URL_ROOT'] = v
82
+ end
83
+
84
+ opts.on("--path PATH", "Runs Rails app mounted at a specific path.",
85
+ "(default: /)") do |v|
86
+ ENV['RAILS_RELATIVE_URL_ROOT'] = v
87
+ end
88
+
89
+ # I'm avoiding Unicorn-specific config options on the command-line.
90
+ # IMNSHO, config options on the command-line are redundant given
91
+ # config files and make things unnecessarily complicated with multiple
92
+ # places to look for a config option.
93
+
94
+ opts.separator "Common options:"
95
+
96
+ opts.on_tail("-h", "--help", "Show this message") do
97
+ puts opts.to_s.gsub(/^.*DEPRECATED.*$/s, '')
98
+ exit
99
+ end
100
+
101
+ opts.on_tail("-v", "--version", "Show version") do
102
+ puts "#{cmd} v#{Unicorn::Const::UNICORN_VERSION}"
103
+ exit
104
+ end
105
+
106
+ opts.parse! ARGV
107
+ end
108
+
109
+ def rails_dispatcher
110
+ if ::Rails::VERSION::MAJOR >= 3 && ::File.exist?('config/application.rb')
111
+ if ::File.read('config/application.rb') =~ /^module\s+([\w:]+)\s*$/
112
+ app_module = Object.const_get($1)
113
+ begin
114
+ result = app_module::Application
115
+ rescue NameError
116
+ end
117
+ end
118
+ end
119
+
120
+ if result.nil? && defined?(ActionController::Dispatcher)
121
+ result = ActionController::Dispatcher.new
122
+ end
123
+
124
+ result || abort("Unable to locate the application dispatcher class")
125
+ end
126
+
127
+ def rails_builder(ru, op, daemonize)
128
+ return Unicorn.builder(ru, op) if ru
129
+
130
+ # allow Configurator to parse cli switches embedded in the ru file
131
+ Unicorn::Configurator::RACKUP.update(:file => :rails, :optparse => op)
132
+
133
+ # this lambda won't run until after forking if preload_app is false
134
+ # this runs after config file reloading
135
+ lambda do ||
136
+ # Rails 3 includes a config.ru, use it if we find it after
137
+ # working_directory is bound.
138
+ ::File.exist?('config.ru') and
139
+ return Unicorn.builder('config.ru', op).call
140
+
141
+ # Load Rails and (possibly) the private version of Rack it bundles.
142
+ begin
143
+ require ::File.expand_path('config/boot')
144
+ require ::File.expand_path('config/environment')
145
+ rescue LoadError => err
146
+ abort "#$0 must be run inside RAILS_ROOT: #{err.inspect}"
147
+ end
148
+
149
+ defined?(::Rails::VERSION::STRING) or
150
+ abort "Rails::VERSION::STRING not defined by config/{boot,environment}"
151
+ # it seems Rails >=2.2 support Rack, but only >=2.3 requires it
152
+ old_rails = case ::Rails::VERSION::MAJOR
153
+ when 0, 1 then true
154
+ when 2 then Rails::VERSION::MINOR < 3 ? true : false
155
+ else
156
+ false
157
+ end
158
+
159
+ Rack::Builder.new do
160
+ map_path = ENV['RAILS_RELATIVE_URL_ROOT'] || '/'
161
+ if old_rails
162
+ if map_path != '/'
163
+ # patches + tests welcome, but I really cbf to deal with this
164
+ # since all apps I've ever dealt with just use "/" ...
165
+ warn "relative URL roots may not work for older Rails"
166
+ end
167
+ warn "LogTailer not available for Rails < 2.3" unless daemonize
168
+ warn "Debugger not available" if $DEBUG
169
+ require 'unicorn/app/old_rails'
170
+ map(map_path) do
171
+ use Unicorn::App::OldRails::Static
172
+ run Unicorn::App::OldRails.new
173
+ end
174
+ else
175
+ use Rails::Rack::LogTailer unless daemonize
176
+ use Rails::Rack::Debugger if $DEBUG
177
+ map(map_path) do
178
+ unless defined?(ActionDispatch::Static)
179
+ use Rails::Rack::Static
180
+ end
181
+ run rails_dispatcher
182
+ end
183
+ end
184
+ end.to_app
185
+ end
186
+ end
187
+
188
+ app = rails_builder(ARGV[0], op, rackup_opts[:daemonize])
189
+ op = nil
190
+
191
+ if $DEBUG
192
+ require 'pp'
193
+ pp({
194
+ :unicorn_options => options,
195
+ :app => app,
196
+ :daemonize => rackup_opts[:daemonize],
197
+ })
198
+ end
199
+
200
+ # ensure Rails standard tmp paths exist
201
+ options[:after_reload] = lambda do
202
+ FileUtils.mkdir_p(%w(cache pids sessions sockets).map! { |d| "tmp/#{d}" })
203
+ end
204
+
205
+ if rackup_opts[:daemonize]
206
+ options[:pid] = "tmp/pids/unicorn.pid"
207
+ Unicorn::Launcher.daemonize!(options)
208
+ end
209
+ Unicorn::HttpServer.new(app, options).start.join
@@ -0,0 +1,2 @@
1
+ # see {Unicorn::OobGC}[http://unicorn.bogomips.org/Unicorn/OobGC.html]
2
+ # Unicorn::OobGC was broken in Unicorn v3.3.1 - v3.6.1 and fixed in v3.6.2
data/examples/echo.ru ADDED
@@ -0,0 +1,27 @@
1
+ #\-E none
2
+ #
3
+ # Example application that echoes read data back to the HTTP client.
4
+ # This emulates the old echo protocol people used to run.
5
+ #
6
+ # An example of using this in a client would be to run:
7
+ # curl --no-buffer -T- http://host:port/
8
+ #
9
+ # Then type random stuff in your terminal to watch it get echoed back!
10
+
11
+ class EchoBody < Struct.new(:input)
12
+
13
+ def each(&block)
14
+ while buf = input.read(4096)
15
+ yield buf
16
+ end
17
+ self
18
+ end
19
+
20
+ end
21
+
22
+ use Rack::Chunked
23
+ run lambda { |env|
24
+ /\A100-continue\z/i =~ env['HTTP_EXPECT'] and return [100, {}, []]
25
+ [ 200, { 'Content-Type' => 'application/octet-stream' },
26
+ EchoBody.new(env['rack.input']) ]
27
+ }
data/examples/git.ru ADDED
@@ -0,0 +1,13 @@
1
+ #\-E none
2
+
3
+ # See http://thread.gmane.org/gmane.comp.web.curl.general/10473/raw on
4
+ # how to setup git for this. A better version of the above patch was
5
+ # accepted and committed on June 15, 2009, so you can pull the latest
6
+ # curl CVS snapshot to try this out.
7
+ require 'unicorn/app/inetd'
8
+
9
+ use Rack::Lint
10
+ use Rack::Chunked # important!
11
+ run Unicorn::App::Inetd.new(
12
+ *%w(git daemon --verbose --inetd --export-all --base-path=/home/ew/unicorn)
13
+ )
data/examples/init.sh ADDED
@@ -0,0 +1,74 @@
1
+ #!/bin/sh
2
+ set -e
3
+ # Example init script, this can be used with nginx, too,
4
+ # since nginx and unicorn accept the same signals
5
+
6
+ # Feel free to change any of the following variables for your app:
7
+ TIMEOUT=${TIMEOUT-60}
8
+ APP_ROOT=/home/x/my_app/current
9
+ PID=$APP_ROOT/tmp/pids/unicorn.pid
10
+ CMD="/usr/bin/unicorn -D -c $APP_ROOT/config/unicorn.rb"
11
+ INIT_CONF=$APP_ROOT/config/init.conf
12
+ action="$1"
13
+ set -u
14
+
15
+ test -f "$INIT_CONF" && . $INIT_CONF
16
+
17
+ old_pid="$PID.oldbin"
18
+
19
+ cd $APP_ROOT || exit 1
20
+
21
+ sig () {
22
+ test -s "$PID" && kill -$1 `cat $PID`
23
+ }
24
+
25
+ oldsig () {
26
+ test -s $old_pid && kill -$1 `cat $old_pid`
27
+ }
28
+
29
+ case $action in
30
+ start)
31
+ sig 0 && echo >&2 "Already running" && exit 0
32
+ $CMD
33
+ ;;
34
+ stop)
35
+ sig QUIT && exit 0
36
+ echo >&2 "Not running"
37
+ ;;
38
+ force-stop)
39
+ sig TERM && exit 0
40
+ echo >&2 "Not running"
41
+ ;;
42
+ restart|reload)
43
+ sig HUP && echo reloaded OK && exit 0
44
+ echo >&2 "Couldn't reload, starting '$CMD' instead"
45
+ $CMD
46
+ ;;
47
+ upgrade)
48
+ if sig USR2 && sleep 2 && sig 0 && oldsig QUIT
49
+ then
50
+ n=$TIMEOUT
51
+ while test -s $old_pid && test $n -ge 0
52
+ do
53
+ printf '.' && sleep 1 && n=$(( $n - 1 ))
54
+ done
55
+ echo
56
+
57
+ if test $n -lt 0 && test -s $old_pid
58
+ then
59
+ echo >&2 "$old_pid still exists after $TIMEOUT seconds"
60
+ exit 1
61
+ fi
62
+ exit 0
63
+ fi
64
+ echo >&2 "Couldn't upgrade, starting '$CMD' instead"
65
+ $CMD
66
+ ;;
67
+ reopen-logs)
68
+ sig USR1
69
+ ;;
70
+ *)
71
+ echo >&2 "Usage: $0 <start|stop|restart|upgrade|force-stop|reopen-logs>"
72
+ exit 1
73
+ ;;
74
+ esac
@@ -0,0 +1,25 @@
1
+ # Multi-Processing-safe monkey patch for Logger
2
+ #
3
+ # This monkey patch fixes the case where "preload_app true" is used and
4
+ # the application spawns a background thread upon being loaded.
5
+ #
6
+ # This removes all lock from the Logger code and solely relies on the
7
+ # underlying filesystem to handle write(2) system calls atomically when
8
+ # O_APPEND is used. This is safe in the presence of both multiple
9
+ # threads (native or green) and multiple processes when writing to
10
+ # a filesystem with POSIX O_APPEND semantics.
11
+ #
12
+ # It should be noted that the original locking on Logger could _never_ be
13
+ # considered reliable on non-POSIX filesystems with multiple processes,
14
+ # either, so nothing is lost in that case.
15
+
16
+ require 'logger'
17
+ class Logger::LogDevice
18
+ def write(message)
19
+ @dev.syswrite(message)
20
+ end
21
+
22
+ def close
23
+ @dev.close
24
+ end
25
+ end
@@ -0,0 +1,29 @@
1
+ # example logrotate config file, I usually keep this in
2
+ # /etc/logrotate.d/unicorn_app on my Debian systems
3
+ #
4
+ # See the logrotate(8) manpage for more information:
5
+ # http://linux.die.net/man/8/logrotate
6
+
7
+ # Modify the following glob to match the logfiles your app writes to:
8
+ /var/log/unicorn_app/*.log {
9
+ # this first block is mostly just personal preference, though
10
+ # I wish logrotate offered an "hourly" option...
11
+ daily
12
+ missingok
13
+ rotate 180
14
+ compress # must use with delaycompress below
15
+ dateext
16
+
17
+ # this is important if using "compress" since we need to call
18
+ # the "lastaction" script below before compressing:
19
+ delaycompress
20
+
21
+ # note the lack of the evil "copytruncate" option in this
22
+ # config. Unicorn supports the USR1 signal and we send it
23
+ # as our "lastaction" action:
24
+ lastaction
25
+ # assuming your pid file is in /var/run/unicorn_app/pid
26
+ pid=/var/run/unicorn_app/pid
27
+ test -s $pid && kill -USR1 "$(cat $pid)"
28
+ endscript
29
+ }