module Sequel::Database::AsyncThreadPool::DatabaseMethods

Public Class Methods

extended(db) click to toggle source
# File lib/sequel/extensions/async_thread_pool.rb, line 345
def self.extended(db)
  db.instance_exec do
    case pool.pool_type
    when :single, :sharded_single
      raise Error, "cannot load async_thread_pool extension if using single or sharded_single connection pool"
    end

    num_async_threads = opts[:num_async_threads] ? typecast_value_integer(opts[:num_async_threads]) : (Integer(opts[:max_connections] || 4))
    raise Error, "must have positive number for num_async_threads" if num_async_threads <= 0

    proxy_klass = typecast_value_boolean(opts[:preempt_async_thread]) ? PreemptableProxy : Proxy
    define_singleton_method(:async_job_class){proxy_klass}

    queue = @async_thread_queue = Queue.new
    pool = @async_thread_pool = num_async_threads.times.map{JobProcessor.new(queue)}
    ObjectSpace.define_finalizer(db, JobProcessor.create_finalizer(queue, pool))

    extend_datasets(DatasetMethods)
  end
end

Private Instance Methods

async_run(&block) click to toggle source

Wrap the block in a job/proxy object and schedule it to run using the async thread pool.

# File lib/sequel/extensions/async_thread_pool.rb, line 369
def async_run(&block)
  proxy = async_job_class.new(&block)
  @async_thread_queue.push(proxy)
  proxy
end