Skip to content

Commit

Permalink
[api] split the perform function for PCACR
Browse files Browse the repository at this point in the history
  • Loading branch information
coolo committed Jan 28, 2014
1 parent 48b0089 commit dd8ca0a
Showing 1 changed file with 35 additions and 30 deletions.
65 changes: 35 additions & 30 deletions src/api/app/jobs/project_create_auto_cleanup_requests.rb
@@ -1,3 +1,5 @@
require 'opensuse/backend'

class ProjectCreateAutoCleanupRequests

Description="This is a kind request to remove this project.
Expand All @@ -10,48 +12,51 @@ def initialize
end

def perform
require 'opensuse/backend'
# disabled ?
cleanupDays = ::Configuration.first.cleanup_after_days
return unless cleanupDays and cleanupDays > 0

# defaults
User.current ||= User.find_by_login "Admin"
at = AttribType.find_by_namespace_and_name("OBS", "AutoCleanup")
cleanupTime = DateTime.now + cleanupDays.days

Project.find_by_attribute_type( at ).each do |prj|
# project may be locked?
next if prj.nil? or prj.is_locked?

# open requests do block the cleanup
next if BsRequest.open_requests_for(prj).length > 0

# check the time in project attribute
time = nil
begin
next unless attribute = prj.attribs.find_by_attrib_type_id( at.id )
next unless time = DateTime.parse(attribute.values.first.value)
rescue
# not parseable time
next
end
# not yet
next unless time.past?

# create request, but add some time between to avoid an overload
cleanupTime = cleanupTime + 5.minutes

req = BsRequest.new_from_xml( '<request>
@cleanupTime = DateTime.now + cleanupDays.days

Project.find_by_attribute_type(at).each do |prj|
autoclean_project(prj)
end
end

def autoclean_project(prj)
# project may be locked?
return if prj.nil? or prj.is_locked?

# open requests do block the cleanup
return if BsRequest.open_requests_for(prj).length > 0

# check the time in project attribute
time = nil
begin
return unless attribute = prj.attribs.find_by_attrib_type_id(at.id)
return unless time = DateTime.parse(attribute.values.first.value)
rescue ArgumentError
# not parseable time
return
end
# not yet
return unless time.past?

# create request, but add some time between to avoid an overload
@cleanupTime = @cleanupTime + 5.minutes

req = BsRequest.new_from_xml('<request>
<action type="delete">
<target project="' + prj.name + '" />
<target project="' + prj.name + '" />
</action>
<description>'+Description+'</description>
<state who="Admin" name="new"/>
<accept_at>' + cleanupTime.to_s + '</accept_at>
<accept_at>' + @cleanupTime.to_s + '</accept_at>
</request>')
req.save!
end
req.save!
end

end
Expand Down

0 comments on commit dd8ca0a

Please sign in to comment.