Class: SalesforceBulkApi::Job
- Inherits:
-
Object
- Object
- SalesforceBulkApi::Job
show all
- Defined in:
- lib/salesforce_bulk_api/job.rb
Defined Under Namespace
Classes: SalesforceException
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(args) ⇒ Job
Returns a new instance of Job.
8
9
10
11
12
13
14
15
16
17
|
# File 'lib/salesforce_bulk_api/job.rb', line 8
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 = []
= '<?xml version="1.0" encoding="utf-8" ?>'
end
|
Instance Attribute Details
#job_id ⇒ Object
Returns the value of attribute job_id.
4
5
6
|
# File 'lib/salesforce_bulk_api/job.rb', line 4
def job_id
@job_id
end
|
Instance Method Details
#add_batch(keys, batch) ⇒ Object
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/salesforce_bulk_api/job.rb', line 87
def add_batch(keys, batch)
xml = "#{@XML_HEADER}<sObjects xmlns=\"http://www.force.com/2009/06/asyncapi/dataload\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
batch.each do |r|
xml += create_sobject(keys, r)
end
xml += '</sObjects>'
path = "job/#{@job_id}/batch/"
= Hash["Content-Type" => "application/xml; charset=UTF-8"]
response = @connection.post_xml(nil, path, xml, )
response_parsed = XmlSimple.xml_in(response)
response_parsed['id'][0] if response_parsed['id']
end
|
#add_batches ⇒ Object
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/salesforce_bulk_api/job.rb', line 70
def add_batches
raise 'Records must be an array of hashes.' unless @records.is_a? Array
keys = @records.reduce({}) {|h, pairs| pairs.each {|k, v| (h[k] ||= []) << v}; h}.keys
@records_dup = @records.clone
super_records = []
(@records_dup.size/@batch_size).to_i.times do
super_records << @records_dup.pop(@batch_size)
end
super_records << @records_dup unless @records_dup.empty?
super_records.each do |batch|
@batch_ids << add_batch(keys, batch)
end
end
|
#add_query ⇒ Object
60
61
62
63
64
65
66
67
68
|
# File 'lib/salesforce_bulk_api/job.rb', line 60
def add_query
path = "job/#{@job_id}/batch/"
= Hash["Content-Type" => "application/xml; charset=UTF-8"]
response = @connection.post_xml(nil, path, @records, )
response_parsed = XmlSimple.xml_in(response)
@batch_ids << response_parsed['id'][0]
end
|
#build_sobject(data) ⇒ Object
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/salesforce_bulk_api/job.rb', line 100
def build_sobject(data)
xml = '<sObject>'
data.keys.each do |k|
if k.is_a?(Hash)
xml += build_sobject(k)
elsif data[k] != :type
xml += "<#{k}>#{data[k]}</#{k}>"
end
end
xml += '</sObject>'
end
|
#check_batch_status(batch_id) ⇒ Object
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
# File 'lib/salesforce_bulk_api/job.rb', line 152
def check_batch_status(batch_id)
path = "job/#{@job_id}/batch/#{batch_id}"
= Hash.new
response = @connection.get_request(nil, path, )
begin
response_parsed = XmlSimple.xml_in(response) if response
response_parsed
rescue StandardError => e
puts "Error parsing XML response for #{@job_id}, batch #{batch_id}"
puts e
puts e.backtrace
end
end
|
#check_job_status ⇒ Object
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
# File 'lib/salesforce_bulk_api/job.rb', line 137
def check_job_status
path = "job/#{@job_id}"
= Hash.new
response = @connection.get_request(nil, path, )
begin
response_parsed = XmlSimple.xml_in(response) if response
response_parsed
rescue StandardError => e
puts "Error parsing XML response for #{@job_id}"
puts e
puts e.backtrace
end
end
|
#close_job ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/salesforce_bulk_api/job.rb', line 48
def close_job()
xml = "#{@XML_HEADER}<jobInfo xmlns=\"http://www.force.com/2009/06/asyncapi/dataload\">"
xml += "<state>Closed</state>"
xml += "</jobInfo>"
path = "job/#{@job_id}"
= Hash['Content-Type' => 'application/xml; charset=utf-8']
response = @connection.post_xml(nil, path, 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# 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 = "#{@XML_HEADER}<jobInfo xmlns=\"http://www.force.com/2009/06/asyncapi/dataload\">"
xml += "<operation>#{@operation}</operation>"
xml += "<object>#{@sobject}</object>"
if !@external_field.nil?
xml += "<externalIdFieldName>#{@external_field}</externalIdFieldName>"
end
xml += "<contentType>XML</contentType>"
xml += "</jobInfo>"
path = "job"
= Hash['Content-Type' => 'application/xml; charset=utf-8']
response = @connection.post_xml(nil, path, xml, )
response_parsed = XmlSimple.xml_in(response)
raise SalesforceException.new("#{response_parsed['exceptionMessage'][0]} (#{response_parsed['exceptionCode'][0]})") if response_parsed['exceptionCode']
@job_id = response_parsed['id'][0]
end
|
#create_sobject(keys, r) ⇒ Object
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
# File 'lib/salesforce_bulk_api/job.rb', line 112
def create_sobject(keys, r)
sobject_xml = '<sObject>'
keys.each do |k|
if r[k].is_a?(Hash)
sobject_xml += "<#{k}>"
sobject_xml += build_sobject(r[k])
sobject_xml += "</#{k}>"
elsif !r[k].to_s.empty?
sobject_xml += "<#{k}>"
if r[k].respond_to?(:encode)
sobject_xml += r[k].encode(:xml => :text)
elsif r[k].respond_to?(:iso8601)
sobject_xml += r[k].iso8601.to_s
else
sobject_xml += r[k].to_s
end
sobject_xml += "</#{k}>"
elsif @send_nulls && !@no_null_list.include?(k)
sobject_xml += "<#{k} xsi:nil=\"true\"/>"
end
end
sobject_xml += '</sObject>'
sobject_xml
end
|
#get_batch_result(batch_id) ⇒ Object
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
# File 'lib/salesforce_bulk_api/job.rb', line 202
def get_batch_result(batch_id)
path = "job/#{@job_id}/batch/#{batch_id}/result"
= Hash["Content-Type" => "application/xml; charset=UTF-8"]
response = @connection.get_request(nil, path, )
response_parsed = XmlSimple.xml_in(response)
results = response_parsed['result'] unless @operation == 'query'
if(@operation == 'query')
result_id = response_parsed["result"][0]
path = "job/#{@job_id}/batch/#{batch_id}/result/#{result_id}"
= Hash.new
= Hash["Content-Type" => "application/xml; charset=UTF-8"]
response = @connection.get_request(nil, path, )
response_parsed = XmlSimple.xml_in(response)
results = response_parsed['records']
end
results
end
|
#get_job_result(return_result, timeout) ⇒ Object
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
# File 'lib/salesforce_bulk_api/job.rb', line 168
def get_job_result(return_result, timeout)
begin
state = []
Timeout::timeout(timeout, SalesforceBulkApi::JobTimeout) do
while true
if self.check_job_status['state'][0] == 'Closed'
@batch_ids.each do |batch_id|
batch_state = self.check_batch_status(batch_id)
if batch_state['state'][0] != "Queued" && batch_state['state'][0] != "InProgress"
state << (batch_state)
@batch_ids.delete(batch_id)
end
end
break if @batch_ids.empty?
else
break
end
end
end
rescue SalesforceBulkApi::JobTimeout => e
puts 'Timeout waiting for Salesforce to process job batches #{@batch_ids} of job #{@job_id}.'
puts e
raise
end
state.each_with_index do |batch_state, i|
if batch_state['state'][0] == 'Completed' && return_result == true
state[i].merge!({'response' => self.get_batch_result(batch_state['id'][0])})
end
end
state
end
|