module RocketJob::Batch::Statistics
Allow statistics to be gathered while a batch job is running.
Notes:
-
Statistics
for successfully processed records within a slice are saved. -
Statistics
gathered during a perform that then results in an exception are discarded.
Public Instance Methods
statistics_inc(key, increment = 1)
click to toggle source
Increment a statistic
# File lib/rocket_job/batch/statistics.rb, line 67 def statistics_inc(key, increment = 1) return if key.nil? || key == "" (@rocket_job_perform_statistics ||= []) << (key.is_a?(Hash) ? key : [key, increment]) end
Private Instance Methods
rocket_job_batch_log_payload()
click to toggle source
Overrides RocketJob::Batch::Logger#rocket_job_batch_log_payload
# File lib/rocket_job/batch/statistics.rb, line 101 def rocket_job_batch_log_payload h = { from: aasm.from_state, to: aasm.to_state, event: aasm.current_event } h[:statistics] = statistics.dup if statistics.present? && (completed? || failed?) h end
rocket_job_slice_statistics()
click to toggle source
# File lib/rocket_job/batch/statistics.rb, line 85 def rocket_job_slice_statistics @rocket_job_slice_statistics ||= Stats.new(new_record? ? statistics : nil) end
rocket_job_statistics_capture() { || ... }
click to toggle source
# File lib/rocket_job/batch/statistics.rb, line 75 def rocket_job_statistics_capture @rocket_job_perform_statistics = nil @rocket_job_slice_statistics = nil yield ensure if @rocket_job_slice_statistics && !@rocket_job_slice_statistics.empty? collection.update_one({_id: id}, {"$inc" => @rocket_job_slice_statistics.stats}) end end
rocket_job_statistics_commit()
click to toggle source
Apply stats gathered during the perform to the slice level stats
# File lib/rocket_job/batch/statistics.rb, line 90 def rocket_job_statistics_commit return unless @rocket_job_perform_statistics @rocket_job_perform_statistics.each do |key| key.is_a?(Hash) ? rocket_job_slice_statistics.inc(key) : rocket_job_slice_statistics.inc_key(*key) end @rocket_job_perform_statistics = nil end