Sha256: 2cfe701f538416cd515a52c90abdcae06a050e63a12be0789ae4de38282111b3

Contents?: true

Size: 1.33 KB

Versions: 8

Compression:

Stored size: 1.33 KB

Contents

require 'active_force/bulk/job'
require 'active_force/bulk/records'

module ActiveForce
  module Bulk
    class TimeoutError < Timeout::Error; end
    TIMEOUT_MESSAGE = 'Bulk job execution expired based on timeout of %{timeout} seconds'.freeze

    def bulk_insert_all(attributes, options={})
      run_bulk_job(:insert, attributes, options)
    end

    def bulk_update_all(attributes, options={})
      run_bulk_job(:update, attributes, options)
    end

    def bulk_delete_all(attributes, options={})
      run_bulk_job(:delete, attributes, options)
    end

    private

    def default_options
      {
        timeout: 30,
        sleep: 0.02 # short sleep so we can end our poll loop more quickly
      }
    end

    def run_bulk_job(operation, attributes, options)
      runtime_options = default_options.merge(options)
      records = Records.parse_from_attributes(translate_to_sf(attributes))
      job = Job.run(operation: operation, object: self.table_name, records: records)
      Timeout.timeout(runtime_options[:timeout], ActiveForce::Bulk::TimeoutError, TIMEOUT_MESSAGE % runtime_options) do
        until job.finished? do
          job.info
          sleep(runtime_options[:sleep])
        end
      end
      job.result
    end

    def translate_to_sf(attributes)
      attributes.map{ |r| self.mapping.translate_to_sf(r) }
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
active_force-0.24.0 lib/active_force/bulk.rb
active_force-0.23.0 lib/active_force/bulk.rb
active_force-0.22.1 lib/active_force/bulk.rb
active_force-0.22.0 lib/active_force/bulk.rb
active_force-0.21.0 lib/active_force/bulk.rb
active_force-0.20.1 lib/active_force/bulk.rb
active_force-0.20.0 lib/active_force/bulk.rb
active_force-0.19.0 lib/active_force/bulk.rb