Class: SalesforceBulkApi::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/salesforce_bulk_api/job.rb

Defined Under Namespace

Classes: SalesforceException

Constant Summary collapse

XML_HEADER =
'<?xml version="1.0" encoding="utf-8" ?>'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Job

Returns a new instance of Job.



11
12
13
14
15
16
17
18
19
# File 'lib/salesforce_bulk_api/job.rb', line 11

def initialize(args)
  @job_id = args[:job_id]
  @operation = args[:operation]
  @sobject = args[:sobject]
  @external_field = args[:external_field]
  @records = args[:records]
  @connection = args[:connection]
  @batch_ids = []
end

Instance Attribute Details

#job_idObject (readonly)

Returns the value of attribute job_id.



5
6
7
# File 'lib/salesforce_bulk_api/job.rb', line 5

def job_id
  @job_id
end

Instance Method Details

#add_batchesObject

Raises:

  • (ArgumentError)


43
44
45
46
47
48
49
50
51
52
# File 'lib/salesforce_bulk_api/job.rb', line 43

def add_batches
  raise ArgumentError, "Records must be an array of hashes." unless @records.is_a?(Array)

  keys = @records.each_with_object({}) { |pairs, h| pairs.each { |k, v| (h[k] ||= []) << v } }.keys
  batches = @records.each_slice(@batch_size).to_a

  batches.each do |batch|
    @batch_ids << add_batch(keys, batch)
  end
end

#add_queryObject



37
38
39
40
41
# File 'lib/salesforce_bulk_api/job.rb', line 37

def add_query
  response = post_xml("job/#{@job_id}/batch/", @records)
  response_parsed = XmlSimple.xml_in(response)
  @batch_ids << response_parsed["id"][0]
end

#close_jobObject



31
32
33
34
35
# File 'lib/salesforce_bulk_api/job.rb', line 31

def close_job
  xml = build_close_job_xml
  response = post_xml("job/#{@job_id}", xml)
  XmlSimple.xml_in(response)
end

#create_job(batch_size, send_nulls, no_null_list) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/salesforce_bulk_api/job.rb', line 21

def create_job(batch_size, send_nulls, no_null_list)
  @batch_size = batch_size
  @send_nulls = send_nulls
  @no_null_list = no_null_list

  xml = build_job_xml
  response = post_xml("job", xml)
  parse_job_response(response)
end

#get_job_result(return_result, timeout) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/salesforce_bulk_api/job.rb', line 54

def get_job_result(return_result, timeout)
  state = []
  Timeout.timeout(timeout, JobTimeout) do
    loop do
      job_status = check_job_status
      break unless job_closed_and_batches_completed?(job_status, state)
      break if @batch_ids.empty?
    end
  end
rescue JobTimeout => e
  handle_timeout(e)
ensure
  process_batch_results(state) if return_result
  state
end