Module: RocketJob::Batch::Model

Extended by:
ActiveSupport::Concern
Included in:
RocketJob::Batch
Defined in:
lib/rocket_job/batch/model.rb

Overview

Model attributes

Instance Method Summary collapse

Instance Method Details

#percent_completeObject

Returns [Integer] percent of records completed so far Returns 0 if the total record count has not yet been set



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rocket_job/batch/model.rb', line 37

def percent_complete
  return 100 if completed?
  return 0 unless record_count.to_i.positive?

  # Approximate number of input records
  input_records = input.count.to_f * input_category.slice_size
  if input_records > record_count
    # Sanity check in case slice_size is not being adhered to
    0
  else
    ((1.0 - (input_records.to_f / record_count)) * 100).to_i
  end
end

#status(time_zone = "Eastern Time (US & Canada)") ⇒ Object

Returns [Hash] status of this job



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
# File 'lib/rocket_job/batch/model.rb', line 52

def status(time_zone = "Eastern Time (US & Canada)")
  h = {}
  if queued?
    h["queued_slices"] = input.queued.count
  elsif running? || paused? || failed?
    h["active_slices"] = worker_count
    h["failed_slices"] = input.failed.count
    h["queued_slices"] = input.queued.count
    output_categories.each do |category|
      name_str                      = category.name == :main ? "" : "_#{category.name}"
      h["output_slices#{name_str}"] = output(category).count
    end
    # Very high level estimated time left
    if record_count && running? && record_count.positive?
      percent = percent_complete
      if percent >= 5
        secs                        = seconds.to_f
        h["est_remaining_duration"] = RocketJob.seconds_as_duration((((secs / percent) * 100) - secs))
      end
    end
  elsif completed?
    secs                  = seconds.to_f
    h["records_per_hour"] = ((record_count.to_f / secs) * 60 * 60).round if record_count&.positive? && (secs > 0.0)
  end
  h.merge!(super(time_zone))
  h.delete("result")
  # Worker name should be retrieved from the slices when processing
  h.delete("worker_name") if sub_state == :processing
  h
end

#upload_file_nameObject

Deprecated.

For backward compatibility



118
119
120
# File 'lib/rocket_job/batch/model.rb', line 118

def upload_file_name
  input_category.file_name
end

#upload_file_name=(upload_file_name) ⇒ Object

Deprecated.

For backward compatibility



124
125
126
# File 'lib/rocket_job/batch/model.rb', line 124

def upload_file_name=(upload_file_name)
  input_category.file_name = upload_file_name
end

#worker_countObject

Returns [Integer] the number of workers currently working on this job.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rocket_job/batch/model.rb', line 98

def worker_count
  return 0 unless running?
  # Cache the number of workers for 1 second.
  return @worker_count if @worker_count_last && (@worker_count_last == Time.now.to_i)

  @worker_count      =
    case sub_state
    when :before, :after
      1
    when :processing
      input.running.count
    else
      0
    end
  @worker_count_last = Time.now.to_i
  @worker_count
end

#worker_namesObject

Returns [Array<String>] names of workers currently working this job.



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rocket_job/batch/model.rb', line 84

def worker_names
  return [] unless running?

  case sub_state
  when :before, :after
    [worker_name]
  when :processing
    input.running.collect(&:worker_name)
  else
    []
  end
end