Class: SpreeCmCommissioner::InventoryHolds::Release
- Inherits:
-
Object
- Object
- SpreeCmCommissioner::InventoryHolds::Release
- Extended by:
- ServiceModuleThrowable
- Includes:
- Spree::ServiceModule::Base
- Defined in:
- app/services/spree_cm_commissioner/inventory_holds/release.rb
Instance Method Summary collapse
Methods included from ServiceModuleThrowable
Instance Method Details
#call(hold:, reason: :hold_expired, scheduled_release: false) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'app/services/spree_cm_commissioner/inventory_holds/release.rb', line 7 def call(hold:, reason: :hold_expired, scheduled_release: false) return success(hold) if hold.finalized? InventoryHold.transaction do hold.lock! # Already released or converted — nothing to do. next if hold.finalized? # Skip stale scheduled jobs that fire after the hold was extended. # Applies to both hold_expired (ReleaseJob) and payment_timeout (ReleasePaymentLockedJob): # if the hold is still within its window, ignore this job; a newer one is already scheduled. next if scheduled_release && hold.expires_at > Time.current hold.update!(status: :released, release_reason: reason) restock_redis!(hold) enqueue_sync_inventory_on_hold(hold.order) end success(hold) rescue ActiveRecord::StaleObjectError # Another process beat this process to the lock (hold already released/finalized concurrently). # Treat as idempotent success: the goal (hold no longer active) is achieved. CmAppLogger.error(label: "#{self.class.name}#call stale object error", data: { hold_id: hold.id }) success(nil) rescue StandardError => e error = { error_type: e.class.name.demodulize, hold_id: hold.id, message: e. } CmAppLogger.error(label: "#{self.class.name}#call failed", data: error) failure(nil, e.) end |