Class: Debci::StorageLimit

Inherits:
Object
  • Object
show all
Defined in:
lib/debci/storage_limit.rb

Defined Under Namespace

Classes: CLI

Instance Method Summary collapse

Instance Method Details

#cleanup_package(package) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/debci/storage_limit.rb', line 29

def cleanup_package(package)
  storage = 0
  package.jobs.where(files_purged: false).order("date DESC").in_batches.each do |subset|
    subset.each do |job|
      storage += job.disk_usage
      if storage > storage_limit
        job.cleanup(reason: "package taking too much disk space")
      end
    end
  end
end

#packages_with_excess_storageObject



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/debci/storage_limit.rb', line 11

def packages_with_excess_storage
  expiration = Time.now - Debci.config.data_retention.days
  Debci::Package.find_by_sql("
    SELECT packages.*
    FROM packages
    JOIN jobs ON packages.id = jobs.package_id
    WHERE jobs.date > '#{expiration}' AND NOT jobs.files_purged
    GROUP BY packages.id
    HAVING sum(jobs.log_size + jobs.artifacts_size) >= #{storage_limit}
  ")
end

#runObject



23
24
25
26
27
# File 'lib/debci/storage_limit.rb', line 23

def run
  packages_with_excess_storage.each do |package|
    self.cleanup_package(package)
  end
end

#storage_limitObject



7
8
9
# File 'lib/debci/storage_limit.rb', line 7

def storage_limit
  @storage_limit ||= Debci.config.disk_storage_limit.megabytes
end