Class: SalesforceBulkQuery::Batch

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Batch

Returns a new instance of Batch.



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/salesforce_bulk_query/batch.rb', line 10

def initialize(options)
  @sobject = options[:sobject]
  @soql = options[:soql]
  @job_id = options[:job_id]
  @connection = options[:connection]
  @date_field = options[:date_field] or fail "date_field must be given when creating a batch"
  @start = options[:start]
  @stop = options[:stop]
  @logger = options[:logger]
  @@directory_path ||= Dir.mktmpdir
  @filename = nil
end

Instance Attribute Details

#batch_idObject (readonly)

Returns the value of attribute batch_id.



23
24
25
# File 'lib/salesforce_bulk_query/batch.rb', line 23

def batch_id
  @batch_id
end

#csv_record_countObject (readonly)

Returns the value of attribute csv_record_count.



23
24
25
# File 'lib/salesforce_bulk_query/batch.rb', line 23

def csv_record_count
  @csv_record_count
end

#fail_messageObject (readonly)

Returns the value of attribute fail_message.



23
24
25
# File 'lib/salesforce_bulk_query/batch.rb', line 23

def fail_message
  @fail_message
end

#filenameObject (readonly)

Returns the value of attribute filename.



23
24
25
# File 'lib/salesforce_bulk_query/batch.rb', line 23

def filename
  @filename
end

#soqlObject (readonly)

Returns the value of attribute soql.



23
24
25
# File 'lib/salesforce_bulk_query/batch.rb', line 23

def soql
  @soql
end

#startObject (readonly)

Returns the value of attribute start.



23
24
25
# File 'lib/salesforce_bulk_query/batch.rb', line 23

def start
  @start
end

#stopObject (readonly)

Returns the value of attribute stop.



23
24
25
# File 'lib/salesforce_bulk_query/batch.rb', line 23

def stop
  @stop
end

Instance Method Details

#check_statusObject

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



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
78
# File 'lib/salesforce_bulk_query/batch.rb', line 36

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

#createObject

Do the api request



26
27
28
29
30
31
32
# File 'lib/salesforce_bulk_query/batch.rb', line 26

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_filenameObject



80
81
82
# File 'lib/salesforce_bulk_query/batch.rb', line 80

def get_filename
  return "#{@sobject}_#{@date_field}_#{@start}_#{@stop}_#{@batch_id}.csv"
end

#get_result(options = {}) ⇒ Object



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
117
# File 'lib/salesforce_bulk_query/batch.rb', line 84

def get_result(options={})
  # 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 = options[:directory_path]
  skip_verification = options[: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_logObject



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

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

#verifyObject



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/salesforce_bulk_query/batch.rb', line 119

def verify
  api_count = @connection.query_count(@sobject, @date_field, @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