Class: RocketJob::Jobs::HousekeepingJob

Inherits:
RocketJob::Job show all
Includes:
Plugins::Cron
Defined in:
lib/rocket_job/jobs/housekeeping_job.rb

Overview

Applies Retention policies to how long jobs are kept.

Retentions are specific to each state so that for example completed jobs can be cleaned up before jobs that are running.

Only one active instance of this housekeeping job is permitted at a time.

Example:

RocketJob::Jobs::HousekeepingJob.create!

Example, with the default values that can be modified:

RocketJob::Jobs::HousekeepingJob.create!(
  aborted_retention:   7.days,
  completed_retention: 7.days,
  failed_retention:    14.days,
  paused_retention:    nil,
  queued_retention:    nil
)

Example, overriding defaults and disabling removal of failed jobs:

RocketJob::Jobs::HousekeepingJob.create!(
  aborted_retention:   1.day,
  completed_retention: 30.minutes,
  failed_retention:    nil
)

Instance Method Summary collapse

Methods included from Plugins::Cron

#rocket_job_cron_set_run_at

Methods included from Plugins::Job::Throttle

#throttle_filter_class, #throttle_filter_id

Methods included from Plugins::Job::Worker

#fail_on_exception!, #perform_now, #rocket_job_active_workers, #rocket_job_work

Methods included from Plugins::Job::StateMachine

#pausable?

Methods included from Plugins::Job::Persistence

#create_restart!, #reload, #save_with_retry!

Methods included from Plugins::Job::Model

#as_json, #duration, #expired?, #run_now!, #scheduled?, #scheduled_at, #seconds, #sleeping?, #status, #worker_count, #worker_names, #worker_on_server?

Instance Method Details

#performObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rocket_job/jobs/housekeeping_job.rb', line 47

def perform
  RocketJob::Job.aborted.where(completed_at: {"$lte" => aborted_retention.seconds.ago}).destroy_all if aborted_retention
  if completed_retention
    RocketJob::Job.completed.where(completed_at: {"$lte" => completed_retention.seconds.ago}).destroy_all
  end
  RocketJob::Job.failed.where(completed_at: {"$lte" => failed_retention.seconds.ago}).destroy_all if failed_retention
  RocketJob::Job.paused.where(completed_at: {"$lte" => paused_retention.seconds.ago}).destroy_all if paused_retention
  RocketJob::Job.queued.where(created_at: {"$lte" => queued_retention.seconds.ago}).destroy_all if queued_retention

  return unless destroy_zombies

  # Cleanup zombie servers
  RocketJob::Server.destroy_zombies
  # Requeue jobs where the worker is in the zombie state and its server has gone away
  RocketJob::ActiveWorker.requeue_zombies
end