Class: SalesforceBulkQuery::Batch
- Inherits:
-
Object
- Object
- SalesforceBulkQuery::Batch
- Defined in:
- lib/salesforce_bulk_query/batch.rb
Overview
Represents a Salesforce api batch. Batch contains a single subquery. Many batches are contained in a Job.
Instance Attribute Summary collapse
-
#batch_id ⇒ Object
readonly
Returns the value of attribute batch_id.
-
#csv_record_count ⇒ Object
readonly
Returns the value of attribute csv_record_count.
-
#fail_message ⇒ Object
readonly
Returns the value of attribute fail_message.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#soql ⇒ Object
readonly
Returns the value of attribute soql.
-
#start ⇒ Object
readonly
Returns the value of attribute start.
-
#stop ⇒ Object
readonly
Returns the value of attribute stop.
Instance Method Summary collapse
-
#check_status ⇒ Object
check status of the batch if it fails, don’t throw an error now, let the job above collect all fails and raise it at once.
-
#create ⇒ Object
Do the api request.
- #get_filename ⇒ Object
- #get_result(options = {}) ⇒ Object
-
#initialize(options) ⇒ Batch
constructor
A new instance of Batch.
- #to_log ⇒ Object
- #verify ⇒ Object
Constructor Details
#initialize(options) ⇒ Batch
Returns a new instance of Batch.
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/salesforce_bulk_query/batch.rb', line 10 def initialize() @sobject = [:sobject] @soql = [:soql] @job_id = [:job_id] @connection = [:connection] @start = [:start] @stop = [:stop] @logger = [:logger] @@directory_path ||= Dir.mktmpdir @filename = nil end |
Instance Attribute Details
#batch_id ⇒ Object (readonly)
Returns the value of attribute batch_id.
22 23 24 |
# File 'lib/salesforce_bulk_query/batch.rb', line 22 def batch_id @batch_id end |
#csv_record_count ⇒ Object (readonly)
Returns the value of attribute csv_record_count.
22 23 24 |
# File 'lib/salesforce_bulk_query/batch.rb', line 22 def csv_record_count @csv_record_count end |
#fail_message ⇒ Object (readonly)
Returns the value of attribute fail_message.
22 23 24 |
# File 'lib/salesforce_bulk_query/batch.rb', line 22 def @fail_message end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
22 23 24 |
# File 'lib/salesforce_bulk_query/batch.rb', line 22 def filename @filename end |
#soql ⇒ Object (readonly)
Returns the value of attribute soql.
22 23 24 |
# File 'lib/salesforce_bulk_query/batch.rb', line 22 def soql @soql end |
#start ⇒ Object (readonly)
Returns the value of attribute start.
22 23 24 |
# File 'lib/salesforce_bulk_query/batch.rb', line 22 def start @start end |
#stop ⇒ Object (readonly)
Returns the value of attribute stop.
22 23 24 |
# File 'lib/salesforce_bulk_query/batch.rb', line 22 def stop @stop end |
Instance Method Details
#check_status ⇒ Object
check status of the batch if it fails, don’t throw an error now, let the job above collect all fails and raise it at once
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/salesforce_bulk_query/batch.rb', line 35 def check_status succeeded = nil failed = nil # get the status of the batch # https://www.salesforce.com/us/developer/docs/api_asynch/Content/asynch_api_batches_get_info.htm status_path = "job/#{@job_id}/batch/#{@batch_id}" status_response = @connection.get_xml(status_path) # interpret the status @status = status_response['state'][0] # https://www.salesforce.com/us/developer/docs/api_asynch/Content/asynch_api_batches_interpret_status.htm case @status when 'Failed' failed = true @fail_message = status_response['stateMessage'] when 'InProgress', 'Queued' succeeded = false when 'Completed' succeeded = true failed = false else fail "Something weird happened, #{@batch_id} has status #{@status}." end if succeeded # request to get the result id # https://www.salesforce.com/us/developer/docs/api_asynch/Content/asynch_api_batches_get_results.htm path = "job/#{@job_id}/batch/#{@batch_id}/result" response_parsed = @connection.get_xml(path) @result_id = response_parsed["result"] ? response_parsed["result"][0] : nil end return { :failed => failed, :fail_message => @fail_message, :succeeded => succeeded, :result_id => @result_id } end |
#create ⇒ Object
Do the api request
25 26 27 28 29 30 31 |
# File 'lib/salesforce_bulk_query/batch.rb', line 25 def create path = "job/#{@job_id}/batch/" response_parsed = @connection.post_xml(path, @soql, {:csv_content_type => true}) @batch_id = response_parsed['id'][0] end |
#get_filename ⇒ Object
79 80 81 |
# File 'lib/salesforce_bulk_query/batch.rb', line 79 def get_filename return "#{@sobject}_#{@start}_#{@stop}_#{@batch_id}.csv" end |
#get_result(options = {}) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/salesforce_bulk_query/batch.rb', line 83 def get_result(={}) # if it was already downloaded, no one should ask about it if @filename raise "This batch was already downloaded once: #{@filename}, #{@batch_id}" end directory_path = [:directory_path] skip_verification = [:skip_verification] # request to get the actual results path = "job/#{@job_id}/batch/#{@batch_id}/result/#{@result_id}" if !@result_id raise "batch #{@batch_id} not finished yet, trying to get result: #{path}" end directory_path ||= @@directory_path # write it to a file @filename = File.join(directory_path, get_filename) @connection.get_to_file(path, @filename) # Verify the number of downloaded records is roughly the same as # count on the soql api # maybe also verify unless skip_verification verify end @logger.debug "get_result :verification : #{@verification}" if @logger return { :filename => @filename, :verification => @verification } end |
#to_log ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/salesforce_bulk_query/batch.rb', line 139 def to_log return { :sobject => @sobject, :soql => @soql, :job_id => @job_id, :connection => @connection.to_log, :start => @start, :stop => @stop, :directory_path => @@directory_path } end |
#verify ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/salesforce_bulk_query/batch.rb', line 118 def verify api_count = @connection.query_count(@sobject, @start, @stop) # if we weren't able to get the count, fail. if api_count.nil? return @verification = false end # count the records in the csv @csv_record_count = Utils.line_count(@filename) if @logger && @csv_record_count > 0 && @csv_record_count % 100 == 0 @logger.warn "The line count for batch id #{@batch_id} soql #{@soql} is highly suspicious: #{@csv_record_count}" end if @logger && @csv_record_count != api_count @logger.warn "The counts for batch id #{@batch_id}, soql #{@soql} don't match. Record count in downloaded csv #{@csv_record_count}, record count on api count(): #{api_count}" @logger.info "verify result: #{@csv_record_count >= api_count}" end @verification = (@csv_record_count >= api_count) end |