module DontStallMyProcess

Constants

DontStallMyProcessError
KernelMethodCalled

This exception is raised when a forbidden Kernel method is called. These methods do not get forwarded over the DRb.

SubprocessInitializationFailed

This exception is raised when the subprocess could not be created, or its initialization failed.

TimeoutExceeded

This exception is raised when the watchdog bites.

VERSION

Public Class Methods

configure() { |get| ... } click to toggle source
# File lib/dont-stall-my-process.rb, line 14
def self.configure
  yield Configuration.get if block_given?
end
create(klass, opts = {}) { |proxy| ... } click to toggle source
# File lib/dont-stall-my-process.rb, line 18
def self.create(klass, opts = {})
  fail 'no klass given' unless klass && klass.is_a?(Class)

  # Set default values and validate configuration.
  opts = sanitize_options(opts)

  # Fork the child process.
  process = Local::ChildProcessPool.alloc

  # Start the DRb service for the main class and create a proxy.
  proxy = process.start_service(klass, opts)

  # If a block is given, we finalize the service immediately after its return.
  if block_given?

    yield proxy

    proxy.stop_service!
    proxy = nil
  end
  
  proxy
end
sanitize_options(opts, default_timeout = Configuration::DEFAULT_TIMEOUT) click to toggle source
# File lib/dont-stall-my-process.rb, line 42
def self.sanitize_options(opts, default_timeout = Configuration::DEFAULT_TIMEOUT)
  fail 'opts is not a hash' unless opts.is_a?(Hash)

  opts[:timeout] ||= default_timeout
  opts[:methods] ||= {}

  fail 'no timeout given' unless opts[:timeout] && opts[:timeout].is_a?(Fixnum)
  fail 'timeout too low' unless opts[:timeout] > 0
  fail 'methods is not a hash' if opts[:methods] && !opts[:methods].is_a?(Hash)

  {
    timeout: opts[:timeout],
    methods: Hash[
      opts[:methods].map { |meth, mopts| [meth, sanitize_options(mopts, opts[:timeout])] }
    ]
  }
end