Skip to content

Commit

Permalink
Refactor ProjectCreateAutoCleanupRequests job
Browse files Browse the repository at this point in the history
  • Loading branch information
vpereira committed Oct 24, 2020
1 parent cfe64c0 commit c57fd52
Showing 1 changed file with 58 additions and 23 deletions.
81 changes: 58 additions & 23 deletions src/api/app/jobs/project_create_auto_cleanup_requests.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,36 @@
class ProjectCreateAutoCleanupRequests < ApplicationJob
class ProjectCreateAutoCleanupRequestsJob < ApplicationJob
DESCRIPTION = "This is a humble request to remove this project.
Accepting this request will free resources on our always crowded server.
Please decline this request if you want to keep this repository nevertheless. Otherwise this request
will get accepted automatically in near future.
Such requests get not created for projects with open requests or if you remove the OBS:AutoCleanup attribute.".freeze

class CleanupRequestTemplate
attr_accessor :project, :description, :cleanup_time

def initialize(project:, description:, cleanup_time:)
@project = project
@description = description
@cleanup_time = cleanup_time
@erb = ERB.new(template)
end

def template
<<~XML
<request>
<action type="delete"><target project="<%= project %>"/></action>
<description><%= description %></description>
<state />
<accept_at><%= cleanup_time %></accept_at>
</request>
XML
end

def render
@erb.result(binding)
end
end

def perform
# disabled ?
cleanup_days = ::Configuration.cleanup_after_days
Expand All @@ -13,35 +39,30 @@ def perform
# defaults
User.find_by!(login: 'Admin').run_as do
@cleanup_attribute = AttribType.find_by_namespace_and_name!('OBS', 'AutoCleanup')
@cleanup_time = Time.now + cleanup_days.days
@cleanup_time = Time.zone.now + cleanup_days.days

Project.find_by_attribute_type(@cleanup_attribute).each do |prj|
autoclean_project(prj)
end
end
end

def autoclean_project(prj)
# project may be locked?
return if prj.nil? || prj.is_locked? || !prj.check_weak_dependencies?
private

def autoclean_project(prj)
# open requests do block the cleanup
open_requests_count = BsRequest.in_states([:new, :review, :declined])
.joins(:bs_request_actions)
.where('bs_request_actions.target_project = ? OR bs_request_actions.source_project = ?', prj.name, prj.name)
.count
return if open_requests_count.positive?
return if open_requests_count(prj.name).positive?

# check the time in project attribute
time = nil
return unless project_ready_to_autoclean?(prj)

begin
attribute = prj.attribs.find_by_attrib_type_id(@cleanup_attribute.id)
return unless attribute

time = Time.parse(attribute.values.first.value)
return unless time
rescue ArgumentError
# not parseable time
time = Time.zone.parse(attribute.values.first.value)
rescue TypeError, ArgumentError
# nil time raises TypeError
return
end
# not yet
Expand All @@ -50,15 +71,29 @@ def autoclean_project(prj)
# create request, but add some time between to avoid an overload
@cleanup_time += 5.minutes

req = BsRequest.new_from_xml('<request>
<action type="delete">
<target project="' + prj.name + '" />
</action>
<description>' + DESCRIPTION + '</description>
<state />
<accept_at>' + @cleanup_time.to_s + '</accept_at>
</request>')
req = create_request(project: prj.name, cleanup_time: @cleanup_time)
req.save!
Event::RequestCreate.create(req.event_parameters)
end

def create_request(project:, description: DESCRIPTION, cleanup_time: 5)
BsRequest.new_from_xml(CleanupRequestTemplate.new(project: project,
description: description,
cleanup_time: cleanup_time).render)
end

def project_ready_to_autoclean?(project)
# project may be locked?
return false if project.nil? || project.is_locked?
return false unless project.check_weak_dependencies?

true
end

def open_requests_count(project)
BsRequest.in_states([:new, :review, :declined])
.joins(:bs_request_actions)
.where('bs_request_actions.target_project = ? OR bs_request_actions.source_project = ?', project, project)
.count
end
end

0 comments on commit c57fd52

Please sign in to comment.