qilin 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,339 @@
1
+ GNU GENERAL PUBLIC LICENSE
2
+ Version 2, June 1991
3
+
4
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
5
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6
+ Everyone is permitted to copy and distribute verbatim copies
7
+ of this license document, but changing it is not allowed.
8
+
9
+ Preamble
10
+
11
+ The licenses for most software are designed to take away your
12
+ freedom to share and change it. By contrast, the GNU General Public
13
+ License is intended to guarantee your freedom to share and change free
14
+ software--to make sure the software is free for all its users. This
15
+ General Public License applies to most of the Free Software
16
+ Foundation's software and to any other program whose authors commit to
17
+ using it. (Some other Free Software Foundation software is covered by
18
+ the GNU Lesser General Public License instead.) You can apply it to
19
+ your programs, too.
20
+
21
+ When we speak of free software, we are referring to freedom, not
22
+ price. Our General Public Licenses are designed to make sure that you
23
+ have the freedom to distribute copies of free software (and charge for
24
+ this service if you wish), that you receive source code or can get it
25
+ if you want it, that you can change the software or use pieces of it
26
+ in new free programs; and that you know you can do these things.
27
+
28
+ To protect your rights, we need to make restrictions that forbid
29
+ anyone to deny you these rights or to ask you to surrender the rights.
30
+ These restrictions translate to certain responsibilities for you if you
31
+ distribute copies of the software, or if you modify it.
32
+
33
+ For example, if you distribute copies of such a program, whether
34
+ gratis or for a fee, you must give the recipients all the rights that
35
+ you have. You must make sure that they, too, receive or can get the
36
+ source code. And you must show them these terms so they know their
37
+ rights.
38
+
39
+ We protect your rights with two steps: (1) copyright the software, and
40
+ (2) offer you this license which gives you legal permission to copy,
41
+ distribute and/or modify the software.
42
+
43
+ Also, for each author's protection and ours, we want to make certain
44
+ that everyone understands that there is no warranty for this free
45
+ software. If the software is modified by someone else and passed on, we
46
+ want its recipients to know that what they have is not the original, so
47
+ that any problems introduced by others will not reflect on the original
48
+ authors' reputations.
49
+
50
+ Finally, any free program is threatened constantly by software
51
+ patents. We wish to avoid the danger that redistributors of a free
52
+ program will individually obtain patent licenses, in effect making the
53
+ program proprietary. To prevent this, we have made it clear that any
54
+ patent must be licensed for everyone's free use or not licensed at all.
55
+
56
+ The precise terms and conditions for copying, distribution and
57
+ modification follow.
58
+
59
+ GNU GENERAL PUBLIC LICENSE
60
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
61
+
62
+ 0. This License applies to any program or other work which contains
63
+ a notice placed by the copyright holder saying it may be distributed
64
+ under the terms of this General Public License. The "Program", below,
65
+ refers to any such program or work, and a "work based on the Program"
66
+ means either the Program or any derivative work under copyright law:
67
+ that is to say, a work containing the Program or a portion of it,
68
+ either verbatim or with modifications and/or translated into another
69
+ language. (Hereinafter, translation is included without limitation in
70
+ the term "modification".) Each licensee is addressed as "you".
71
+
72
+ Activities other than copying, distribution and modification are not
73
+ covered by this License; they are outside its scope. The act of
74
+ running the Program is not restricted, and the output from the Program
75
+ is covered only if its contents constitute a work based on the
76
+ Program (independent of having been made by running the Program).
77
+ Whether that is true depends on what the Program does.
78
+
79
+ 1. You may copy and distribute verbatim copies of the Program's
80
+ source code as you receive it, in any medium, provided that you
81
+ conspicuously and appropriately publish on each copy an appropriate
82
+ copyright notice and disclaimer of warranty; keep intact all the
83
+ notices that refer to this License and to the absence of any warranty;
84
+ and give any other recipients of the Program a copy of this License
85
+ along with the Program.
86
+
87
+ You may charge a fee for the physical act of transferring a copy, and
88
+ you may at your option offer warranty protection in exchange for a fee.
89
+
90
+ 2. You may modify your copy or copies of the Program or any portion
91
+ of it, thus forming a work based on the Program, and copy and
92
+ distribute such modifications or work under the terms of Section 1
93
+ above, provided that you also meet all of these conditions:
94
+
95
+ a) You must cause the modified files to carry prominent notices
96
+ stating that you changed the files and the date of any change.
97
+
98
+ b) You must cause any work that you distribute or publish, that in
99
+ whole or in part contains or is derived from the Program or any
100
+ part thereof, to be licensed as a whole at no charge to all third
101
+ parties under the terms of this License.
102
+
103
+ c) If the modified program normally reads commands interactively
104
+ when run, you must cause it, when started running for such
105
+ interactive use in the most ordinary way, to print or display an
106
+ announcement including an appropriate copyright notice and a
107
+ notice that there is no warranty (or else, saying that you provide
108
+ a warranty) and that users may redistribute the program under
109
+ these conditions, and telling the user how to view a copy of this
110
+ License. (Exception: if the Program itself is interactive but
111
+ does not normally print such an announcement, your work based on
112
+ the Program is not required to print an announcement.)
113
+
114
+ These requirements apply to the modified work as a whole. If
115
+ identifiable sections of that work are not derived from the Program,
116
+ and can be reasonably considered independent and separate works in
117
+ themselves, then this License, and its terms, do not apply to those
118
+ sections when you distribute them as separate works. But when you
119
+ distribute the same sections as part of a whole which is a work based
120
+ on the Program, the distribution of the whole must be on the terms of
121
+ this License, whose permissions for other licensees extend to the
122
+ entire whole, and thus to each and every part regardless of who wrote it.
123
+
124
+ Thus, it is not the intent of this section to claim rights or contest
125
+ your rights to work written entirely by you; rather, the intent is to
126
+ exercise the right to control the distribution of derivative or
127
+ collective works based on the Program.
128
+
129
+ In addition, mere aggregation of another work not based on the Program
130
+ with the Program (or with a work based on the Program) on a volume of
131
+ a storage or distribution medium does not bring the other work under
132
+ the scope of this License.
133
+
134
+ 3. You may copy and distribute the Program (or a work based on it,
135
+ under Section 2) in object code or executable form under the terms of
136
+ Sections 1 and 2 above provided that you also do one of the following:
137
+
138
+ a) Accompany it with the complete corresponding machine-readable
139
+ source code, which must be distributed under the terms of Sections
140
+ 1 and 2 above on a medium customarily used for software interchange; or,
141
+
142
+ b) Accompany it with a written offer, valid for at least three
143
+ years, to give any third party, for a charge no more than your
144
+ cost of physically performing source distribution, a complete
145
+ machine-readable copy of the corresponding source code, to be
146
+ distributed under the terms of Sections 1 and 2 above on a medium
147
+ customarily used for software interchange; or,
148
+
149
+ c) Accompany it with the information you received as to the offer
150
+ to distribute corresponding source code. (This alternative is
151
+ allowed only for noncommercial distribution and only if you
152
+ received the program in object code or executable form with such
153
+ an offer, in accord with Subsection b above.)
154
+
155
+ The source code for a work means the preferred form of the work for
156
+ making modifications to it. For an executable work, complete source
157
+ code means all the source code for all modules it contains, plus any
158
+ associated interface definition files, plus the scripts used to
159
+ control compilation and installation of the executable. However, as a
160
+ special exception, the source code distributed need not include
161
+ anything that is normally distributed (in either source or binary
162
+ form) with the major components (compiler, kernel, and so on) of the
163
+ operating system on which the executable runs, unless that component
164
+ itself accompanies the executable.
165
+
166
+ If distribution of executable or object code is made by offering
167
+ access to copy from a designated place, then offering equivalent
168
+ access to copy the source code from the same place counts as
169
+ distribution of the source code, even though third parties are not
170
+ compelled to copy the source along with the object code.
171
+
172
+ 4. You may not copy, modify, sublicense, or distribute the Program
173
+ except as expressly provided under this License. Any attempt
174
+ otherwise to copy, modify, sublicense or distribute the Program is
175
+ void, and will automatically terminate your rights under this License.
176
+ However, parties who have received copies, or rights, from you under
177
+ this License will not have their licenses terminated so long as such
178
+ parties remain in full compliance.
179
+
180
+ 5. You are not required to accept this License, since you have not
181
+ signed it. However, nothing else grants you permission to modify or
182
+ distribute the Program or its derivative works. These actions are
183
+ prohibited by law if you do not accept this License. Therefore, by
184
+ modifying or distributing the Program (or any work based on the
185
+ Program), you indicate your acceptance of this License to do so, and
186
+ all its terms and conditions for copying, distributing or modifying
187
+ the Program or works based on it.
188
+
189
+ 6. Each time you redistribute the Program (or any work based on the
190
+ Program), the recipient automatically receives a license from the
191
+ original licensor to copy, distribute or modify the Program subject to
192
+ these terms and conditions. You may not impose any further
193
+ restrictions on the recipients' exercise of the rights granted herein.
194
+ You are not responsible for enforcing compliance by third parties to
195
+ this License.
196
+
197
+ 7. If, as a consequence of a court judgment or allegation of patent
198
+ infringement or for any other reason (not limited to patent issues),
199
+ conditions are imposed on you (whether by court order, agreement or
200
+ otherwise) that contradict the conditions of this License, they do not
201
+ excuse you from the conditions of this License. If you cannot
202
+ distribute so as to satisfy simultaneously your obligations under this
203
+ License and any other pertinent obligations, then as a consequence you
204
+ may not distribute the Program at all. For example, if a patent
205
+ license would not permit royalty-free redistribution of the Program by
206
+ all those who receive copies directly or indirectly through you, then
207
+ the only way you could satisfy both it and this License would be to
208
+ refrain entirely from distribution of the Program.
209
+
210
+ If any portion of this section is held invalid or unenforceable under
211
+ any particular circumstance, the balance of the section is intended to
212
+ apply and the section as a whole is intended to apply in other
213
+ circumstances.
214
+
215
+ It is not the purpose of this section to induce you to infringe any
216
+ patents or other property right claims or to contest validity of any
217
+ such claims; this section has the sole purpose of protecting the
218
+ integrity of the free software distribution system, which is
219
+ implemented by public license practices. Many people have made
220
+ generous contributions to the wide range of software distributed
221
+ through that system in reliance on consistent application of that
222
+ system; it is up to the author/donor to decide if he or she is willing
223
+ to distribute software through any other system and a licensee cannot
224
+ impose that choice.
225
+
226
+ This section is intended to make thoroughly clear what is believed to
227
+ be a consequence of the rest of this License.
228
+
229
+ 8. If the distribution and/or use of the Program is restricted in
230
+ certain countries either by patents or by copyrighted interfaces, the
231
+ original copyright holder who places the Program under this License
232
+ may add an explicit geographical distribution limitation excluding
233
+ those countries, so that distribution is permitted only in or among
234
+ countries not thus excluded. In such case, this License incorporates
235
+ the limitation as if written in the body of this License.
236
+
237
+ 9. The Free Software Foundation may publish revised and/or new versions
238
+ of the General Public License from time to time. Such new versions will
239
+ be similar in spirit to the present version, but may differ in detail to
240
+ address new problems or concerns.
241
+
242
+ Each version is given a distinguishing version number. If the Program
243
+ specifies a version number of this License which applies to it and "any
244
+ later version", you have the option of following the terms and conditions
245
+ either of that version or of any later version published by the Free
246
+ Software Foundation. If the Program does not specify a version number of
247
+ this License, you may choose any version ever published by the Free Software
248
+ Foundation.
249
+
250
+ 10. If you wish to incorporate parts of the Program into other free
251
+ programs whose distribution conditions are different, write to the author
252
+ to ask for permission. For software which is copyrighted by the Free
253
+ Software Foundation, write to the Free Software Foundation; we sometimes
254
+ make exceptions for this. Our decision will be guided by the two goals
255
+ of preserving the free status of all derivatives of our free software and
256
+ of promoting the sharing and reuse of software generally.
257
+
258
+ NO WARRANTY
259
+
260
+ 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
261
+ FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
262
+ OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
263
+ PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
264
+ OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
265
+ MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
266
+ TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
267
+ PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
268
+ REPAIR OR CORRECTION.
269
+
270
+ 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
271
+ WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
272
+ REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
273
+ INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
274
+ OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
275
+ TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
276
+ YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
277
+ PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
278
+ POSSIBILITY OF SUCH DAMAGES.
279
+
280
+ END OF TERMS AND CONDITIONS
281
+
282
+ How to Apply These Terms to Your New Programs
283
+
284
+ If you develop a new program, and you want it to be of the greatest
285
+ possible use to the public, the best way to achieve this is to make it
286
+ free software which everyone can redistribute and change under these terms.
287
+
288
+ To do so, attach the following notices to the program. It is safest
289
+ to attach them to the start of each source file to most effectively
290
+ convey the exclusion of warranty; and each file should have at least
291
+ the "copyright" line and a pointer to where the full notice is found.
292
+
293
+ <one line to give the program's name and a brief idea of what it does.>
294
+ Copyright (C) <year> <name of author>
295
+
296
+ This program is free software; you can redistribute it and/or modify
297
+ it under the terms of the GNU General Public License as published by
298
+ the Free Software Foundation; either version 2 of the License, or
299
+ (at your option) any later version.
300
+
301
+ This program is distributed in the hope that it will be useful,
302
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
303
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
304
+ GNU General Public License for more details.
305
+
306
+ You should have received a copy of the GNU General Public License along
307
+ with this program; if not, write to the Free Software Foundation, Inc.,
308
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
309
+
310
+ Also add information on how to contact you by electronic and paper mail.
311
+
312
+ If the program is interactive, make it output a short notice like this
313
+ when it starts in an interactive mode:
314
+
315
+ Gnomovision version 69, Copyright (C) year name of author
316
+ Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
317
+ This is free software, and you are welcome to redistribute it
318
+ under certain conditions; type `show c' for details.
319
+
320
+ The hypothetical commands `show w' and `show c' should show the appropriate
321
+ parts of the General Public License. Of course, the commands you use may
322
+ be called something other than `show w' and `show c'; they could even be
323
+ mouse-clicks or menu items--whatever suits your program.
324
+
325
+ You should also get your employer (if you work as a programmer) or your
326
+ school, if any, to sign a "copyright disclaimer" for the program, if
327
+ necessary. Here is a sample; alter the names:
328
+
329
+ Yoyodyne, Inc., hereby disclaims all copyright interest in the program
330
+ `Gnomovision' (which makes passes at compilers) written by James Hacker.
331
+
332
+ <signature of Ty Coon>, 1 April 1989
333
+ Ty Coon, President of Vice
334
+
335
+ This General Public License does not permit incorporating your program into
336
+ proprietary programs. If your program is a subroutine library, you may
337
+ consider it more useful to permit linking proprietary applications with the
338
+ library. If this is what you want to do, use the GNU Lesser General
339
+ Public License instead of this License.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
data/bin/qilin ADDED
@@ -0,0 +1,85 @@
1
+ #!/this/will/be/overwritten/or/wrapped/anyways/do/not/worry/ruby
2
+ # -*- encoding: binary -*-
3
+ require 'qilin/launcher'
4
+ require 'optparse'
5
+
6
+ ENV["RACK_ENV"] ||= "development"
7
+ options = {}
8
+ daemonize = false
9
+
10
+ opts = OptionParser.new("", 24, ' ') do |opts|
11
+ cmd = File.basename($0)
12
+ opts.banner = "Usage: #{cmd} " \
13
+ "[ruby options] [#{cmd} options]"
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
+ opts.on("-E", "--env RACK_ENV",
43
+ "use RACK_ENV for defaults (default: development)") do |e|
44
+ ENV["RACK_ENV"] = e
45
+ end
46
+
47
+ opts.on("-D", "--daemonize", "run daemonized in the background") do |d|
48
+ daemonize = !!d
49
+ end
50
+
51
+ opts.on("-P", "--pid FILE", "DEPRECATED") do |f|
52
+ warn %q{Use of --pid/-P is strongly discouraged}
53
+ warn %q{Use the 'pid' directive in the Qilin config file instead}
54
+ options[:pid] = f
55
+ end
56
+
57
+ opts.on("-c", "--config-file FILE", "Qilin-specific config file") do |f|
58
+ options[:config_file] = f
59
+ end
60
+
61
+ opts.separator "Common options:"
62
+
63
+ opts.on_tail("-h", "--help", "Show this message") do
64
+ puts opts.to_s.gsub(/^.*DEPRECATED.*$/s, '')
65
+ exit
66
+ end
67
+
68
+ opts.on_tail("-v", "--version", "Show version") do
69
+ puts "#{cmd} v#{Qilin::VERSION}"
70
+ exit
71
+ end
72
+
73
+ opts.parse! ARGV
74
+ end
75
+
76
+ if $DEBUG
77
+ require 'pp'
78
+ pp({
79
+ :qilin_options => options,
80
+ :daemonize => daemonize,
81
+ })
82
+ end
83
+
84
+ Qilin::Launcher.daemonize!(options) if daemonize
85
+ Qilin.run(options)
data/lib/qilin.rb ADDED
@@ -0,0 +1,13 @@
1
+ module Qilin
2
+ def self.run(options = {})
3
+ Qilin::Manager.new(options).start.join
4
+ end
5
+ end
6
+
7
+ require 'fcntl'
8
+
9
+ require 'qilin/version'
10
+ require 'qilin/manager'
11
+ require 'qilin/worker'
12
+ require 'qilin/configurator'
13
+ require 'qilin/tmpio'
@@ -0,0 +1,267 @@
1
+ # -*- encoding: binary -*-
2
+ require 'logger'
3
+
4
+ # Implements a simple DSL for configuring a Qilin server.
5
+ #
6
+ # See http://unicorn.bogomips.org/examples/unicorn.conf.rb and
7
+ # http://unicorn.bogomips.org/examples/unicorn.conf.minimal.rb
8
+ # example configuration files. An example config file for use with
9
+ # nginx is also available at
10
+ # http://unicorn.bogomips.org/examples/nginx.conf
11
+ class Qilin::Configurator
12
+ include Qilin
13
+
14
+ # :stopdoc:
15
+ attr_accessor :set, :config_file
16
+
17
+ # Default settings for Qilin
18
+ DEFAULTS = {
19
+ :timeout => 300,
20
+ :logger => Logger.new($stderr),
21
+ :worker_processes => 1,
22
+ :after_fork => lambda { |parent, worker|
23
+ parent.logger.info("worker=#{worker.nr} spawned pid=#{$$}")
24
+ },
25
+ :before_fork => lambda { |parent, worker|
26
+ parent.logger.info("worker=#{worker.nr} spawning...")
27
+ },
28
+ :pid => nil,
29
+ :preload_app => false
30
+ }
31
+ #:startdoc:
32
+
33
+ def initialize(defaults = {}) #:nodoc:
34
+ self.set = Hash.new(:unset)
35
+ @use_defaults = defaults.delete(:use_defaults)
36
+ self.config_file = defaults.delete(:config_file)
37
+
38
+ set.merge!(DEFAULTS) if @use_defaults
39
+ defaults.each { |key, value| self.__send__(key, value) }
40
+ reload(false)
41
+ end
42
+
43
+ def reload(merge_defaults = true) #:nodoc:
44
+ if merge_defaults && @use_defaults
45
+ set.merge!(DEFAULTS) if @use_defaults
46
+ end
47
+ instance_eval(File.read(config_file), config_file) if config_file
48
+
49
+ # working_directory binds immediately (easier error checking that way),
50
+ # now ensure any paths we changed are correctly set.
51
+ [ :pid, :stderr_path, :stdout_path ].each do |var|
52
+ String === (path = set[var]) or next
53
+ path = File.expand_path(path)
54
+ File.writable?(path) || File.writable?(File.dirname(path)) or \
55
+ raise ArgumentError, "directory for #{var}=#{path} not writable"
56
+ end
57
+ end
58
+
59
+ def commit!(parent, options = {}) #:nodoc:
60
+ skip = options[:skip] || []
61
+ set.each do |key, value|
62
+ value == :unset and next
63
+ skip.include?(key) and next
64
+ parent.__send__("#{key}=", value)
65
+ end
66
+ end
67
+
68
+ def [](key) # :nodoc:
69
+ set[key]
70
+ end
71
+
72
+ # sets object to the +new+ Logger-like object. The new logger-like
73
+ # object must respond to the following methods:
74
+ # +debug+, +info+, +warn+, +error+, +fatal+
75
+ # The default Logger will log its output to the path specified
76
+ # by +stderr_path+. If you're running Qilin daemonized, then
77
+ # you must specify a path to prevent error messages from going
78
+ # to /dev/null.
79
+ def logger(new)
80
+ %w(debug info warn error fatal).each do |m|
81
+ new.respond_to?(m) and next
82
+ raise ArgumentError, "logger=#{new} does not respond to method=#{m}"
83
+ end
84
+
85
+ set[:logger] = new
86
+ end
87
+
88
+ # sets after_fork hook to a given block. This block will be called by
89
+ # the worker after forking. The following is an example hook which adds
90
+ # a per-process listener to every worker:
91
+ #
92
+ # after_fork do |parent,worker|
93
+ # # drop permissions to "www-data" in the worker
94
+ # # generally there's no reason to start Qilin as a priviledged user
95
+ # # as it is not recommended to expose Qilin to public clients.
96
+ # worker.user('www-data', 'www-data') if Process.euid == 0
97
+ # end
98
+ def after_fork(*args, &block)
99
+ set_hook(:after_fork, block_given? ? block : args[0])
100
+ end
101
+
102
+ # sets before_fork got be a given Proc object. This Proc
103
+ # object will be called by the master process before forking
104
+ # each worker.
105
+ def before_fork(*args, &block)
106
+ set_hook(:before_fork, block_given? ? block : args[0])
107
+ end
108
+
109
+ # TODO
110
+ def pull_job(*args, &block)
111
+ set_hook(:pull_job, block_given? ? block : args[0], 1)
112
+ end
113
+
114
+ # TODO
115
+ def process_job(*args, &block)
116
+ set_hook(:process_job, block_given? ? block : args[0])
117
+ end
118
+
119
+ def load_app(*args, &block)
120
+ set_hook(:load_app, block_given? ? block : args[0], -1)
121
+ end
122
+
123
+ # sets the timeout of worker processes to +seconds+. Workers
124
+ # handling the request/app.call/response cycle taking longer than
125
+ # this time period will be forcibly killed (via SIGKILL). This
126
+ # timeout is enforced by the master process itself and not subject
127
+ # to the scheduling limitations by the worker process. Due the
128
+ # low-complexity, low-overhead implementation, timeouts of less
129
+ # than 3.0 seconds can be considered inaccurate and unsafe.
130
+ def timeout(seconds)
131
+ set_int(:timeout, seconds, 3)
132
+ end
133
+
134
+ # sets the current number of worker_processes to +nr+. Each worker
135
+ # process will serve exactly one client at a time. You can
136
+ # increment or decrement this value at runtime by sending SIGTTIN
137
+ # or SIGTTOU respectively to the master process without reloading
138
+ # the rest of your Qilin configuration. See the SIGNALS document
139
+ # for more information.
140
+ def worker_processes(nr)
141
+ set_int(:worker_processes, nr, 1)
142
+ end
143
+
144
+ # sets the +path+ for the PID file of the Qilin master process
145
+ def pid(path); set_path(:pid, path); end
146
+
147
+ # Enabling this preloads an application before forking worker
148
+ # processes. This allows memory savings when using a
149
+ # copy-on-write-friendly GC but can cause bad things to happen when
150
+ # resources like sockets are opened at load time by the master
151
+ # process and shared by multiple children. People enabling this are
152
+ # highly encouraged to look at the before_fork/after_fork hooks to
153
+ # properly close/reopen sockets. Files opened for logging do not
154
+ # have to be reopened as (unbuffered-in-userspace) files opened with
155
+ # the File::APPEND flag are written to atomically on UNIX.
156
+ #
157
+ # In addition to reloading the Qilin-specific config settings,
158
+ # SIGHUP will reload application code in the working
159
+ # directory/symlink when workers are gracefully restarted when
160
+ # preload_app=false (the default). As reloading the application
161
+ # sometimes requires RubyGems updates, +Gem.refresh+ is always
162
+ # called before the application is loaded (for RubyGems users).
163
+ #
164
+ # During deployments, care should _always_ be taken to ensure your
165
+ # applications are properly deployed and running. Using
166
+ # preload_app=false (the default) means you _must_ check if
167
+ # your application is responding properly after a deployment.
168
+ # Improperly deployed applications can go into a spawn loop
169
+ # if the application fails to load. While your children are
170
+ # in a spawn loop, it is is possible to fix an application
171
+ # by properly deploying all required code and dependencies.
172
+ # Using preload_app=true means any application load error will
173
+ # cause the master process to exit with an error.
174
+ def preload_app(bool)
175
+ set_bool(:preload_app, bool)
176
+ end
177
+
178
+ # Allow redirecting $stderr to a given path. Unlike doing this from
179
+ # the shell, this allows the Qilin process to know the path its
180
+ # writing to and rotate the file if it is used for logging. The
181
+ # file will be opened with the File::APPEND flag and writes
182
+ # synchronized to the kernel (but not necessarily to _disk_) so
183
+ # multiple processes can safely append to it.
184
+ #
185
+ # If you are daemonizing and using the default +logger+, it is important
186
+ # to specify this as errors will otherwise be lost to /dev/null.
187
+ # Some applications/libraries may also triggering warnings that go to
188
+ # stderr, and they will end up here.
189
+ def stderr_path(path)
190
+ set_path(:stderr_path, path)
191
+ end
192
+
193
+ # Same as stderr_path, except for $stdout. Not many Rack applications
194
+ # write to $stdout, but any that do will have their output written here.
195
+ # It is safe to point this to the same location a stderr_path.
196
+ # Like stderr_path, this defaults to /dev/null when daemonized.
197
+ def stdout_path(path)
198
+ set_path(:stdout_path, path)
199
+ end
200
+
201
+ # sets the working directory for Qilin.
202
+ def working_directory(path)
203
+ # just let chdir raise errors
204
+ path = File.expand_path(path)
205
+ if config_file &&
206
+ config_file[0] != ?/ &&
207
+ ! File.readable?("#{path}/#{config_file}")
208
+ raise ArgumentError,
209
+ "config_file=#{config_file} would not be accessible in" \
210
+ " working_directory=#{path}"
211
+ end
212
+ Dir.chdir(path)
213
+ # TODO Sockets::Center::START_CTX[:cwd] = ENV["PWD"] = path
214
+ end
215
+
216
+ # Runs worker processes as the specified +user+ and +group+.
217
+ # The master process always stays running as the user who started it.
218
+ # This switch will occur after calling the after_fork hook, and only
219
+ # if the Worker#user method is not called in the after_fork hook
220
+ def user(user, group = nil)
221
+ # raises ArgumentError on invalid user/group
222
+ Etc.getpwnam(user)
223
+ Etc.getgrnam(group) if group
224
+ set[:user] = [ user, group ]
225
+ end
226
+
227
+ private
228
+ def set_int(var, n, min) #:nodoc:
229
+ Integer === n or raise ArgumentError, "not an integer: #{var}=#{n.inspect}"
230
+ n >= min or raise ArgumentError, "too low (< #{min}): #{var}=#{n.inspect}"
231
+ set[var] = n
232
+ end
233
+
234
+ def set_path(var, path) #:nodoc:
235
+ case path
236
+ when NilClass, String
237
+ set[var] = path
238
+ else
239
+ raise ArgumentError
240
+ end
241
+ end
242
+
243
+ def set_bool(var, bool) #:nodoc:
244
+ case bool
245
+ when true, false
246
+ set[var] = bool
247
+ else
248
+ raise ArgumentError, "#{var}=#{bool.inspect} not a boolean"
249
+ end
250
+ end
251
+
252
+ def set_hook(var, my_proc, req_arity = 2) #:nodoc:
253
+ case my_proc
254
+ when Proc
255
+ arity = my_proc.arity
256
+ (arity == req_arity) or \
257
+ raise ArgumentError,
258
+ "#{var}=#{my_proc.inspect} has invalid arity: " \
259
+ "#{arity} (need #{req_arity})"
260
+ when NilClass
261
+ my_proc = DEFAULTS[var]
262
+ else
263
+ raise ArgumentError, "invalid type: #{var}=#{my_proc.inspect}"
264
+ end
265
+ set[var] = my_proc
266
+ end
267
+ end
@@ -0,0 +1,57 @@
1
+ # -*- encoding: binary -*-
2
+
3
+ $stdout.sync = $stderr.sync = true
4
+ $stdin.binmode
5
+ $stdout.binmode
6
+ $stderr.binmode
7
+
8
+ require 'qilin'
9
+
10
+ module Qilin::Launcher
11
+
12
+ # We don't do a lot of standard daemonization stuff:
13
+ # * umask is whatever was set by the parent process at startup
14
+ # and can be set in config.ru and config_file, so making it
15
+ # 0000 and potentially exposing sensitive log data can be bad
16
+ # policy.
17
+ # * don't bother to chdir("/") here since qilin is designed to
18
+ # run inside APP_ROOT. Qilin will also re-chdir() to
19
+ # the directory it was started in when being re-executed
20
+ # to pickup code changes if the original deployment directory
21
+ # is a symlink or otherwise got replaced.
22
+ def self.daemonize!(options)
23
+ cfg = Qilin::Configurator
24
+ $stdin.reopen("/dev/null")
25
+
26
+ # grandparent - reads pipe, exits when master is ready
27
+ # \_ parent - exits immediately ASAP
28
+ # \_ qilin manager - writes to pipe when ready
29
+
30
+ rd, wr = IO.pipe
31
+ grandparent = $$
32
+ if fork
33
+ wr.close # grandparent does not write
34
+ else
35
+ rd.close # qilin master does not read
36
+ Process.setsid
37
+ exit if fork # parent dies now
38
+ end
39
+
40
+ if grandparent == $$
41
+ # this will block until Manager#join runs (or it dies)
42
+ master_pid = (rd.readpartial(16) rescue nil).to_i
43
+ unless master_pid > 1
44
+ warn "master failed to start, check stderr log for details"
45
+ exit!(1)
46
+ end
47
+ exit 0
48
+ else # qilin master process
49
+ options[:ready_pipe] = wr
50
+ end
51
+
52
+ # $stderr/$stderr can/will be redirected separately in the qilin config
53
+ cfg::DEFAULTS[:stderr_path] ||= "/dev/null"
54
+ cfg::DEFAULTS[:stdout_path] ||= "/dev/null"
55
+ end
56
+
57
+ end
@@ -0,0 +1,390 @@
1
+ class Qilin::Manager
2
+ attr_accessor :timeout, :worker_processes,
3
+ :before_fork, :after_fork, :load_app,
4
+ :preload_app, :pull_job, :process_job,
5
+ :master_pid, :config, :user, :ready_pipe
6
+ attr_reader :pid, :logger
7
+
8
+ CHILD_READY = []
9
+ CHILD_PIPES = {}
10
+
11
+ # signal queue used for self-piping
12
+ SIG_QUEUE = []
13
+
14
+ # This hash maps PIDs to Workers
15
+ WORKERS = {}
16
+
17
+ # list of signals we care about and trap in master.
18
+ QUEUE_SIGS = [ :WINCH, :QUIT, :INT, :TERM, :USR1, :HUP, :TTIN, :TTOU ]
19
+
20
+ # general
21
+ def logger=(obj)
22
+ @logger = obj
23
+ end
24
+
25
+ # sets the path for the PID file of the master process
26
+ def pid=(path)
27
+ if path
28
+ if x = valid_pid?(path)
29
+ return path if pid && path == pid && x == $$
30
+ raise ArgumentError, "Already running on PID:#{x} " \
31
+ "(or pid=#{path} is stale)"
32
+ end
33
+ end
34
+ unlink_pid_safe(pid) if pid
35
+
36
+ if path
37
+ fp = begin
38
+ tmp = "#{File.dirname(path)}/#{rand}.#$$"
39
+ File.open(tmp, File::RDWR|File::CREAT|File::EXCL, 0644)
40
+ rescue Errno::EEXIST
41
+ retry
42
+ end
43
+ fp.syswrite("#$$\n")
44
+ File.rename(fp.path, path)
45
+ fp.close
46
+ end
47
+ @pid = path
48
+ end
49
+
50
+ # unlinks a PID file at given +path+ if it contains the current PID
51
+ # still potentially racy without locking the directory (which is
52
+ # non-portable and may interact badly with other programs), but the
53
+ # window for hitting the race condition is small
54
+ def unlink_pid_safe(path)
55
+ (File.read(path).to_i == $$ and File.unlink(path)) rescue nil
56
+ end
57
+
58
+ # returns a PID if a given path contains a non-stale PID file,
59
+ # nil otherwise.
60
+ def valid_pid?(path)
61
+ wpid = File.read(path).to_i
62
+ wpid <= 0 and return
63
+ Process.kill(0, wpid)
64
+ wpid
65
+ rescue Errno::ESRCH, Errno::ENOENT
66
+ # don't unlink stale pid files, racy without non-portable locking...
67
+ end
68
+
69
+ def initialize(options = {})
70
+ options = options.dup
71
+ self.ready_pipe = options.delete(:ready_pipe)
72
+ options[:use_defaults] = true
73
+ self.config = Qilin::Configurator.new(options)
74
+ config.commit!(self, :skip => [:pid])
75
+ end
76
+
77
+ # Runs the thing. Returns self so you can run join on it
78
+ def start
79
+ # setup signal handlers before writing pid file in case people get
80
+ # trigger happy and send signals as soon as the pid file exists.
81
+ # Note that signals don't actually get handled until the #join method
82
+ QUEUE_SIGS.each { |sig| trap(sig) { SIG_QUEUE << sig } }
83
+
84
+ self.pid = config[:pid]
85
+ self.master_pid = $$
86
+
87
+ build_app! if preload_app
88
+ maintain_worker_count
89
+ self
90
+ end
91
+
92
+ # monitors children and receives signals forever
93
+ # (or until a termination signal is sent). This handles signals
94
+ # one-at-a-time time and we'll happily drop signals in case somebody
95
+ # is signalling us too often.
96
+ def join
97
+ respawn = true
98
+ last_check = Time.now
99
+
100
+ proc_name 'master'
101
+ logger.info "master process ready" # test_exec.rb relies on this message
102
+
103
+ if ready_pipe
104
+ ready_pipe.syswrite($$.to_s)
105
+ ready_pipe.close rescue nil
106
+ self.ready_pipe = nil
107
+ end
108
+
109
+ begin
110
+ reap_all_workers
111
+ case SIG_QUEUE.shift
112
+ when nil
113
+ # avoid murdering workers after our master process (or the
114
+ # machine) comes out of suspend/hibernation
115
+ if (last_check + @timeout) >= (last_check = Time.now)
116
+ murder_lazy_workers
117
+ end
118
+ sleep_time = 1 # TODO make configurable
119
+ maintain_worker_count if respawn
120
+ master_poll(sleep_time)
121
+ when :QUIT # graceful shutdown
122
+ break
123
+ when :TERM, :INT # immediate shutdown
124
+ stop(false)
125
+ break
126
+ when :USR1 # rotate logs
127
+ logger.info "master reopening logs..."
128
+ # TODO Unicorn::Util.reopen_logs
129
+ logger.info "master done reopening logs"
130
+ kill_each_worker(:USR1)
131
+ when :WINCH
132
+ if Process.ppid == 1 || Process.getpgrp != $$
133
+ respawn = false
134
+ logger.info "gracefully stopping all workers"
135
+ kill_each_worker(:QUIT)
136
+ self.worker_processes = 0
137
+ else
138
+ logger.info "SIGWINCH ignored because we're not daemonized"
139
+ end
140
+ when :TTIN
141
+ respawn = true
142
+ self.worker_processes += 1
143
+ when :TTOU
144
+ self.worker_processes -= 1 if self.worker_processes > 0
145
+ when :HUP
146
+ respawn = true
147
+ if config.config_file
148
+ load_config!
149
+ else # exec binary and exit if there's no config file
150
+ logger.info "config_file not present, reexecuting binary"
151
+ reexec
152
+ end
153
+ end
154
+ rescue Errno::EINTR
155
+ rescue => e
156
+ logger.error "Unhandled master loop exception #{e.inspect}."
157
+ logger.error e.backtrace.join("\n")
158
+ end while true
159
+ stop # gracefully shutdown all workers on our way out
160
+ logger.info "master complete"
161
+ unlink_pid_safe(pid) if pid
162
+ end
163
+
164
+ # wait for a signal hander to wake us up and then consume the pipe
165
+ def master_poll(sec)
166
+ r = IO.select(CHILD_READY, nil, nil, sec) or return false
167
+ rd = r.flatten.first
168
+ return false unless rd
169
+
170
+ begin
171
+ rd.gets
172
+ ready_child = CHILD_PIPES[rd]
173
+ return false unless ready_child
174
+
175
+ job = pull_job.call(self)
176
+ ready_child.puts(job) if job
177
+ rescue Exception => e
178
+ logger.error "Unhandled master poll exception: #{e.inspect}"
179
+ logger.error e.backtrace.join("\n")
180
+ end
181
+ end
182
+
183
+ # Terminates all workers, but does not exit master process
184
+ def stop(graceful = true)
185
+ limit = Time.now + timeout
186
+ until WORKERS.empty? || Time.now > limit
187
+ kill_each_worker(graceful ? :QUIT : :TERM)
188
+ sleep(0.1)
189
+ reap_all_workers
190
+ end
191
+ kill_each_worker(:KILL)
192
+ end
193
+
194
+ def load_config!
195
+ logger.info "reloading config_file=#{config.config_file}"
196
+ config.reload
197
+ config.commit!(self)
198
+ kill_each_worker(:QUIT)
199
+ # TODO Unicorn::Util.reopen_logs
200
+ logger.info "done reloading config_file=#{config.config_file}"
201
+ rescue StandardError, LoadError, SyntaxError => e
202
+ logger.error "error reloading config_file=#{config.config_file}: " \
203
+ "#{e.class} #{e.message} #{e.backtrace}"
204
+ end
205
+
206
+ private
207
+
208
+ def spawn_missing_workers
209
+ (0...worker_processes).each do |worker_nr|
210
+ WORKERS.values.include?(worker_nr) and next
211
+ worker = Qilin::Worker.new(worker_nr, Qilin::TmpIO.new)
212
+ spawn_worker(worker)
213
+ end
214
+ end
215
+
216
+ def spawn_worker(worker)
217
+ before_fork.call(self, worker)
218
+
219
+ # Used by the worker to tell the manager its ready for a job
220
+ read_ready, write_ready = IO.pipe
221
+
222
+ # Used by the manager to pass a job to the worker
223
+ read_job, write_job = IO.pipe
224
+
225
+ # set it on the worker
226
+ worker.ready_pipe = [read_ready, write_ready]
227
+ worker.job_pipe = [read_job, write_job]
228
+
229
+ # Fork the worker
230
+ WORKERS[fork {
231
+ worker_loop(worker)
232
+ }] = worker
233
+
234
+ CHILD_READY << read_ready
235
+ CHILD_PIPES[read_ready] = write_job
236
+ end
237
+
238
+ def maintain_worker_count
239
+ (off = WORKERS.size - worker_processes) == 0 and return
240
+ off < 0 and return spawn_missing_workers
241
+ WORKERS.dup.each_pair { |wpid,w|
242
+ w.nr >= worker_processes and kill_worker(:QUIT, wpid) rescue nil
243
+ }
244
+ end
245
+
246
+ # gets rid of stuff the worker has no business keeping track of
247
+ # to free some resources and drops all sig handlers.
248
+ # traps for USR1, USR2, and HUP may be set in the after_fork Proc
249
+ # by the user.
250
+ def init_worker_process(worker)
251
+ # TODO QUEUE_SIGS.each { |sig| trap(sig, nil) }
252
+ trap(:CHLD, 'DEFAULT')
253
+ SIG_QUEUE.clear
254
+ proc_name "worker [#{worker.nr}]"
255
+ WORKERS.values.each { |other| other.tmp.close rescue nil }
256
+ WORKERS.clear
257
+ worker.tmp.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
258
+ after_fork.call(self, worker) # can drop perms
259
+ worker.user(*user) if user.kind_of?(Array) && ! worker.switched
260
+ self.timeout /= 2.0 # halve it for select()
261
+ build_app! unless preload_app
262
+ end
263
+
264
+ def reopen_worker_logs(worker_nr)
265
+ logger.info "worker=#{worker_nr} reopening logs..."
266
+ # TODO Unicorn::Util.reopen_logs
267
+ logger.info "worker=#{worker_nr} done reopening logs"
268
+ end
269
+
270
+ # runs inside each forked worker, this sits around and waits
271
+ # for connections and doesn't die until the parent dies (or is
272
+ # given a INT, QUIT, or TERM signal)
273
+ def worker_loop(worker)
274
+ ppid = master_pid
275
+ init_worker_process(worker)
276
+ nr = 0 # this becomes negative if we need to reopen logs
277
+ alive = worker.tmp # tmp is our lifeline to the master process
278
+
279
+ # closing anything we IO.select on will raise EBADF
280
+ trap(:USR1) { nr = -65536; worker.job_pipe[0].close rescue nil }
281
+ trap(:QUIT) { alive = nil; }
282
+ [:TERM, :INT].each { |sig| trap(sig) { exit!(0) } } # instant shutdown
283
+ logger.info "worker=#{worker.nr} ready"
284
+ m = 0
285
+
286
+ begin
287
+ nr < 0 and reopen_worker_logs(worker.nr)
288
+ nr = 0
289
+
290
+ # we're a goner in timeout seconds anyways if alive.chmod
291
+ # breaks, so don't trap the exception. Using fchmod() since
292
+ # futimes() is not available in base Ruby and I very strongly
293
+ # prefer temporary files to be unlinked for security,
294
+ # performance and reliability reasons, so utime is out. No-op
295
+ # changes with chmod doesn't update ctime on all filesystems; so
296
+ # we change our counter each and every time (after process_client
297
+ # and before IO.select).
298
+ alive.chmod(m = 0 == m ? 1 : 0)
299
+
300
+ # Signal to the manager we're ready for a job
301
+ worker.ready_pipe[1].puts('.')
302
+
303
+ # Read a job from the manager
304
+ job_payload = worker.job_pipe[0].gets.chomp
305
+
306
+ # timestamp
307
+ alive.chmod(m = 0 == m ? 1 : 0)
308
+
309
+ # process the job payload
310
+ process_job.call(worker, job_payload)
311
+
312
+ # make the following bet: if we accepted clients this round,
313
+ # we're probably reasonably busy, so avoid calling select()
314
+ # and do a speculative non-blocking accept() on ready listeners
315
+ # before we sleep again in select().
316
+ redo unless nr == 0 # (nr < 0) => reopen logs
317
+
318
+ ppid == Process.ppid or return
319
+ alive.chmod(m = 0 == m ? 1 : 0)
320
+ rescue => e
321
+ if alive
322
+ logger.error "Unhandled listen loop exception #{e.inspect}."
323
+ logger.error e.backtrace.join("\n")
324
+ end
325
+ end while alive
326
+ end
327
+
328
+ # forcibly terminate all workers that haven't checked in in timeout
329
+ # seconds. The timeout is implemented using an unlinked File
330
+ # shared between the parent process and each worker. The worker
331
+ # runs File#chmod to modify the ctime of the File. If the ctime
332
+ # is stale for >timeout seconds, then we'll kill the corresponding
333
+ # worker.
334
+ def murder_lazy_workers
335
+ t = @timeout
336
+ WORKERS.dup.each_pair do |wpid, worker|
337
+ stat = worker.tmp.stat
338
+ # skip workers that disable fchmod or have never fchmod-ed
339
+ stat.mode == 0100600 and next
340
+ diff = Time.now - stat.ctime
341
+ next if diff <= t
342
+ logger.error "worker=#{worker.nr} PID:#{wpid} timeout " \
343
+ "(#{diff}s > #{t}s), killing"
344
+ kill_worker(:KILL, wpid) # take no prisoners for timeout violations
345
+ end
346
+ end
347
+
348
+ # reaps all unreaped workers
349
+ def reap_all_workers
350
+ begin
351
+ wpid, status = Process.waitpid2(-1, Process::WNOHANG)
352
+ wpid or return
353
+ worker = WORKERS.delete(wpid) and worker.tmp.close rescue nil
354
+ CHILD_READY.delete(worker.ready_pipe[0]) and CHILD_PIPES.delete(worker.ready_pipe[0]) and worker.ready_pipe.map(&:close) and worker.job_pipe.map(&:close) rescue nil
355
+ m = "reaped #{status.inspect} worker=#{worker.nr rescue 'unknown'}"
356
+ status.success? ? logger.info(m) : logger.error(m)
357
+ rescue Errno::ECHILD
358
+ break
359
+ end while true
360
+ end
361
+
362
+ # delivers a signal to a worker and fails gracefully if the worker
363
+ # is no longer running.
364
+ def kill_worker(signal, wpid)
365
+ Process.kill(signal, wpid)
366
+ rescue Errno::ESRCH
367
+ worker = WORKERS.delete(wpid) and worker.tmp.close rescue nil
368
+ end
369
+
370
+ # delivers a signal to each worker
371
+ def kill_each_worker(signal)
372
+ WORKERS.keys.each { |wpid| kill_worker(signal, wpid) }
373
+ end
374
+
375
+ def build_app!
376
+ return unless load_app
377
+
378
+ if defined?(Gem) && Gem.respond_to?(:refresh)
379
+ logger.info "Refreshing Gem list"
380
+ Gem.refresh
381
+ end
382
+
383
+ load_app.call
384
+ end
385
+
386
+ def proc_name(tag)
387
+ $0 = "qilin_#{tag}"
388
+ end
389
+
390
+ end
@@ -0,0 +1,29 @@
1
+ # -*- encoding: binary -*-
2
+ # :stopdoc:
3
+ require 'tmpdir'
4
+
5
+ # some versions of Ruby had a broken Tempfile which didn't work
6
+ # well with unlinked files. This one is much shorter, easier
7
+ # to understand, and slightly faster.
8
+ class Qilin::TmpIO < File
9
+
10
+ # creates and returns a new File object. The File is unlinked
11
+ # immediately, switched to binary mode, and userspace output
12
+ # buffering is disabled
13
+ def self.new
14
+ fp = begin
15
+ super("#{Dir::tmpdir}/#{rand}", RDWR|CREAT|EXCL, 0600)
16
+ rescue Errno::EEXIST
17
+ retry
18
+ end
19
+ unlink(fp.path)
20
+ fp.binmode
21
+ fp.sync = true
22
+ fp
23
+ end
24
+
25
+ # for easier env["rack.input"] compatibility with Rack <= 1.1
26
+ def size
27
+ stat.size
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module Qilin
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,40 @@
1
+ # -*- encoding: binary -*-
2
+
3
+ # This class and its members can be considered a stable interface
4
+ # and will not change in a backwards-incompatible fashion between
5
+ # releases of Qilin. You may need to access it in the
6
+ # before_fork/after_fork hooks. See the Qilin::Configurator RDoc
7
+ # for examples.
8
+ class Qilin::Worker < Struct.new(:nr, :tmp, :switched, :ready_pipe, :job_pipe)
9
+
10
+ # worker objects may be compared to just plain numbers
11
+ def ==(other_nr)
12
+ self.nr == other_nr
13
+ end
14
+
15
+ # Changes the worker process to the specified +user+ and +group+
16
+ # This is only intended to be called from within the worker
17
+ # process from the +after_fork+ hook. This should be called in
18
+ # the +after_fork+ hook after any priviledged functions need to be
19
+ # run (e.g. to set per-worker CPU affinity, niceness, etc)
20
+ #
21
+ # Any and all errors raised within this method will be propagated
22
+ # directly back to the caller (usually the +after_fork+ hook.
23
+ # These errors commonly include ArgumentError for specifying an
24
+ # invalid user/group and Errno::EPERM for insufficient priviledges
25
+ def user(user, group = nil)
26
+ # we do not protect the caller, checking Process.euid == 0 is
27
+ # insufficient because modern systems have fine-grained
28
+ # capabilities. Let the caller handle any and all errors.
29
+ uid = Etc.getpwnam(user).uid
30
+ gid = Etc.getgrnam(group).gid if group
31
+ # TODO Unicorn::Util.chown_logs(uid, gid)
32
+ tmp.chown(uid, gid)
33
+ if gid && Process.egid != gid
34
+ Process.initgroups(user, gid)
35
+ Process::GID.change_privilege(gid)
36
+ end
37
+ Process.euid != uid and Process::UID.change_privilege(uid)
38
+ self.switched = true
39
+ end
40
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: qilin
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Ken Robertson
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-05-16 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: A lightweight framework for background processing across many child processes, inspired heavily by Unicorn.
23
+ email:
24
+ - ken@invalidlogic.com
25
+ executables:
26
+ - qilin
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - Rakefile
33
+ - LICENSE
34
+ - bin/qilin
35
+ - lib/qilin/configurator.rb
36
+ - lib/qilin/launcher.rb
37
+ - lib/qilin/manager.rb
38
+ - lib/qilin/tmpio.rb
39
+ - lib/qilin/version.rb
40
+ - lib/qilin/worker.rb
41
+ - lib/qilin.rb
42
+ has_rdoc: true
43
+ homepage: http://github.com/krobertson/qilin
44
+ licenses: []
45
+
46
+ post_install_message:
47
+ rdoc_options: []
48
+
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ hash: 3
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ requirements: []
70
+
71
+ rubyforge_project: qilin
72
+ rubygems_version: 1.3.7
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: A lightweight framework for background processing across many child processes, inspired heavily by Unicorn.
76
+ test_files: []
77
+