Skip to content

Commit

Permalink
Merge branch 'main'
Browse files Browse the repository at this point in the history
  • Loading branch information
laritakr committed Nov 1, 2024
2 parents 004c9bc + 65c6293 commit 7bfd25d
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 23 deletions.
3 changes: 3 additions & 0 deletions app/assets/stylesheets/_bootstrap-default-overrides.scss
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ $warning: #565653 !default;
$breadcrumb-active-color: #4c4c4c;
$pagination-disabled-color: #4c4c4c;
$blue: #0056B3 !default;
// Adjusts link text to show as underlined and then not underlined on hover
$link-decoration: underline !default;
$link-hover-decoration: none !default;

// Date picker on safari placeholder text was misaligned
@media screen {
Expand Down
1 change: 1 addition & 0 deletions app/assets/stylesheets/hyrax/_forms.scss
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ legend small {

.select2-container .select2-choice > .select2-chosen {
max-width: 26em;
overflow: auto;
}

form {
Expand Down
12 changes: 6 additions & 6 deletions app/services/hyrax/lock_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ def initialize(time_to_live, retry_count, retry_delay)

##
# Blocks until lock is acquired or timeout.
def lock(key)
def lock(key, ttl: @ttl, retry_count: @retry_count, retry_delay: @retry_delay)
returned_from_block = nil

pool.then do |conn|
client(conn).lock(key, @ttl) do |locked|
client(conn, retry_count: retry_count, retry_delay: retry_delay).lock(key, ttl) do |locked|
raise UnableToAcquireLockError unless locked
returned_from_block = yield
end
Expand All @@ -31,16 +31,16 @@ def lock(key)
Hyrax.logger.error(err.message)
raise(ConnectionPool::TimeoutError,
"Failed to acquire a lock from Redlock due to a Redis connection " \
"timeout: #{err}. If you are using Redis via `ConnectionPool` " \
"you may wish to increase the pool size.")
"timeout: #{err}. If you are using Redis via `ConnectionPool` " \
"you may wish to increase the pool size.")
end

private

##
# @api_private
def client(conn)
Redlock::Client.new([conn], retry_count: @retry_count, retry_delay: @retry_delay)
def client(conn, retry_count:, retry_delay:)
Redlock::Client.new([conn], retry_count: retry_count, retry_delay: retry_delay)
end

##
Expand Down
7 changes: 4 additions & 3 deletions app/services/hyrax/lockable.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# frozen_string_literal: true
require 'forwardable'

module Hyrax
module Lockable
extend Forwardable
extend ActiveSupport::Concern

def acquire_lock_for(lock_key, &block)
lock_manager.lock(lock_key, &block)
end
def_delegator :lock_manager, :lock, :acquire_lock_for

def lock_manager
@lock_manager ||= LockManager.new(
Expand Down
10 changes: 6 additions & 4 deletions app/views/hyrax/dashboard/collections/_form_share.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@
class: 'form-control search-input' %>
</div>
<div class="form-group">
<label class="mr-2">as</label>
<label id="add_group_as" class="mr-2">as</label>
<%= builder.select :access,
access_options,
{ prompt: t('.select_a_role') },
'aria-labelledby': "add_group_as",
class: 'form-control' %>
</div>
<% end %>
Expand Down Expand Up @@ -56,10 +57,11 @@
placeholder: t('.search_for_a_user') %>
</div>
<div class="form-group">
<label class="mx-2">as</label>
<label id="add_user_as" class="mx-2">as</label>
<%= builder.select :access,
access_options,
{ prompt: t('.select_a_role') },
'aria-labelledby': "add_user_as",
class: 'form-control' %>
</div>
<% end %>
Expand All @@ -75,8 +77,8 @@

<h2 class="h3"><%= t(".current_shared") %></h2>
<section class="section-collection-sharing">


<%= render 'form_share_table', access: 'managers', filter: :manage? %>
<%= render 'form_share_table', access: 'depositors', filter: :deposit? %>
<%= render 'form_share_table', access: 'viewers', filter: :view? %>
Expand Down
8 changes: 4 additions & 4 deletions app/views/hyrax/pages/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
<div class="card tabs">

<ul class="nav nav-tabs" role="tablist">
<li class="nav-item">
<li class="nav-item" role="presentation">
<a href="#about" role="tab" data-toggle="tab" class="nav-link active nav-safety-confirm">
<%= t(:'hyrax.pages.tabs.about_page') %>
</a>
</li>
<li class="nav-item">
<li class="nav-item" role="presentation">
<a href="#help" role="tab" data-toggle="tab" class="nav-link nav-safety-confirm">
<%= t(:'hyrax.pages.tabs.help_page') %>
</a>
</li>
<li class="nav-item">
<li class="nav-item" role="presentation">
<a href="#agreement" role="tab" data-toggle="tab" class="nav-link nav-safety-confirm">
<%= t(:'hyrax.pages.tabs.agreement_page') %>
</a>
</li>
<li class="nav-item">
<li class="nav-item" role="presentation">
<a href="#terms" role="tab" data-toggle="tab" class="nav-link nav-safety-confirm">
<%= t(:'hyrax.pages.tabs.terms_page') %>
</a>
Expand Down
9 changes: 6 additions & 3 deletions app/views/hyrax/users/_user_row.html.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<tr>
<td><%= link_to hyrax.user_path(user) do %>
<%= image_tag(user.avatar.url(:thumb), width: 30) if user.avatar.file %>
<td>
<% if user.avatar.file %>
<%= link_to hyrax.user_path(user) do %>
<%= image_tag(user.avatar.url(:thumb), width: 30, alt: user.name) %>
<% end %>
</td>
<% end %>
</td>
<td><%= link_to user.name, hyrax.user_path(user) %></td>
<td><%= link_to user.user_key, hyrax.user_path(user) %></td>
<td><%= user.department %></td>
Expand Down
85 changes: 82 additions & 3 deletions spec/services/hyrax/lock_manager_spec.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,93 @@
# frozen_string_literal: true
RSpec.describe Hyrax::LockManager do
let(:ttl) { Hyrax.config.lock_time_to_live }
let(:retry_count) { Hyrax.config.lock_retry_count }
let(:retry_delay) { Hyrax.config.lock_retry_delay }

subject do
described_class.new(Hyrax.config.lock_time_to_live,
Hyrax.config.lock_retry_count,
Hyrax.config.lock_retry_delay)
described_class.new(ttl, retry_count, retry_delay)
end

describe "lock", unless: ENV['TRAVIS'] do
it "calls the block" do
expect { |probe| subject.lock('foobar', &probe) }.to yield_with_no_args
end

describe 'ttl' do
it "uses the configured ttl" do
client = instance_double(Redlock::Client)
allow(Redlock::Client).to receive(:new).and_return(client)

key = 'the key'
expect(client).to receive(:lock).with(key, ttl).and_yield(true)
subject.lock(key) { |_| }
end

it "accepts an optional ttl argument" do
new_ttl = 1000
expect(new_ttl).not_to eq(ttl) # just to be sure

client = instance_double(Redlock::Client)
allow(Redlock::Client).to receive(:new).and_return(client)

key = 'the key'
expect(client).to receive(:lock).with(key, new_ttl).and_yield(true)
subject.lock(key, ttl: new_ttl) { |_| }
end
end

describe 'retry_count' do
it "uses the configured retry_count" do
client = instance_double(Redlock::Client)
expect(Redlock::Client)
.to receive(:new)
.with(kind_of(Array), retry_count: retry_count, retry_delay: kind_of(Integer))
.and_return(client)

allow(client).to receive(:lock).and_yield(true)
subject.lock("a key") { |_| }
end

it "accepts an optional retry_count argument" do
new_retry_count = 11
expect(new_retry_count).not_to eq(retry_count) # just to be sure

client = instance_double(Redlock::Client)
expect(Redlock::Client)
.to receive(:new)
.with(kind_of(Array), retry_count: new_retry_count, retry_delay: retry_delay)
.and_return(client)

allow(client).to receive(:lock).and_yield(true)
subject.lock("a key", retry_count: new_retry_count) { |_| }
end
end

describe 'retry_delay' do
it "uses the configured retry_delay" do
client = instance_double(Redlock::Client)
expect(Redlock::Client)
.to receive(:new)
.with(kind_of(Array), retry_count: kind_of(Integer), retry_delay: retry_delay)
.and_return(client)

allow(client).to receive(:lock).and_yield(true)
subject.lock("a key") { |_| }
end

it "accepts an optional retry_delay argument" do
new_retry_delay = 11
expect(new_retry_delay).not_to eq(retry_delay) # just to be sure

client = instance_double(Redlock::Client)
expect(Redlock::Client)
.to receive(:new)
.with(kind_of(Array), retry_count: retry_count, retry_delay: new_retry_delay)
.and_return(client)

allow(client).to receive(:lock).and_yield(true)
subject.lock("a key", retry_delay: new_retry_delay) { |_| }
end
end
end
end
57 changes: 57 additions & 0 deletions spec/services/hyrax/lockable_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# frozen_string_literal: true
RSpec.describe Hyrax::Lockable do
let(:lockable_class) { Class.new.include(described_class) }
let(:lock_manager) { instance_double(Hyrax::LockManager) }

subject { lockable_class.new }

describe "lock_manager" do
it "lazily creates a new lock manager with the default configuration" do
expect(Hyrax::LockManager).to receive(:new).once.with(
Hyrax.config.lock_time_to_live,
Hyrax.config.lock_retry_count,
Hyrax.config.lock_retry_delay
).and_return(lock_manager)

expect(subject.lock_manager).to be(lock_manager)
expect(subject.lock_manager).to be(lock_manager)
end
end

describe "acquire_lock_for" do
before do
subject.instance_variable_set(:@lock_manager, lock_manager)
end

it 'acquires the lock' do
key = 'a key'
block = proc {}
expect(lock_manager).to receive(:lock).with(key, &block)
subject.acquire_lock_for(key, &block)
end

it 'accepts an optional ttl argument' do
key = 'a key'
new_ttl = 1000
block = proc {}
expect(lock_manager).to receive(:lock).with(key, ttl: new_ttl, &block)
subject.acquire_lock_for(key, ttl: new_ttl, &block)
end

it 'accepts an optional retry_count argument' do
key = 'a key'
new_retry_count = 11
block = proc {}
expect(lock_manager).to receive(:lock).with(key, retry_count: new_retry_count, &block)
subject.acquire_lock_for(key, retry_count: new_retry_count, &block)
end

it 'accepts an optional retry_delay argument' do
key = 'a key'
new_retry_delay = 11
block = proc {}
expect(lock_manager).to receive(:lock).with(key, retry_delay: new_retry_delay, &block)
subject.acquire_lock_for(key, retry_delay: new_retry_delay, &block)
end
end
end

0 comments on commit 7bfd25d

Please sign in to comment.