-
-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2613 from AlchemyCMS/backport/7.0-stable/pr-2612
[7.0-stable] Merge pull request #2612 from sascha-karnatz/harden-page-publisher
- Loading branch information
Showing
9 changed files
with
185 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
module Alchemy | ||
class PageMutex < BaseRecord | ||
class LockFailed < StandardError; end | ||
|
||
MAX_AGE = 300 # seconds | ||
|
||
belongs_to :page, class_name: "Alchemy::Page", optional: true | ||
|
||
scope :expired, -> { where(arel_table[:created_at].lteq(MAX_AGE.seconds.ago)) } | ||
|
||
def self.with_lock!(page) | ||
raise ArgumentError, "A page is necessary to lock it" if page.nil? | ||
|
||
# remove old expired page if it wasn't deleted before | ||
expired.where(page: page).delete_all | ||
|
||
begin | ||
page_mutex = create!(page: page) | ||
rescue ActiveRecord::RecordNotUnique | ||
error = LockFailed.new("Can't lock page #{page.id} twice!") | ||
logger.error error.inspect | ||
raise error | ||
end | ||
yield | ||
ensure | ||
page_mutex&.destroy | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class CreatePageMutexes < ActiveRecord::Migration[6.1] | ||
def change | ||
create_table :alchemy_page_mutexes do |t| | ||
t.references :page, null: false, index: {unique: true}, foreign_key: {to_table: :alchemy_pages} | ||
t.datetime :created_at | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class CreatePageMutexes < ActiveRecord::Migration[6.1] | ||
def change | ||
create_table :alchemy_page_mutexes do |t| | ||
t.references :page, null: false, index: {unique: true}, foreign_key: {to_table: :alchemy_pages} | ||
t.datetime :created_at | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# frozen_string_literal: true | ||
|
||
require "rails_helper" | ||
|
||
module Alchemy | ||
describe PageMutex do | ||
it { is_expected.to belong_to(:page).optional } | ||
|
||
let(:page) { create(:alchemy_page) } | ||
let(:page_mutex_active) { described_class.create(page: page, created_at: 2.minutes.ago) } | ||
let(:page_mutex_expired) { described_class.create(page: page, created_at: 6.minutes.ago) } | ||
|
||
describe "#expired" do | ||
it "should not have expired mutexes if one is active" do | ||
page_mutex_active | ||
expect(PageMutex.expired).to be_empty | ||
end | ||
|
||
it "should have expired mutexes if one is older than 5 minutes" do | ||
page_mutex_expired | ||
expect(PageMutex.expired.count).to eq(1) | ||
end | ||
end | ||
|
||
describe "#with_lock!" do | ||
context "without page" do | ||
it "fires an argument error" do | ||
expect { described_class.with_lock!(nil) }.to raise_error(ArgumentError) | ||
end | ||
end | ||
|
||
context "without a lock" do | ||
it "executes the block" do | ||
expect { |block| | ||
described_class.with_lock!(page, &block) | ||
}.to yield_control.once | ||
end | ||
|
||
it "executes the block multiple times (sequentially)" do | ||
expect { |block| | ||
described_class.with_lock!(page, &block) | ||
described_class.with_lock!(page, &block) | ||
}.to yield_control.twice | ||
end | ||
|
||
it "return the result of the block" do | ||
expect(described_class.with_lock!(page) { "foo" }).to eq("foo") | ||
end | ||
end | ||
|
||
context "with a look" do | ||
it "should not run in parallel and raise an exception" do | ||
described_class.with_lock!(page) do | ||
expect { |block| | ||
described_class.with_lock!(page, &block) | ||
}.to raise_error Alchemy::PageMutex::LockFailed | ||
end | ||
end | ||
|
||
it "should not run if an entry is already in the database" do | ||
page_mutex_active | ||
expect { |block| | ||
described_class.with_lock!(page, &block) | ||
}.to raise_error Alchemy::PageMutex::LockFailed | ||
end | ||
|
||
it "should allow multiple mutexes for different pages" do | ||
described_class.with_lock!(create(:alchemy_page)) do | ||
expect { |block| | ||
described_class.with_lock!(page, &block) | ||
}.to yield_control.once | ||
end | ||
end | ||
end | ||
|
||
context "with an expired lock" do | ||
it "should run if database entry is expired" do | ||
page_mutex_expired | ||
expect { |block| | ||
described_class.with_lock!(page, &block) | ||
}.to yield_control.once | ||
end | ||
end | ||
end | ||
end | ||
end |