Class: SalesforceBulk2::Job

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

Constant Summary collapse

@@fields =
[:id, :operation, :object, :createdById, :state, :createdDate, 
:systemModstamp, :externalIdFieldName, :concurrencyMode, :contentType, 
:numberBatchesQueued, :numberBatchesInProgress, :numberBatchesCompleted, 
:numberBatchesFailed, :totalBatches, :retries, :numberRecordsProcessed, 
:numberRecordsFailed, :totalProcessingTime, :apiActiveProcessingTime, 
:apexProcessingTime, :apiVersion]
@@valid_operations =
[:delete, :insert, :update, :upsert, :query]
@@valid_concurrency_modes =
['Parallel', 'Serial']

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Job

Returns a new instance of Job.



74
75
76
# File 'lib/salesforce_bulk2/job.rb', line 74

def initialize client
  @client = client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



3
4
5
# File 'lib/salesforce_bulk2/job.rb', line 3

def client
  @client
end

#concurrency_modeObject (readonly)

Returns the value of attribute concurrency_mode.



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

def concurrency_mode
  @concurrency_mode
end

#dataObject (readonly)

Returns the value of attribute data.



7
8
9
# File 'lib/salesforce_bulk2/job.rb', line 7

def data
  @data
end

#external_idObject (readonly)

Returns the value of attribute external_id.



6
7
8
# File 'lib/salesforce_bulk2/job.rb', line 6

def external_id
  @external_id
end

#xml_dataObject (readonly)

Returns the value of attribute xml_data.



8
9
10
# File 'lib/salesforce_bulk2/job.rb', line 8

def xml_data
  @xml_data
end

Class Method Details

.create(client, options = {}) ⇒ Object

Raises:

  • (ArgumentError)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/salesforce_bulk2/job.rb', line 32

def self.create client, options = {}
  job = Job.new(client)

  options.assert_valid_keys(:external_id, :concurrency_mode, :object, :operation)

  operation = options[:operation].to_sym.downcase
  raise ArgumentError.new("Invalid operation: #{operation}") unless Job.valid_operation?(operation)

  external_id = options[:external_id]
  concurrency_mode = options[:concurrency_mode]
  object = options[:object]
  
  if concurrency_mode
    concurrency_mode = concurrency_mode.capitalize
    raise ArgumentError.new("Invalid concurrency mode: #{concurrency_mode}") unless Job.valid_concurrency_mode?(concurrency_mode)
  end
  
  xml  = '<?xml version="1.0" encoding="utf-8"?>'
  xml += '<jobInfo xmlns="http://www.force.com/2009/06/asyncapi/dataload">'
  xml += "  <operation>#{operation}</operation>"
  xml += "  <object>#{object}</object>" if object
  xml += "  <externalIdFieldName>#{external_id}</externalIdFieldName>" if external_id
  xml += "  <concurrencyMode>#{concurrency_mode}</concurrencyMode>" if concurrency_mode
  xml += "  <contentType>CSV</contentType>"
  xml += "</jobInfo>"

  job.update(client.http_post_xml("job", xml))
  job
end

.find(client, id) ⇒ Object



62
63
64
65
66
67
# File 'lib/salesforce_bulk2/job.rb', line 62

def self.find client, id
  Job.new(client)
  job.id = id
  job.refresh
  job
end

.valid_concurrency_mode?(mode) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/salesforce_bulk2/job.rb', line 28

def self.valid_concurrency_mode? mode
  @@valid_concurrency_modes.include?(concurrency_mode)
end

.valid_operation?(operation) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/salesforce_bulk2/job.rb', line 24

def self.valid_operation? operation
  @@valid_operations.include?(operation)
end

Instance Method Details

#abortObject



94
95
96
97
98
99
100
101
# File 'lib/salesforce_bulk2/job.rb', line 94

def abort
  xml  = '<?xml version="1.0" encoding="utf-8"?>'
  xml += '<jobInfo xmlns="http://www.force.com/2009/06/asyncapi/dataload">'
  xml += '  <state>Aborted</state>'
  xml += '</jobInfo>'
  
  @client.http_post_xml("job/#{@id}", xml)
end

#aborted?Boolean

Returns:

  • (Boolean)


173
174
175
# File 'lib/salesforce_bulk2/job.rb', line 173

def aborted?
  state? 'Aborted'
end

