From e12875f1d21480442e328584d14bfc04a977c938 Mon Sep 17 00:00:00 2001 From: Nathan Papes Date: Wed, 4 Oct 2023 16:04:54 -0400 Subject: [PATCH 1/2] use destroy in CleanupLocksJob to delete locks to avoid validation errors --- README.md | 4 +++- app/jobs/cleanup_locks_job.rb | 7 ++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index e2b0fb45..8326a59d 100644 --- a/README.md +++ b/README.md @@ -50,4 +50,6 @@ until the view is refreshed. Set a cron job or use Heroku Scheduler or the equivalent to run `rake indices:refresh` as frequently as makes sense. To send a weekly report of usage data, run `rake emails:send_weekly_report` once -per week. \ No newline at end of file +per week. + +Run `rake locks:enqueue_cleanup` hourly to delete locks older than 8 hours. \ No newline at end of file diff --git a/app/jobs/cleanup_locks_job.rb b/app/jobs/cleanup_locks_job.rb index 983b1228..7344bc12 100644 --- a/app/jobs/cleanup_locks_job.rb +++ b/app/jobs/cleanup_locks_job.rb @@ -25,12 +25,9 @@ def unlock_resources(locks) end def unlock_single_resource(lock) - resource = lock.lockable - resource.unlock + lock.destroy! BroadcastEdit.to(lock, type: :destroy, session_id: nil) rescue StandardError => e - Rails.logger.error <<~ERROR_MESSAGE - Failed to unlock Resource: #{resource.inspect} with Error: #{e.message} - ERROR_MESSAGE + Rails.logger.error "destroy Lock##{lock.id} failed: #{e.message}" end end From 0738ce8f7f5e801ab9202dcb3ac751ae801d3836 Mon Sep 17 00:00:00 2001 From: Nathan Papes Date: Wed, 4 Oct 2023 17:49:21 -0400 Subject: [PATCH 2/2] fix tests --- app/jobs/cleanup_locks_job.rb | 2 +- spec/jobs/cleanup_locks_job_spec.rb | 37 +++++++++-------------------- 2 files changed, 12 insertions(+), 27 deletions(-) diff --git a/app/jobs/cleanup_locks_job.rb b/app/jobs/cleanup_locks_job.rb index 7344bc12..ab839eac 100644 --- a/app/jobs/cleanup_locks_job.rb +++ b/app/jobs/cleanup_locks_job.rb @@ -25,7 +25,7 @@ def unlock_resources(locks) end def unlock_single_resource(lock) - lock.destroy! + lock.destroy BroadcastEdit.to(lock, type: :destroy, session_id: nil) rescue StandardError => e Rails.logger.error "destroy Lock##{lock.id} failed: #{e.message}" diff --git a/spec/jobs/cleanup_locks_job_spec.rb b/spec/jobs/cleanup_locks_job_spec.rb index fd2b7c42..c346cea5 100644 --- a/spec/jobs/cleanup_locks_job_spec.rb +++ b/spec/jobs/cleanup_locks_job_spec.rb @@ -4,36 +4,21 @@ RSpec.describe CleanupLocksJob, type: :job do describe '#perform' do - let(:lockable1) { create(:case) } - let(:lockable2) { create(:case_with_elements) } + let(:bob) { create(:reader, name: 'bob') } + let(:alice) { create(:reader, name: 'alice') } it 'unlocks resources for a specific reader_id' do - reader_id = create(:reader).id - reader_lock = create(:lock, lockable: lockable1, reader_id: reader_id) - allow(Lock).to receive(:where).with(reader_id: reader_id) - .and_return([reader_lock]) - - expect(lockable1).to receive(:unlock) - expect(BroadcastEdit).to receive(:to) - .with(reader_lock, type: :destroy, session_id: nil) - - described_class.new.perform(reader_id: reader_id) + create(:lock, reader: bob) + create_list(:lock, 2, :one_hour_old, reader: alice) + job = described_class.new + expect { job.perform(reader_id: bob.id) }.to change { Lock.count }.by(-1) end - it 'unlocks resources based on the destroy_after time' do - one_hour_old_lock = create(:lock, :one_hour_old, lockable: lockable1) - eight_hours_old_lock = create(:lock, :eight_hours_old, lockable: lockable2) - allow(Lock).to receive(:where).and_return([eight_hours_old_lock]) - - expect(lockable1).not_to receive(:unlock) - expect(BroadcastEdit).not_to receive(:to) - .with(one_hour_old_lock, type: :destroy, session_id: nil) - - expect(lockable2).to receive(:unlock) - expect(BroadcastEdit).to receive(:to) - .with(eight_hours_old_lock, type: :destroy, session_id: nil) - - described_class.new.perform + it 'unlocks resources based on destroy_after time' do + create(:lock, reader: bob) + create_list(:lock, 2, :eight_hours_old, reader: alice) + job = described_class.new + expect { job.perform }.to change { Lock.count }.by(-2) end end end