Class: Alchemy::PageMutex

Inherits:
BaseRecord
  • Object
show all
Defined in:
app/models/alchemy/page_mutex.rb

Defined Under Namespace

Classes: LockFailed

Constant Summary collapse

MAX_AGE =

seconds

300

Constants included from SearchableResource

SearchableResource::SEARCHABLE_COLUMN_TYPES

Class Method Summary collapse

Methods included from SearchableResource

#ransackable_associations, #ransackable_attributes, #ransortable_attributes

Class Method Details

.with_lock!(page) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/models/alchemy/page_mutex.rb', line 13

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