Class: SalesforceBulkClient::Job

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

Constant Summary collapse

BATCH_CHARACTER_LIMIT =
10000000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Job

Returns a new instance of Job.



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

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.



10
11
12
# File 'lib/salesforce_bulk_client/job.rb', line 10

def job_id
  @job_id
end

Instance Method Details

#add_batch(batch) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/salesforce_bulk_client/job.rb', line 49

def add_batch(batch)
  batch_ids = []
  batch_groups = []
  batch_size = MultiJson.dump(batch).size
  if batch_size <= BATCH_CHARACTER_LIMIT
    batch_groups << batch
  else

    # Split batch into sub-batches
    batch_group = []
    batch_group_size = MultiJson.dump(batch_group).size
    batch.each do |record|
      record_size = MultiJson.dump(record).size
      if batch_group_size + record_size + 1 > BATCH_CHARACTER_LIMIT
        batch_groups << batch_group.dup
        batch_group.clear
        batch_group_size = MultiJson.dump(batch_group).size
      end
      batch_group << record.dup
      batch_group_size += record_size + 1
    end
    
    # Add remaining records
    if !batch_group.empty?
      batch_groups << batch_group.dup
      batch_group.clear
    end

  end

  batch_groups.each do |batch_group|
    add_batch_result = @connection.post_request("job/#{@job_id}/batch", batch_group)
    batch_ids << add_batch_result.id
  end

  batch_ids
end

#add_batchesObject



42
43
44
45
46
47
# File 'lib/salesforce_bulk_client/job.rb', line 42

def add_batches
  raise 'Records must be an array of hashes.' unless @records.is_a? Array
  @records.each_slice(@batch_size) do |batch|
    @batch_ids.concat(add_batch(batch))
  end
end

#add_queryObject



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

def add_query
  add_query_result = @connection.post_request("job/#{@job_id}/batch", @records, false)
  @batch_ids << add_query_result.id
end

#check_batch_status(batch_id) ⇒ Object



91
92
93
# File 'lib/salesforce_bulk_client/job.rb', line 91

def check_batch_status(batch_id)
  @connection.get_request("job/#{@job_id}/batch/#{batch_id}")
end

#check_job_statusObject



87
88
89
# File 'lib/salesforce_bulk_client/job.rb', line 87

def check_job_status
  @connection.get_request("job/#{@job_id}")
end

#close_jobObject



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

def close_job
  close_job_request = { state: 'Closed' }
  @connection.post_request("job/#{@job_id}", close_job_request)
end

#create_job(batch_size) ⇒ Object



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

def create_job(batch_size)
  @batch_size = batch_size
  create_job_request = { operation: @operation.to_s.downcase, object: @sobject, contentType: 'JSON' }
  if !@external_field.nil?
    create_job_request[:externalIdFieldName] = @external_field
  end
  create_job_result = @connection.post_request('job', create_job_request)
  @job_id = create_job_result.id
end

#each_batch(timeout = 3600, poll_delay = 5) ⇒ Object



152
153
154
155
156
157
158
159
160
161
# File 'lib/salesforce_bulk_client/job.rb', line 152

def each_batch(timeout = 3600, poll_delay = 5)
  job_result = self.get_job_result(false, timeout, poll_delay)
  job_result.batches.each do |batch_info|
    batch_result = nil
    if batch_info.state == 'Completed'
      batch_result = self.get_batch_result(batch_info.id)
    end
    yield(batch_info, batch_result)
  end
end

#get_batch_result(batch_id) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/salesforce_bulk_client/job.rb', line 139

def get_batch_result(batch_id)
  batch_results = @connection.get_request("job/#{@job_id}/batch/#{batch_id}/result")
  results = []
  if @operation.to_s != 'query'
    results = batch_results
  else
    batch_results.each do |batch_result_id|
      results.concat(@connection.get_request("job/#{@job_id}/batch/#{batch_id}/result/#{batch_result_id}"))
    end
  end
  results
end

#get_job_result(return_result, timeout, poll_delay) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
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_bulk_client/job.rb', line 99

def get_job_result(return_result, timeout, poll_delay)
  batch_infos = []
  polling_started = false
  polling_completed = false
  FirePoll.poll("Timeout waiting for Salesforce to process job batches #{@batch_ids} of job #{@job_id}.", timeout) do
    sleep poll_delay if polling_started
    polling_started = true
    job_status = self.check_job_status
    if job_status.state == 'Closed'
      batch_info_map = {}

      batches_ready = @batch_ids.all? do |batch_id|
        batch_info = batch_info_map[batch_id] = self.check_batch_status(batch_id)
        batch_info.state != 'Queued' && batch_info.state != 'InProgress'
      end

      if batches_ready
        @batch_ids.each do |batch_id|
          batch_infos.insert(0, batch_info_map[batch_id])
          @batch_ids.delete(batch_id)
        end
      end
      polling_completed = true if @batch_ids.empty?
    else
      polling_completed = true
    end
    polling_completed
  end
  job_status = self.check_job_status

  batch_infos.each_with_index do |batch_info, i|
    if batch_info.state == 'Completed' && return_result == true
      batch_infos[i].merge!({ 'response' => self.get_batch_result(batch_info.id)})
    end
  end

  job_status.merge!({ 'batches' => batch_infos })
  job_status
end

#list_batchesObject



95
96
97
# File 'lib/salesforce_bulk_client/job.rb', line 95

def list_batches
  @connection.get_request("job/#{@job_id}/batch")&.batchInfo
end