Class: Workhorse::Jobs::CleanupSucceededJobs

Inherits:
Object
  • Object
show all
Defined in:
lib/workhorse/jobs/cleanup_succeeded_jobs.rb

Overview

Job for cleaning up old succeeded jobs from the database. This maintenance job helps keep the jobs table from growing indefinitely by removing successfully completed jobs older than a specified age.

Examples:

Schedule cleanup job

Workhorse.enqueue(CleanupSucceededJobs.new(max_age: 30))

Daily cleanup with cron

# Clean up jobs older than 14 days every day at 2 AM
Workhorse.enqueue(CleanupSucceededJobs.new, perform_at: 1.day.from_now.beginning_of_day + 2.hours)

Instance Method Summary collapse

Constructor Details

#initialize(max_age: 14) ⇒ CleanupSucceededJobs

Instantiates a new job.

Parameters:

  • max_age (Integer) (defaults to: 14)

    The maximal age of jobs to retain, in days. Will be evaluated at perform time.



17
18
19
# File 'lib/workhorse/jobs/cleanup_succeeded_jobs.rb', line 17

def initialize(max_age: 14)
  @max_age = max_age
end

Instance Method Details

#performvoid

This method returns an undefined value.

Executes the cleanup by deleting old succeeded jobs.



24
25
26
27
28
29
# File 'lib/workhorse/jobs/cleanup_succeeded_jobs.rb', line 24

def perform
  age_limit = seconds_ago(@max_age)
  Workhorse::DbJob.where(
    'STATE = ? AND UPDATED_AT <= ?', Workhorse::DbJob::STATE_SUCCEEDED, age_limit
  ).delete_all
end