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

Use destroy instead of unlock in CleanupLocksJob #688

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
per week.

Run `rake locks:enqueue_cleanup` hourly to delete locks older than 8 hours.
7 changes: 2 additions & 5 deletions app/jobs/cleanup_locks_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
37 changes: 11 additions & 26 deletions spec/jobs/cleanup_locks_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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