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



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/rocket_job/plugins/job/model.rb', line 249

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)


210
211
212
# File 'lib/rocket_job/plugins/job/model.rb', line 210

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)


215
216
217
# File 'lib/rocket_job/plugins/job/model.rb', line 215

def collect_output?
  collect_output == true
end

#durationObject

Returns a human readable duration the job has taken



234
235
236
# File 'lib/rocket_job/plugins/job/model.rb', line 234

def duration
  RocketJob.seconds_as_duration(seconds)
end

#expired?Boolean

Returns [true|false] whether the job has expired

Returns:

  • (Boolean)


239
240
241
# File 'lib/rocket_job/plugins/job/model.rb', line 239

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)


244
245
246
# File 'lib/rocket_job/plugins/job/model.rb', line 244

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



223
224
225
226
227
228
229
230
231
# File 'lib/rocket_job/plugins/job/model.rb', line 223

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



285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/rocket_job/plugins/job/model.rb', line 285

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

#worker_on_server?(server_name) ⇒ Boolean

Returns [Boolean] whether the worker runs on a particular server.

Returns:

  • (Boolean)


300
301
302
303
# File 'lib/rocket_job/plugins/job/model.rb', line 300

def worker_on_server?(server_name)
  return false unless worker_name.present? && server_name.present?
  worker_name.start_with?(server_name)
end