Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework gbif cleanup #1220

Merged
merged 4 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions app/jobs/delete_gbif_events_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,18 @@
class DeleteGbifEventsJob < ApplicationJob
queue_as :lupo_background

def perform(id, options = {})
event = Event.find_by(uuid: id)
def perform(ids, options = {})
label = options[:label]
index = options[:index]

event.destroy! if event.present?
# delete event records from mysql
sql = ActiveRecord::Base.sanitize_sql_array(["DELETE FROM events WHERE id IN (?)", ids])
ActiveRecord::Base.connection.execute(sql)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we delete the events using raw query, we are skipping the after_commit on: [:destroy] callback.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ashwinisukale yes this is by design as I will be using the bulk api to delete the documents in the events index.


# delete event documents from elasticsearch
bulk_payload = ids.map { |id| { delete: { _index: index, _id: id } } }
Event.__elasticsearch__.client.bulk(body: bulk_payload)
rescue => err
Rails.logger.info("#{options[:label]}: event delete error: #{err.message}")
Rails.logger.error("#{label}: #{err.message}")
end
end
35 changes: 35 additions & 0 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,41 @@ def self.loop_through_events(options)
Rails.logger.info("#{label}: task completed")
end

def self.loop_through_gbif_events(options)
size = (options[:size] || 1_000).to_i
cursor = options[:cursor] || []
filter = options[:filter] || {}
label = options[:label] || ""
job_name = options[:job_name] || ""
query = options[:query].presence
delete_count = 0
max_delete_count = options[:max_delete_count]

response = Event.query(query, filter.merge(page: { size: 1, cursor: [] }))

if response.size.positive?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should verify if response.present? && response.respond_to?(:size) && response.size.positive? response present, incase of an error

while response.size.positive? && delete_count < max_delete_count
response = Event.query(query, filter.merge(page: { size: size, cursor: cursor }))

break unless response.size.positive?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can make little readable by checking it like this break if response.size.zero?


Rails.logger.info("#{label}: #{response.size} events starting with _id #{response.results.to_a.first[:_id]}")

cursor = response.results.to_a.last[:sort]

Rails.logger.info "#{label}: cursor: #{cursor}"

ids = response.results.map(&:_id).uniq

Object.const_get(job_name).perform_later(ids, options)

delete_count += response.size
end
end

Rails.logger.info("#{label}: task completed")
end

def metric_type
if /(requests|investigations)/.match?(relation_type_id.to_s)
arr = relation_type_id.split("-", 4)
Expand Down
22 changes: 19 additions & 3 deletions lib/tasks/event.rake
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,33 @@ end
namespace :gbif_events do
desc "delete gbif events"
task delete_gbif_events: :environment do
index = ENV["INDEX"]

if index.blank?
Rails.logger.error("You must provide an INDEX environment variable")
exit
end

max_delete_count = ENV["MAX_DELETE_COUNT"]

if max_delete_count.blank?
Rails.logger.error("You must provide an MAX_DELETE_COUNT environment variable")
exit
end

options = {
size: 100,
size: 1000,
from_id: (ENV["FROM_ID"] || Event.minimum(:id)).to_i,
until_id: (ENV["UNTIL_ID"] || Event.maximum(:id)).to_i,
filter: {},
query: "+subj.registrantId:datacite.gbif.gbif +relation_type_id:references -source_doi:(\"10.15468/QJGWBA\" OR \"10.35035/GDWQ-3V93\" OR \"10.15469/3XSWXB\" OR \"10.15469/UBP6QO\" OR \"10.35000/TEDB-QD70\" OR \"10.15469/2YMQOZ\")",
job_name: "DeleteGbifEventsJob",
label: "gbif_event_cleanup_#{Time.now.utc.strftime("%d%m%Y%H%M%S")}",
job_name: "DeleteGbifEventsJob"
max_delete_count: max_delete_count.to_i,
index: index
}

Event.loop_through_events(options)
Event.loop_through_gbif_events(options)
end

desc "delete orphaned gbif_events"
Expand Down