Sha256: 47a1643c6be8e6ad0a0277badb4e19953f70e940d6443ed6518104c87b649394

Contents?: true

Size: 991 Bytes

Versions: 11

Compression:

Stored size: 991 Bytes

Contents

module Enumerable

  def parallel
    ParallelEnumerable.new(self)
  end

end

class ParallelEnumerable

  def initialize(enumerable)
    @enumerable = enumerable
  end



  def each
    enumerable.map do |item|
      Thread.new do
        begin
          yield item
        rescue Exception # rescues StandardError by default; but we want to rescue and report all errors
          Houston.report_exception $!
        ensure
          ActiveRecord::Base.clear_active_connections!
        end
      end
    end.each(&:join)
  end

  def map
    queue = Queue.new

    each do |item|
      queue << yield(item)
    end

    [].tap do |results|
      results.push queue.pop until queue.empty?
    end
  end



  def method_missing(method_name, *args, &block)
    return super unless enumerable.respond_to?(method_name)

    $stderr.puts "[parallel-enumerable] ##{method_name} is not defined"
    enumerable.public_send(method_name, *args, &block)
  end

private

  attr_reader :enumerable

end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
houston-core-0.6.3 lib/parallel_enumerable.rb
houston-core-0.6.2 lib/parallel_enumerable.rb
houston-core-0.6.1 lib/parallel_enumerable.rb
houston-core-0.6.0 lib/parallel_enumerable.rb
houston-core-0.5.6 lib/parallel_enumerable.rb
houston-core-0.5.5 lib/parallel_enumerable.rb
houston-core-0.5.4 lib/parallel_enumerable.rb
houston-core-0.5.3 lib/parallel_enumerable.rb
houston-core-0.5.2 lib/parallel_enumerable.rb
houston-core-0.5.1 lib/parallel_enumerable.rb
houston-core-0.5.0 lib/parallel_enumerable.rb