Skip to content

Commit

Permalink
feat: APPS-2387 update delete rake task (#949)
Browse files Browse the repository at this point in the history
* feat: update deleter class to add logic of deleteing a collection and its works only if its children are deleted

* fix: rubocop errors

* fix: add check if works or child works are empty continue with deleteion
  • Loading branch information
pghorpade authored Dec 4, 2023
1 parent 6d44815 commit 298692e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
45 changes: 30 additions & 15 deletions app/lib/californica/deleter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,37 +18,48 @@ def delete
destroy_and_eradicate
end

# Modified to ensure all works are deleted before deleting the collection
def delete_collection_with_works(of_type: nil)
# replace log to puts when running locally
log('In delete_collection_with_works start.')
delete_works(of_type: of_type)
delete if of_type.nil? || record.is_a?(of_type)
log('In delete_collection_with_works stop.')
works = work_id_list
all_works_deleted = works.empty? || delete_works(of_type: of_type)
if all_works_deleted && (of_type.nil? || record.is_a?(of_type))
delete
true
else
log('Deletion skipped for some works.')
false
end
end

# Ensures all works are deleted; returns true if successful
def delete_works(of_type: nil)
# log('In delete_works start.')
work_id_list&.each do |work_id|
work_id_list.empty? || work_id_list.all? do |work_id|
Californica::Deleter.new(id: work_id, logger: logger)
.delete_with_children(of_type: of_type)
end
# log('In delete_works end.')
end

# Modified to ensure all works are deleted before deleting the collection
def delete_with_children(of_type: nil)
# log('In delete_with_children start.')
delete_children(of_type: of_type)
delete if of_type.nil? || record.is_a?(of_type)
# log('In delete_with_children end.')
log('In delete_with_children start.')
children = record&.member_ids
all_children_deleted = children.nil? || children.empty? || delete_children(of_type: of_type)
if all_children_deleted && (of_type.nil? || record.is_a?(of_type))
delete
true
else
log('Deletion skipped for some child works.')
false
end
end

# Ensures all child works are deleted; returns true if successful
def delete_children(of_type: nil)
# log('In delete_children start.')
record&.member_ids&.each do |child_id|
record&.member_ids&.empty? || record&.member_ids&.all? do |child_id|
Californica::Deleter.new(id: child_id, logger: logger)
.delete_with_children(of_type: of_type)
end
# log('In delete_children end.')
end

private
Expand All @@ -62,7 +73,11 @@ def destroy_and_eradicate
rescue Ldp::HttpError, Faraday::TimeoutError, Faraday::ConnectionFailed => e
log("#{e.class}: #{e.message}")
retries ||= 0
retry if (retries += 1) <= 3
if (retries += 1) > 3
return false # Explicitly return false after retries are exhausted
else
retry
end
end

def delete_from_fcrepo
Expand Down
7 changes: 6 additions & 1 deletion lib/tasks/delete.rake
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ namespace :californica do
end

task delete_collection: [:environment] do
Californica::Deleter.new(id: ENV.fetch('DELETE_COLLECTION_ID')).delete_collection_with_works
begin
deletion_successful = Californica::Deleter.new(id: ENV.fetch('DELETE_COLLECTION_ID')).delete_collection_with_works
puts deletion_successful ? 'Deletion completed successfully!' : 'Deletion skipped for some items.'
rescue => e
puts "An error occurred: #{e.message}"
end
puts('Done!')
end

Expand Down

0 comments on commit 298692e

Please sign in to comment.