Class: Shipping::DeliveryReadyForLabelingHandler

Inherits:
ApplicationJob
  • Object
show all
Includes:
RailsEventStore::AsyncHandler
Defined in:
app/subscribers/shipping/delivery_ready_for_labeling_handler.rb

Overview

Refreshes the from_delivery Packing record for a Delivery that just
transitioned from picking into pending_ship_labels.

Subscribes to: Events::DeliveryReadyForLabeling

WHY async: the prior synchronous set_packaged_items_md5_hash call
in the picking → pending_ship_labels after_transition block added
50-150 ms to the warehouse "ready to label" click — DeliveryMd5Extractor
runs a shipments+contents+line_items read, an Md5Hash hash over every
packable line, a Packing.upsert, and a HABTM item_ids |= write.
That latency is bookkeeping, not transactional data, so deferring it
to a Sidekiq tick is safe and noticeably speeds up the transition.

Skip semantics carry over from the original after_transition block:
run only when the delivery has non-shipping line items and is not a
warehouse pickup. The publisher gates on those predicates; this handler
re-checks after re-querying by id so a same-transaction destroy
(or stale queue retry against a transitioned-back delivery) is a clean
no-op rather than a noisy raise.

Instance Method Summary collapse

Instance Method Details

#perform(event) ⇒ void

This method returns an undefined value.

Re-queries the delivery and runs Shipping::DeliveryMd5Extractor when the
gating predicates still apply, refreshing the from_delivery Packing row.

Parameters:

  • event (RubyEventStore::Event)

    the persisted Events::DeliveryReadyForLabeling event

Raises:

  • (StandardError)

    if the extractor or any predicate raises; reported then re-raised so Sidekiq retries



36
37
38
39
40
41
42
43
44
45
46
# File 'app/subscribers/shipping/delivery_ready_for_labeling_handler.rb', line 36

def perform(event)
  delivery = Delivery.find_by(id: event.data[:delivery_id])
  return unless delivery
  return if delivery.is_warehouse_pickup?
  return unless delivery.line_items.non_shipping.any?

  Shipping::DeliveryMd5Extractor.new.process(delivery)
rescue StandardError => e
  ErrorReporting.error(e)
  raise
end