Skip to content

Commit

Permalink
Use with_advisory_lock to lock/check if a publication is already in t…
Browse files Browse the repository at this point in the history
…he make-me-finalizer process (see papyri#169)
  • Loading branch information
ryanfb committed Apr 28, 2016
1 parent 3bf7479 commit fe2a072
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@ gem 'warbler'
gem 'puma'
gem 'database_cleaner'
gem 'sucker_punch', '~> 1.0'
gem 'with_advisory_lock'

gem 'test_after_commit', :group => :test
5 changes: 5 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ GEM
test_after_commit (0.4.1)
activerecord (>= 3.2)
thor (0.19.1)
thread_safe (0.3.5-java)
tilt (1.4.1)
timers (4.1.1)
hitimes
Expand All @@ -180,6 +181,9 @@ GEM
jruby-rack (>= 1.1.1, < 1.3)
rake (>= 0.9.6)
rubyzip (>= 0.9, < 1.2)
with_advisory_lock (3.0.0)
activerecord (>= 3.2)
thread_safe

PLATFORMS
java
Expand Down Expand Up @@ -215,6 +219,7 @@ DEPENDENCIES
test_after_commit
uglifier (>= 1.0.3)
warbler
with_advisory_lock

BUNDLED WITH
1.11.2
10 changes: 7 additions & 3 deletions app/controllers/publications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -301,14 +301,18 @@ def become_finalizer
# TODO make sure we don't steal it from someone who is working on it
@publication = Publication.find(params[:id].to_s)
original_publication_owner_id = @publication.owner.id

@publication.with_lock do
#note this can only be called on a board owned publication
if @publication.owner_type != "Board"
#note this can only be called on a board owned publication
flash[:error] = "Can't change finalizer on non-board copy of publication."
redirect_to show
elsif @publication.advisory_lock_exists?("become_finalizer_#{@publication.id}")
flash[:notice] = "Another user is currently making themselves the finalizer."
redirect_to show
else
SendToFinalizerJob.new.async.perform(@publication.id, @current_user.id)
end

SendToFinalizerJob.new.async.perform(@publication.id, @current_user.id)
end

flash[:notice] = "Finalizer change running. Check back in a few minutes."
Expand Down
6 changes: 4 additions & 2 deletions app/jobs/send_to_finalizer_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ def perform(publication_id, user_id = nil)
ActiveRecord::Base.connection_pool.with_connection do
publication = Publication.find(publication_id)
publication.with_lock do
user = User.find(user_id) unless user_id.nil?
publication.send_to_finalizer(user)
publication.with_advisory_lock("become_finalizer_#{publication_id}") do
user = User.find(user_id) unless user_id.nil?
publication.send_to_finalizer(user)
end
end
end
ensure
Expand Down

0 comments on commit fe2a072

Please sign in to comment.