Sha256: 423570171df0bf6f3ba61045a9458a9162b992d2dc70815751f206f94518e519
Contents?: true
Size: 1.11 KB
Versions: 15
Compression:
Stored size: 1.11 KB
Contents
module Spree class OrderMutex < Spree::Base class LockFailed < StandardError; end belongs_to :order, class_name: "Spree::Order" scope :expired, -> { where(arel_table[:created_at].lteq(Spree::Config[:order_mutex_max_age].seconds.ago)) } class << self # Obtain a lock on an order, execute the supplied block and then release the lock. # Raise a LockFailed exception immediately if we cannot obtain the lock. # We raise instead of blocking to avoid tying up multiple server processes waiting for the lock. def with_lock!(order) raise ArgumentError, "order must be supplied" if order.nil? # limit the maximum lock time just in case a lock is somehow left in place accidentally expired.where(order: order).delete_all begin order_mutex = create!(order: order) rescue ActiveRecord::RecordNotUnique error = LockFailed.new("Could not obtain lock on order #{order.id}") logger.error error.inspect raise error end yield ensure order_mutex.destroy if order_mutex end end end end
Version data entries
15 entries across 15 versions & 1 rubygems