Sha256: 1ffa856d9c0d10a85f52f798581fc6d8f0a780e867bce9393c3d509a279cc974

Contents?: true

Size: 1.29 KB

Versions: 1

Compression:

Stored size: 1.29 KB

Contents

require "ferry/version"
require "ferry/engine"
require "ferry/logger"

module Ferry
  #
  class ActiveRecord::Relation
    def migrate(options, &block)
      options[:max_workers] ||= 4
      options[:batch_size]  ||= 10_000

      log = Logger.new()

      active_workers = []
      collection = self
      collection.find_in_batches(batch_size: options[:batch_size]) do |batch|
        if active_workers.length >= options[:max_workers]
          log.write "active_workers oversized at capacity of #{active_workers.length}/#{options[:max_workers]}"
          finished_process = Process.wait
          log.write "finished_process: #{finished_process}"
          active_workers.delete finished_process
          log.write "active_workers capacity now at: #{active_workers.length}/#{options[:max_workers]}"
        else
          active_workers << fork do
            ActiveRecord::Base.connection.reconnect!
            log.write "kicking off engine on batch(#{batch.first}-#{batch.last})"
            engine = Engine.new()
            engine.run({log: log, batch: batch}, &block)
          end
        end
        ActiveRecord::Base.connection.reconnect!
      end
    end
  end

  class Exporter
    def speak
      puts "exporting!"
    end
  end

  class Importer
    def speak
      puts "importing!"
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ferry-0.0.2 lib/ferry.rb