Skip to content

Commit

Permalink
add rake task to delete expired orcid tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Jun 10, 2021
1 parent 4e8794e commit 134fd4b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
13 changes: 13 additions & 0 deletions app/jobs/user_token_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class UserJob < ApplicationJob
queue_as :volpino

rescue_from ActiveJob::DeserializationError, ActiveRecord::ConnectionTimeoutError, Faraday::TimeoutError do
retry_job wait: 5.minutes, queue: :volpino
end

def perform(user)
ActiveRecord::Base.connection_pool.with_connection do
user.update(orcid_expires_at: "1970-01-01", orcid_token: nil),
end
end
end
27 changes: 27 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,33 @@ def queue_user_job
UserJob.perform_later(self)
end

def self.delete_expired_token(index: nil)
size = (options[:size] || 1000).to_i
query = "orcid_expires_at:[1970-01-02 TO #{Date.today.strftime('%F')}]"

response = User.query(query, index: index, page: { size: 1, cursor: [] })
Rails.logger.info "#{response.results.total} Users with expired ORCID token found."

if response.results.total > 0
# walk through results using cursor
cursor = []

while !response.results.results.empty?
response = User.query(query, index: index, page: { size: size, cursor: cursor })
break if response.results.results.empty?

Rails.logger.info "Deleting #{response.results.length} User ORCID tokens starting with _id #{response.results.to_a.first[:_id]}."
cursor = response.results.to_a.last[:sort]

response.records.each do |u|
DeleteTokenJob.perform_later(u.uid)
end
end
end

response.results.total
end

def claims_count
claims.size
end
Expand Down
5 changes: 5 additions & 0 deletions lib/tasks/user.rake
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ namespace :user do
User.import_by_ids(from_id: from_id, until_id: until_id, index: ENV["INDEX"] || User.inactive_index)
end

desc "Delete expired ORCID tokens"
task delete_expired_token: :environment do
User.delete_expired_token(index: ENV["INDEX"] || User.inactive_index)
end

desc "Update all claims counts"
task update_counts: :environment do
User.find_each do |user|
Expand Down

0 comments on commit 134fd4b

Please sign in to comment.