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

achydra-950: monthly cron to delete stale pending works #289

Merged
merged 1 commit into from
Oct 31, 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
5 changes: 5 additions & 0 deletions config/schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,8 @@
every :day, at: '12am' do
rake 'resque:restart_workers', email_subject: 'Resque workers restart'
end

# Delete stale pending works once a month
every :month do
rake 'ac:delete_stale_pending_works', email_subject: 'Delete stale pending works'
end
34 changes: 34 additions & 0 deletions lib/tasks/ac.rake
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,38 @@ namespace :ac do
puts Rainbow('Incorrect arguments. Pass output=/path/to/file').red
end
end

task delete_stale_pending_works: :environment do
log = Rails.logger
users = User.all
users.each do |current_user|
deposits = current_user.deposits.where('created_at < ?', 6.months.ago)
identifiers = deposits.map(&:hyacinth_identifier).compact
if identifiers.present?
results = AcademicCommons.search do |params|
identifiers = identifiers.map { |i| "\"#{i}\"" }.join(' OR ')
.prepend('(').concat(')')
params.filter('fedora3_pid_ssi', identifiers)
params.aggregators_only
params.field_list('fedora3_pid_ssi')
end

hyacinth_ids_in_ac = results.documents.map { |d| d[:fedora3_pid_ssi] }
else
hyacinth_ids_in_ac = []
end

pending_works = deposits.to_a.keep_if do |deposit|
hyacinth_id = deposit.hyacinth_identifier
hyacinth_id.blank? || !hyacinth_ids_in_ac.include?(hyacinth_id)
end

log.info "===Deleting #{pending_works.count} stale pending work(s)===" unless pending_works.empty?

pending_works.each do |pending_work|
log.info pending_work.inspect
pending_work.destroy
end
end
end
end
3 changes: 1 addition & 2 deletions lib/tasks/resque.rake
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ namespace :resque do
count.times do |index|
number = index + 1
pidfile = Rails.root.join('tmp/pids/', "resque_work_#{number}.pid").to_s
_stdout_str, _stderr_str, status = Open3.capture3("RAILS_ENV=#{rails_env} QUEUE=\"#{queues}\" PIDFILE=#{pidfile} BACKGROUND=yes VERBOSE=1 INTERVAL=#{interval} rake resque:work >> #{out} 2>> #{err}")
puts "Worker #{number} started, status: #{status}"
Open3.capture3("RAILS_ENV=#{rails_env} QUEUE=\"#{queues}\" PIDFILE=#{pidfile} BACKGROUND=yes VERBOSE=1 INTERVAL=#{interval} rake resque:work >> #{out} 2>> #{err}")
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions spec/factories/deposit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,18 @@
license { 'https://creativecommons.org/licenses/by/4.0/' }
previously_published { true }
current_student { false }
created_at { Time.zone.now }
hyacinth_identifier {}

after(:build) do |deposit|
deposit.files.attach(
io: File.open(Rails.root.join('spec', 'fixtures', 'test_file.txt')),
filename: 'test_file.txt'
)
end

trait :with_user do
association :user, factory: :user
end
end
end
2 changes: 1 addition & 1 deletion spec/factories/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
uid { 'tu123' }
first_name { 'Test' }
last_name { 'User' }
email { '[email protected]' }
sequence(:email) { |n| "tu123#{n}@columbia.edu" }
end

factory :admin, class: User do
Expand Down
2 changes: 1 addition & 1 deletion spec/features/upload_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
it 'sends student reminder email' do
email = ActionMailer::Base.deliveries.pop
expect(email.subject).to eql 'Department approval may be needed'
expect(email.to).to include '[email protected]'
expect(email.to).to include User.first.email
end
end

Expand Down
92 changes: 92 additions & 0 deletions spec/tasks/ac/pending_works_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# frozen_string_literal: true

require 'rails_helper'

describe 'rake ac:delete_stale_pending_works', type: :task do
context 'when a deposit with no hyacinth identifier' do
let!(:deposit_fresh) { FactoryBot.create(:deposit, :with_user) }
let!(:deposit_stale) do
FactoryBot.create(:deposit, :with_user, created_at: 7.months.ago)
end

describe 'that is less than 6 months old' do
it 'does not delete the deposit' do
task.execute
expect(Deposit.exists?(deposit_fresh.id)).to be true
end
end

describe 'that is over 6 months old' do
it 'deletes the deposit' do
task.execute
expect { deposit_stale.reload }.to raise_error(ActiveRecord::RecordNotFound)
end
end
end

context 'when a deposit with an identifier that is unindexed' do
let(:empty_response) { wrap_solr_response_data('response' => { 'docs' => [] }) }
let!(:deposit_fresh) do
FactoryBot.create(:deposit, :with_user, hyacinth_identifier: 'unindexed')
end
let!(:deposit_stale) do
FactoryBot.create(:deposit, :with_user, created_at: 7.months.ago, hyacinth_identifier: 'unindexed')
end

before do
allow(AcademicCommons).to receive(:search).and_return(empty_response)
end

describe 'and is less than 6 months old' do
it 'does not delete the deposit' do
task.execute
expect(Deposit.exists?(deposit_fresh.id)).to be true
end
end

describe 'and is over 6 months old' do
it 'deletes the deposit' do
task.execute
expect { deposit_stale.reload }.to raise_error(ActiveRecord::RecordNotFound)
end
end
end

context 'when a deposit with an identifier that is indexed' do
let(:solr_response) do
wrap_solr_response_data(
'response' => {
'docs' => [
{ 'id' => '10.7916/TESTDOC10', 'title_ssi' => 'First Test Document', 'object_state_ssi' => 'A',
'cul_doi_ssi' => '10.7916/TESTDOC10', 'fedora3_pid_ssi' => 'actest:10', 'genre_ssim' => '',
'publisher_doi_ssi' => '', 'free_to_read_start_date_ssi' => Date.current.tomorrow.strftime('%Y-%m-%d') }
]
}
)
end
let!(:deposit_fresh) do
FactoryBot.create(:deposit, :with_user, hyacinth_identifier: 'actest:10')
end
let!(:deposit_stale) do
FactoryBot.create(:deposit, :with_user, created_at: 7.months.ago, hyacinth_identifier: 'actest:10')
end

before do
allow(AcademicCommons).to receive(:search).and_return(solr_response)
end

describe 'and is less than 6 months old' do
it 'does not delete the deposit' do
task.execute
expect(Deposit.exists?(deposit_fresh.id)).to be true
end
end

describe 'and is over 6 months old' do
it 'does not delete the deposit' do
task.execute
expect(Deposit.exists?(deposit_stale.id)).to be true
end
end
end
end
Loading