Class: SpeedeeLabelWorker

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Job, Workers::LabelPrinting, Workers::StatusBroadcastable
Defined in:
app/workers/speedee_label_worker.rb

Overview

Finalizes a Spee-Dee (Dispatch Science) label after the order was created.

Spee-Dee's create-order call returns immediately with placeholder barcodes
("01", "02", …); a carrier-side background job then rewrites them with the
real account/type barcode (e.g. "SP90035P47SD42972901") — usually <1s, up to
~10s. Polling for that would tie up a web request, so Shipping::SpeedeeDelivery
only does the fast create POST (storing the orderId on
delivery.freight_order_number) and the controller enqueues this worker, then
redirects to the job page. Here we poll the barcode, set each shipment's
tracking number, render the label PDFs, and transition the delivery — the same
async shape GetFreightquoteLoadNumber uses for its load number.

On success it pipes the labels to the operator's print profile HERE (see
Workers::LabelPrinting — the landing page can't be relied on) and stores a
redirect to +complete_asynch_print_speedee+; on timeout it redirects back to
the delivery with an error so the operator can retry.

Instance Attribute Summary

Attributes included from Workers::StatusBroadcastable

#broadcast_status_updates

Instance Method Summary collapse

Methods included from Workers::StatusBroadcastable::Overrides

#at, #store, #total

Instance Method Details

#perform(options = {}) ⇒ void

This method returns an undefined value.

Parameters:

  • options (Hash) (defaults to: {})

    serialized worker options (string or symbol keys)

Options Hash (options):

  • delivery_id (Integer)

    Delivery whose Spee-Dee label is finalized



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/workers/speedee_label_worker.rb', line 32

def perform(options = {})
  options = options.deep_symbolize_keys
  delivery = Delivery.find_by(id: options[:delivery_id])
  return unless delivery

  order_id = delivery.freight_order_number
  if order_id.blank?
    store redirect_to: "/deliveries/#{delivery.id}", error_message: 'Spee-Dee order id missing; retry label generation.'
    return
  end

  result = Shipping::SpeedeeDispatchScienceClient.new.items_when_ready(order_id: order_id)
  unless result[:status] == :ok
    Rails.logger.warn("SpeedeeLabelWorker: barcodes not ready for delivery #{delivery.id} order #{order_id}: #{result[:message]}")
    store redirect_to: "/deliveries/#{delivery.id}",
          error_message: "Spee-Dee label barcodes did not finalize (#{result[:message]}). Please retry label generation."
    return
  end

  case finalize_labels(delivery, result[:items])
  when :locked
    # Advisory lock held by a concurrent buy/void — raise so Sidekiq retries
    # (retry: 5) rather than reporting success on work that never ran.
    raise "SpeedeeLabelWorker: label lock busy for delivery #{delivery.id}; retrying"
  when :ok
    # Print BEFORE handing back to the browser: complete_asynch_print_speedee
    # only runs if the operator is still on the job page, which a queued job
    # can't count on.
    store({ redirect_to: "/deliveries/#{delivery.id}/complete_asynch_print_speedee" }
            .merge(print_labels_status(delivery.reload, options[:print_intent])))
  else # :incomplete — do NOT send the operator to the print page as if it shipped
    store redirect_to: "/deliveries/#{delivery.id}",
          error_message: 'Some Spee-Dee labels did not finalize. Please retry label generation.'
  end
end