module RocketJob::Plugins::Job::Model

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

Public Instance Methods

as_json() click to toggle source

Returns [Hash] status of this job

# File lib/rocket_job/plugins/job/model.rb, line 258
def as_json
  attrs = serializable_hash(methods: %i[seconds duration])
  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
duration() click to toggle source

Returns a human readable duration the job has taken

# File lib/rocket_job/plugins/job/model.rb, line 213
def duration
  RocketJob.seconds_as_duration(seconds)
end
expired?() click to toggle source

Returns [true|false] whether the job has expired

# File lib/rocket_job/plugins/job/model.rb, line 218
def expired?
  expires_at && (expires_at < Time.now)
end
run_now!() click to toggle source

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

# File lib/rocket_job/plugins/job/model.rb, line 244
def run_now!
  update_attributes(run_at: nil) if run_at
end
scheduled?() click to toggle source

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

# File lib/rocket_job/plugins/job/model.rb, line 223
def scheduled?
  queued? && run_at.present? && (run_at > Time.now)
end
scheduled_at() click to toggle source

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.

# File lib/rocket_job/plugins/job/model.rb, line 253
def scheduled_at
  run_at || created_at
end
seconds() click to toggle source

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

# File lib/rocket_job/plugins/job/model.rb, line 202
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?() click to toggle source

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

# File lib/rocket_job/plugins/job/model.rb, line 229
def sleeping?
  running? && worker_count.zero?
end
status(time_zone = "Eastern Time (US & Canada)") click to toggle source

Returns [Hash] the status of this job

# File lib/rocket_job/plugins/job/model.rb, line 292
def status(time_zone = "Eastern Time (US & Canada)")
  h = as_json
  h.delete("seconds")
  h.dup.each_pair do |k, v|
    case v
    when Time
      h[k] = v.in_time_zone(time_zone).to_s
    when BSON::ObjectId
      h[k] = v.to_s
    end
  end
  h
end
worker_count() click to toggle source

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

# File lib/rocket_job/plugins/job/model.rb, line 234
def worker_count
  running? && worker_name.present? ? 1 : 0
end
worker_names() click to toggle source

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

# File lib/rocket_job/plugins/job/model.rb, line 239
def worker_names
  running? && worker_name.present? ? [worker_name] : []
end
worker_on_server?(server_name) click to toggle source

Returns [true|false] whether the worker runs on a particular server.

# File lib/rocket_job/plugins/job/model.rb, line 307
def worker_on_server?(server_name)
  return false unless worker_name.present? && server_name.present?

  worker_name.start_with?(server_name)
end