Module: Spawn
- Defined in:
- lib/spawn.rb
Defined Under Namespace
Classes: SpawnId
Constant Summary collapse
- @@default_options =
{ # default to forking (unless windows or jruby) :method => ((RUBY_PLATFORM =~ /(win32|java|mingw32)/) ? :thread : :fork), :nice => nil, :kill => false, :argv => nil }
- @@resources =
things to close in child process
[]
- @@logger =
in some environments, logger isn’t defined
Rails.logger || Logger.new(STDERR)
- @@punks =
forked children to kill on exit
[]
Class Method Summary collapse
- .alive?(pid) ⇒ Boolean
-
.close_resources ⇒ Object
close all the resources added by calls to resource_to_close.
-
.default_options(options = {}) ⇒ Object
Set the options to use every time spawn is called unless specified otherwise.
- .kill_punks ⇒ Object
-
.resources_to_close(*resources) ⇒ Object
set the resources to disconnect from in the child process (when forking).
Instance Method Summary collapse
-
#spawn_block(opts = {}) ⇒ Object
Spawns a long-running section of code and returns the ID of the spawned process.
- #wait(sids = []) ⇒ Object
Class Method Details
.alive?(pid) ⇒ Boolean
47 48 49 50 51 52 53 54 55 |
# File 'lib/spawn.rb', line 47 def self.alive?(pid) begin Process::kill 0, pid # if the process is alive then kill won't throw an exception true rescue Errno::ESRCH false end end |
.close_resources ⇒ Object
close all the resources added by calls to resource_to_close
39 40 41 42 43 44 45 |
# File 'lib/spawn.rb', line 39 def self.close_resources @@resources.each do |resource| resource.close if resource && resource.respond_to?(:close) && !resource.closed? end # in case somebody spawns recursively @@resources.clear end |
.default_options(options = {}) ⇒ Object
Set the options to use every time spawn is called unless specified otherwise. For example, in your environment, do something like this:
Spawn:: = {:nice => 5}
to default to using the :nice option with a value of 5 on every call. Valid options are:
:method => (:thread | :fork | :yield)
:nice => nice value of the forked process
:kill => whether or not the parent process will kill the
spawned child process when the parent exits
:argv => changes name of the spawned process as seen in ps
28 29 30 31 |
# File 'lib/spawn.rb', line 28 def self.( = {}) @@default_options.merge!() @@logger.info "spawn> default options = #{.inspect}" end |
.kill_punks ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/spawn.rb', line 57 def self.kill_punks @@punks.each do |punk| if alive?(punk) @@logger.info "spawn> parent(#{Process.pid}) killing child(#{punk})" begin Process.kill("TERM", punk) rescue end end end @@punks = [] end |
.resources_to_close(*resources) ⇒ Object
set the resources to disconnect from in the child process (when forking)
34 35 36 |
# File 'lib/spawn.rb', line 34 def self.resources_to_close(*resources) @@resources = resources end |
Instance Method Details
#spawn_block(opts = {}) ⇒ Object
Spawns a long-running section of code and returns the ID of the spawned process. By default the process will be a forked process. To use threading, pass :method => :thread or override the default behavior in the environment by setting ‘Spawn::method :thread’.
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/spawn.rb', line 76 def spawn_block(opts = {}) = @@default_options.merge(opts.symbolize_keys) # setting options[:method] will override configured value in default_options[:method] if [:method] == :yield yield elsif [:method] == :thread || ([:method] == nil && @@method == :thread) thread_it() { yield } else fork_it() { yield } end end |
#wait(sids = []) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/spawn.rb', line 88 def wait(sids = []) # wait for all threads and/or forks (if a single sid passed in, convert to array first) Array(sids).each do |sid| if sid.type == :thread sid.handle.join() else begin Process.wait(sid.handle) rescue # if the process is already done, ignore the error end end end # clean up connections from expired threads ActiveRecord::Base.verify_active_connections!() if defined? ActiveRecord end |