class Bosh::Director::Jobs::ScheduledOrphanCleanup

Public Class Methods

has_work(params) click to toggle source
# File lib/bosh/director/jobs/scheduled_orphan_cleanup.rb, line 10
def self.has_work(params)
  time = time_days_ago(params.first['max_orphaned_age_in_days'])
  Models::OrphanDisk.where('created_at < ?', time).any?
end
job_type() click to toggle source
# File lib/bosh/director/jobs/scheduled_orphan_cleanup.rb, line 6
def self.job_type
  :scheduled_orphan_cleanup
end
new(params = {}) click to toggle source
# File lib/bosh/director/jobs/scheduled_orphan_cleanup.rb, line 23
def initialize(params = {})
  logger.debug("ScheduledOrphanCleanup initialized with params: #{params.inspect}")
  @max_orphaned_age_in_days = params['max_orphaned_age_in_days']
  cloud = params.fetch(:cloud) { Config.cloud }
  @disk_manager = DiskManager.new(cloud, logger)
end
schedule_message() click to toggle source
# File lib/bosh/director/jobs/scheduled_orphan_cleanup.rb, line 19
def self.schedule_message
  "clean up orphan disks"
end
time_days_ago(days) click to toggle source
# File lib/bosh/director/jobs/scheduled_orphan_cleanup.rb, line 15
def self.time_days_ago(days)
  Time.now - (days * 24 * 60 * 60)
end

Public Instance Methods

perform() click to toggle source
# File lib/bosh/director/jobs/scheduled_orphan_cleanup.rb, line 30
def perform
  time = self.class.time_days_ago(@max_orphaned_age_in_days)
  logger.info("Started cleanup of orphan disks and orphan snapshots older than #{time}")

  old_orphans = Models::OrphanDisk.where('created_at < ?', time)
  old_orphans_count = old_orphans.count
  stage = Config.event_log.begin_stage('Deleting orphan disks', old_orphans_count)
  old_orphans.each do |old_orphan|
    stage.advance_and_track("#{old_orphan.disk_cid}") do
      @disk_manager.delete_orphan_disk(old_orphan)
    end
  end
  "Deleted #{old_orphans_count} orphaned disk(s) older than #{time}"
end