#add_data(data, batch_size = nil) ⇒ Object



139
140
141
142
143
# File 'lib/salesforce_bulk2/job.rb', line 139

def add_data data, batch_size = nil
  data.each_slice(batch_size || Batch.batch_size) do |records|
    new_batch(records)
  end
end

#batches_finished?Boolean

Statuses

Returns:

  • (Boolean)


158
159
160
161
# File 'lib/salesforce_bulk2/job.rb', line 158

def batches_finished?
  (@number_batches_queued == 0) and
  (@number_batches_in_progress == 0)
end

#closeObject



103
104
105
106
107
108
109
110
# File 'lib/salesforce_bulk2/job.rb', line 103

def close
  xml  = '<?xml version="1.0" encoding="utf-8"?>'
  xml += '<jobInfo xmlns="http://www.force.com/2009/06/asyncapi/dataload">'
  xml += '  <state>Closed</state>'
  xml += '</jobInfo>'
  
  @client.http_post_xml("job/#{@id}", xml)
end

#closed?Boolean

Returns:

  • (Boolean)


177
178
179
# File 'lib/salesforce_bulk2/job.rb', line 177

def closed?
  state? 'Closed'
end

#failed?Boolean

Returns:

  • (Boolean)


169
170
171
# File 'lib/salesforce_bulk2/job.rb', line 169

def failed?
  state? 'Failed'
end

#finished?Boolean

Returns:

  • (Boolean)


163
164
165
166
167
# File 'lib/salesforce_bulk2/job.rb', line 163

def finished?
  failed?  or 
  aborted? or 
  (closed? and batches_finished?)
end

#get_batchesObject



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/salesforce_bulk2/job.rb', line 82

def get_batches
  result = @client.http_get_xml("job/#{@id}/batch")

  if result['batchInfo'].is_a?(Array)
    result['batchInfo'].collect { |info| Batch.new(self, info) }
  elsif result['batchInfo']
    [Batch.new(self, result['batchInfo'])]
  else
    []
  end
end

#get_resultsObject



145
146
147
148
149
# File 'lib/salesforce_bulk2/job.rb', line 145

def get_results
  results = BatchResultCollection.new
  get_batches.each { |batch| results << batch.get_result }
  results.flatten
end

#new_batch(data) ⇒ Object



78
79
80
# File 'lib/salesforce_bulk2/job.rb', line 78

def new_batch data
  Batch.create(self, data)
end

#open?Boolean

Returns:

  • (Boolean)


181
182
183
# File 'lib/salesforce_bulk2/job.rb', line 181

def open?
  state? 'Open'
end

#refreshObject



69
70
71
72
# File 'lib/salesforce_bulk2/job.rb', line 69

def refresh
  xml_data = @client.http_get_xml("job/#{@id}")
  update(xml_data)
end

#state?(value) ⇒ Boolean

Returns:

  • (Boolean)


185
186
187
# File 'lib/salesforce_bulk2/job.rb', line 185

def state?(value)
  @state.present? && @state.casecmp(value) == 0
end

#update(xml_data) ⇒ 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
136
137
# File 'lib/salesforce_bulk2/job.rb', line 112

def update xml_data
  #Assign object
  @xml_data = xml_data

  #Mass assign the defaults
  @@fields.each do |field|
    instance_variable_set(:"@#{field}", xml_data[field.to_s])
  end

  #Special cases and data formats
  @created_date = DateTime.parse(xml_data['createdDate'])
  @system_modstamp = DateTime.parse(xml_data['systemModstamp'])

  @retries = xml_data['retries'].to_i
  @api_version = xml_data['apiVersion'].to_i
  @number_batches_queued = xml_data['numberBatchesQueued'].to_i
  @number_batches_in_progress = xml_data['numberBatchesInProgress'].to_i
  @number_batches_completed = xml_data['numberBatchesCompleted'].to_i
  @number_batches_failed = xml_data['numberBatchesFailed'].to_i
  @total_batches = xml_data['totalBatches'].to_i
  @number_records_processed = xml_data['numberRecordsProcessed'].to_i
  @number_records_failed = xml_data['numberRecordsFailed'].to_i
  @total_processing_time = xml_data['totalProcessingTime'].to_i
  @api_active_processing_time = xml_data['apiActiveProcessingTime'].to_i
  @apex_processing_time = xml_data['apexProcessingTime'].to_i
end