Module: RocketJob::Plugins::Job::Model

Extended by:
ActiveSupport::Concern
Included in:
Job
Defined in:
lib/rocket_job/plugins/job/model.rb

Overview

Prevent more than one instance of this job class from running at a time

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#as_jsonObject

Returns [Hash] status of this job



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/rocket_job/plugins/job/model.rb', line 206

def as_json
  attrs = serializable_hash(methods: [:seconds, :duration])
  attrs.delete('result') unless collect_output?
  attrs.delete('failure_count') unless failure_count > 0
  case
  when queued?
    attrs.delete('started_at')
    attrs.delete('completed_at')
    attrs.delete('result')
    attrs
  when running?
    attrs.delete('completed_at')
    attrs.delete('result')
    attrs
  when completed?
    attrs.delete('percent_complete')
    attrs
  when paused?
    attrs.delete('completed_at')
    attrs.delete('result')
    # Ensure 'paused_at' appears first in the hash
    {'paused_at' => completed_at}.merge(attrs)
  when aborted?
    attrs.delete('completed_at')
    attrs.delete('result')
    {'aborted_at' => completed_at}.merge(attrs)
  when failed?
    attrs.delete('completed_at')
    attrs.delete('result')
    {'failed_at' => completed_at}.merge(attrs)
  else
    attrs
  end
end

#collect_nil_output?Boolean

Returns [true|false] whether to collect nil results from running this batch

Returns:

  • (Boolean)


167
168
169
# File 'lib/rocket_job/plugins/job/model.rb', line 167

def collect_nil_output?
  collect_output? ? (collect_nil_output == true) : false
end

#collect_output?Boolean

Returns [true|false] whether to collect the results from running this batch

Returns:

  • (Boolean)


172
173
174
# File 'lib/rocket_job/plugins/job/model.rb', line 172

def collect_output?
  collect_output == true
end

#durationObject

Returns a human readable duration the job has taken



191
192
193
# File 'lib/rocket_job/plugins/job/model.rb', line 191

def duration
  RocketJob.seconds_as_duration(seconds)
end

#expired?Boolean

Returns [true|false] whether the job has expired

Returns:

  • (Boolean)


196
197
198
# File 'lib/rocket_job/plugins/job/model.rb', line 196

def expired?
  expires_at && (expires_at < Time.now)
end

#scheduled?Boolean

Returns [true|false] whether the job is scheduled to run in the future

Returns:

  • (Boolean)


201
202
203
# File 'lib/rocket_job/plugins/job/model.rb', line 201

def scheduled?
  queued? && run_at.present? && (run_at > Time.now)
end

#secondsObject

Returns [Float] the number of seconds the job has taken

  • Elapsed seconds to process the job from when a worker first started working on it until now if still running, or until it was completed

  • Seconds in the queue if queued



180
181
182
183
184
185
186
187
188
# File 'lib/rocket_job/plugins/job/model.rb', line 180

def seconds
  if completed_at
    completed_at - (started_at || created_at)
  elsif started_at
    Time.now - started_at
  else
    Time.now - created_at
  end
end

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

Returns [Hash] the status of this job



242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/rocket_job/plugins/job/model.rb', line 242

def status(time_zone = 'Eastern Time (US & Canada)')
  h = as_json
  h.delete('seconds')
  h.dup.each_pair do |k, v|
    case
    when v.is_a?(Time)
      h[k] = v.in_time_zone(time_zone).to_s
    when v.is_a?(BSON::ObjectId)
      h[k] = v.to_s
    end
  end
  h
end