Class: Bosh::Director::Jobs::ScheduledOrphanCleanup

Inherits:
BaseJob show all
Defined in:
lib/bosh/director/jobs/scheduled_orphan_cleanup.rb

Instance Attribute Summary

Attributes inherited from BaseJob

#task_id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseJob

#begin_stage, #dns_manager, #event_manager, #logger, perform, #result_file, #single_step_stage, #task_cancelled?, #task_checkpoint, #track_and_log, #username

Constructor Details

#initialize(params = {}) ⇒ ScheduledOrphanCleanup

Returns a new instance of ScheduledOrphanCleanup.



23
24
25
26
27
28
# 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

Class Method Details

.has_work(params) ⇒ Object



10
11
12
13
# 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_typeObject



6
7
8
# File 'lib/bosh/director/jobs/scheduled_orphan_cleanup.rb', line 6

def self.job_type
  :scheduled_orphan_cleanup
end

.schedule_messageObject



19
20
21
# File 'lib/bosh/director/jobs/scheduled_orphan_cleanup.rb', line 19

def self.schedule_message
  "clean up orphan disks"
end

.time_days_ago(days) ⇒ Object



15
16
17
# File 'lib/bosh/director/jobs/scheduled_orphan_cleanup.rb', line 15

def self.time_days_ago(days)
  Time.now - (days * 24 * 60 * 60)
end

Instance Method Details

#performObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# 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