Class: Backup::RemoveOld

Inherits:
Object
  • Object
show all
Defined in:
lib/backup/remove_old.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, dry_run_reporter = nil) ⇒ RemoveOld

Returns a new instance of RemoveOld.



7
8
9
10
# File 'lib/backup/remove_old.rb', line 7

def initialize(config, dry_run_reporter=nil)
  @config = config
  @dry_run_reporter = dry_run_reporter
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



5
6
7
# File 'lib/backup/remove_old.rb', line 5

def config
  @config
end

Instance Method Details

#dry_run_reportObject



12
13
14
# File 'lib/backup/remove_old.rb', line 12

def dry_run_report
  @dry_run_reporter.report
end

#process_all_reposObject



42
43
44
45
46
# File 'lib/backup/remove_old.rb', line 42

def process_all_repos
  Repository.order(:id).each do |repository|
    process_repo(repository)
  end
end

#process_repo(repository) ⇒ Object



48
49
50
51
# File 'lib/backup/remove_old.rb', line 48

def process_repo(repository)
  process_repo_builds(repository)
  process_repo_requests(repository)
end

#process_repo_builds(repository) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/backup/remove_old.rb', line 53

def process_repo_builds(repository) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  threshold = @config.threshold.to_i.months.ago.to_datetime
  current_build_id = repository.current_build_id || -1
  repository.builds.where('created_at < ? and id != ?', threshold, current_build_id)
            .in_batches(of: @config.limit.to_i).map do |builds_batch|
    if @config.if_backup
      file_prefix = "repository_#{repository.id}"
      save_and_destroy_builds_batch(builds_batch, file_prefix)
    else
      destroy_builds_batch(builds_batch)
    end
  end.compact.reduce(&:&)
end

#process_repo_requests(repository) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/backup/remove_old.rb', line 67

def process_repo_requests(repository)
  threshold = @config.threshold.to_i.months.ago.to_datetime
  repository.requests.where('created_at < ?', threshold)
            .in_batches(of: @config.limit.to_i).map do |requests_batch|
    @config.if_backup ? save_and_destroy_requests_batch(requests_batch, repository) : destroy_requests_batch(requests_batch)
  end.compact
end

#process_repo_with_id(repo_id) ⇒ Object



38
39
40
# File 'lib/backup/remove_old.rb', line 38

def process_repo_with_id(repo_id)
  process_repo(Repository.find(repo_id))
end

#process_repos_for_owner(owner_id, owner_type) ⇒ Object



32
33
34
35
36
# File 'lib/backup/remove_old.rb', line 32

def process_repos_for_owner(owner_id, owner_type)
  Repository.where('owner_id = ? and owner_type = ?', owner_id, owner_type).order(:id).each do |repository|
    process_repo(repository)
  end
end

#run(args = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/backup/remove_old.rb', line 16

def run(args={})
  user_id = args[:user_id] || @config.user_id
  repo_id = args[:repo_id] || @config.repo_id
  org_id = args[:org_id] || @config.org_id

  if user_id
    process_repos_for_owner(user_id, 'User')
  elsif org_id
    process_repos_for_owner(org_id, 'Organization')
  elsif repo_id
    process_repo_with_id(repo_id)
  else
    process_all_repos
  end  
end