Class: SalesforceBulkClient::Job

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Job

Returns a new instance of Job.



9
10
11
12
13
14
15
16
17
# File 'lib/salesforce_bulk_client/job.rb', line 9

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.



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

def job_id
  @job_id
end

Instance Method Details

#add_batch(batch) ⇒ Object



46
47
48
49
# File 'lib/salesforce_bulk_client/job.rb', line 46

def add_batch(batch)
  add_batch_result = @connection.post_request("job/#{@job_id}/batch", batch)
  add_batch_result.id
end

#add_batchesObject



39
40
41
42
43
44
# File 'lib/salesforce_bulk_client/job.rb', line 39

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 << add_batch(batch)
  end
end

#add_queryObject



34
35
36
37
# File 'lib/salesforce_bulk_client/job.rb', line 34

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



55
56
57
# File 'lib/salesforce_bulk_client/job.rb', line 55

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

#check_job_statusObject



51
52
53
# File 'lib/salesforce_bulk_client/job.rb', line 51

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

#close_jobObject



29
30
31
32
# File 'lib/salesforce_bulk_client/job.rb', line 29

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

#create_job(batch_size) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/salesforce_bulk_client/job.rb', line 19

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



116
117
118
119
120
121
122
123
124
125
# File 'lib/salesforce_bulk_client/job.rb', line 116

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



103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/salesforce_bulk_client/job.rb', line 103

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



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/salesforce_bulk_client/job.rb', line 63

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 != '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_state, i|
    if batch_state.state == 'Completed' && return_result == true
      batch_infos[i].merge!({ 'response' => self.get_batch_result(batch_state.id)})
    end
  end

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

#list_batchesObject



59
60
61
# File 'lib/salesforce_bulk_client/job.rb', line 59

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