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



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'lib/rocket_job/plugins/job/model.rb', line 283

def as_json
  attrs = serializable_hash(methods: %i[seconds duration])
  attrs.delete("result") unless collect_output?
  attrs.delete("failure_count") unless failure_count.positive?
  if queued?
    attrs.delete("started_at")
    attrs.delete("completed_at")
    attrs.delete("result")
    attrs
  elsif running?
    attrs.delete("completed_at")
    attrs.delete("result")
    attrs
  elsif completed?
    attrs.delete("percent_complete")
    attrs
  elsif paused?
    attrs.delete("completed_at")
    attrs.delete("result")
    # Ensure 'paused_at' appears first in the hash
    {"paused_at" => completed_at}.merge(attrs)
  elsif aborted?
    attrs.delete("completed_at")
    attrs.delete("result")
    {"aborted_at" => completed_at}.merge(attrs)
  elsif 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)


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

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)


219
220
221
# File 'lib/rocket_job/plugins/job/model.rb', line 219

def collect_output?
  collect_output == true
end

#durationObject

Returns a human readable duration the job has taken



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

def duration
  RocketJob.seconds_as_duration(seconds)
end

#expired?Boolean

Returns [true|false] whether the job has expired

Returns:

  • (Boolean)


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

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

#run_now!Object

Clear ‘run_at` so that this job will run now.



269
270
271
# File 'lib/rocket_job/plugins/job/model.rb', line 269

def run_now!
  update_attributes(run_at: nil) if run_at
end

#scheduled?Boolean

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

Returns:

  • (Boolean)


248
249
250
# File 'lib/rocket_job/plugins/job/model.rb', line 248

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

#scheduled_atObject

Returns [Time] at which this job was intended to run at.

Takes into account any delays that could occur. Recommended to use this Time instead of Time.now in the ‘#perform` since the job could run outside its intended window. Especially if a failed job is only retried quite sometime later.



278
279
280
# File 'lib/rocket_job/plugins/job/model.rb', line 278

def scheduled_at
  run_at || created_at
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



227
228
229
230
231
232
233
234
235
# File 'lib/rocket_job/plugins/job/model.rb', line 227

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

#sleeping?Boolean

Return [true|false] whether this job is sleeping. I.e. No workers currently working on this job even if it is running.

Returns:

  • (Boolean)


254
255
256
# File 'lib/rocket_job/plugins/job/model.rb', line 254

def sleeping?
  running? && worker_count.zero?
end

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

Returns [Hash] the status of this job



318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/rocket_job/plugins/job/model.rb', line 318

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

#worker_countObject

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



259
260
261
# File 'lib/rocket_job/plugins/job/model.rb', line 259

def worker_count
  running? && worker_name.present? ? 1 : 0
end

#worker_namesObject

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



264
265
266
# File 'lib/rocket_job/plugins/job/model.rb', line 264

def worker_names
  running? && worker_name.present? ? [worker_name] : []
end

#worker_on_server?(server_name) ⇒ Boolean

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

Returns:

  • (Boolean)


332
333
334
335
336
# File 'lib/rocket_job/plugins/job/model.rb', line 332

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

  worker_name.start_with?(server_name)
end