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



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
283
284
285
286
287
288
289
# 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

#durationObject

Returns a human readable duration the job has taken



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

def duration
  RocketJob.seconds_as_duration(seconds)
end

#expired?Boolean

Returns [true|false] whether the job has expired

Returns:

  • (Boolean)


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

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

#run_now!Object

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



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

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)


223
224
225
# File 'lib/rocket_job/plugins/job/model.rb', line 223

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.



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

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



202
203
204
205
206
207
208
209
210
# 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?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)


229
230
231
# File 'lib/rocket_job/plugins/job/model.rb', line 229

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

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

Returns [Hash] the status of this job



292
293
294
295
296
297
298
299
300
301
302
303
304
# 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_countObject

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



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

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

#worker_namesObject

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



239
240
241
# 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) ⇒ Boolean

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

Returns:

  • (Boolean)


307
308
309
310
311
# 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