Module: Gitlab::Artifacts::Cleaner

Extended by:
Cleaner
Included in:
Cleaner
Defined in:
lib/gitlab/artifacts/cleaner.rb,
lib/gitlab/artifacts/cleaner/version.rb

Constant Summary collapse

VERSION =
"0.1.3"

Instance Method Summary collapse

Instance Method Details

#delete_artifact_api(job_id) ⇒ Object



81
82
83
# File 'lib/gitlab/artifacts/cleaner.rb', line 81

def delete_artifact_api(job_id)
  "#{@api_route}/jobs/#{job_id}/artifacts"
end

#delete_job_artifact!(job_id) ⇒ Object



63
64
65
66
67
# File 'lib/gitlab/artifacts/cleaner.rb', line 63

def delete_job_artifact!(job_id)
  ::HTTParty.delete(delete_artifact_api(job_id), headers: headers).tap do |response|
    raise "Failed to delete job artifact: #{response}" unless response.code == 204
  end
end

#done?Boolean

Returns:

  • (Boolean)


37
38
39
40
41
# File 'lib/gitlab/artifacts/cleaner.rb', line 37

def done?
  return false unless @total_page

  @current_page > @total_page
end

#executeObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/gitlab/artifacts/cleaner.rb', line 9

def execute
  @api_route, @priavte_token, @older_than = ARGV
  @current_page = 1
  @older_than = Time.parse(@older_than)
  @total_count = 0

  raise 'Insufficient arguments' unless @api_route && @priavte_token && @older_than

  while jobs = safe_get_job_list
    puts "@current_page: #{@current_page}"

    old_jobs = jobs.select { |job| Time.parse(job['created_at']) < @older_than }

    old_jobs.each do |job|
      job_id = job['id']
      begin
        delete_job_artifact!(job_id)
        puts "job_id: #{job_id} message: deleted job artifacts"
        @total_count += 1
      rescue => e
        puts "job_id: #{job_id} message: #{e.message}"
      end
    end
  end

  puts "DONE. @total_count: #{@total_count}"
end

#get_job_list!Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/gitlab/artifacts/cleaner.rb', line 51

def get_job_list!
  response = ::HTTParty.get(job_index_api, headers: headers, query: query)

  raise "Failed to get job list: #{response}" unless response.code == 200

  return nil if response.empty?

  response
ensure
  @current_page += 1
end

#headersObject



69
70
71
# File 'lib/gitlab/artifacts/cleaner.rb', line 69

def headers
  { 'Private-token' => @priavte_token }
end

#job_index_apiObject



77
78
79
# File 'lib/gitlab/artifacts/cleaner.rb', line 77

def job_index_api
  "#{@api_route}/jobs"
end

#queryObject



73
74
75
# File 'lib/gitlab/artifacts/cleaner.rb', line 73

def query
  { page: @current_page, per_page: 100 }
end

#safe_get_job_listObject



43
44
45
46
47
48
49
# File 'lib/gitlab/artifacts/cleaner.rb', line 43

def safe_get_job_list
  retries ||= 0
  get_job_list!
rescue => e
  sleep 10
  retry if (retries += 1) < 10
end