Class: GetFreightquoteLoadNumber

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

Overview

Polls the CHR/Freightquote events API for the carrier load number (and the
PRO number when e-BOL is enabled), then renders the delivery's shipping
documents and pipes them to the operator's print profile.

Document rendering and printing both live HERE rather than in
+complete_asynch_print_bol+, which only runs if the operator is still sitting
on the job page when the job finishes. A job queued behind Sidekiq worker
starvation or a slow carrier lands minutes later, by which time they have
navigated away — and for Freightquote that lost the BOL and commercial
invoice entirely, not just the print job.

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 = {}) ⇒ Object

Parameters:

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

    job options

Options Hash (options):

  • delivery_id (Integer)

    ID of the Delivery to poll a load number for

  • print_intent (Object)

    print intent passed to the labels status payload



23
24
25
26
27
28
29
30
31
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/workers/get_freightquote_load_number.rb', line 23

def perform(options = {})
  # { delivery_id: @delivery.id,
  #   current_user_id: @context_user.id }.deep_stringify_keys

  delivery = Delivery.find(options[:delivery_id])
  load_number = nil
  pro_number = nil
  e_bol_enabled = nil

  # poll on the CHR/Freightquote events API
  20.times do |_i|
    load_result = WyShipping.check_freightquote_events_for_load_number(delivery)
    load_number = load_result.dig(:load_number) # get load_number from load_result
    e_bol_enabled = load_result.dig(:e_bol_enabled).to_b # get e_bol_enabled flag from result, if true, we wait for pro_number, otherwise they will not provide it until the driver arrives with it
    pro_number = load_result.dig(:pro_number) if e_bol_enabled # get pro_number from load_result in case it's there already
    if load_number.present?
      break unless e_bol_enabled == true && pro_number.blank?

      # still waiting for PRO number, so poll on the CHR/Freightquote events API
      20.times do |_i|
        pro_result = WyShipping.check_freightquote_events_for_pro_number(delivery)
        pro_number = pro_result.dig(:pro_number) # get pro_number from pro_result
        break if pro_number.present?

        # with e_bol_enabled true, we need load_number and pro_number

        sleep(3)
      end

    # with e_bol_enabled false, all we need is load_number so break out of the loop

    else
      sleep(3)
    end
  end
  if load_number.present? # we need this at minimum, if e_bol_enabled and we still have no pro_number, then let the LTL driver give it to warehouse at pickup time as the fallback, otherwise set it
    # Direct update bypasses Delivery's state machine, so the before_transition
    # callback that normally writes ship_labeled_at on entry to
    # pending_ship_confirm doesn't fire — set it inline here so the warehouse
    # mass ship-confirm carrier-pickup-time gate sees a real timestamp.
    delivery.update(
      freight_load_number: load_number,
      state: 'pending_ship_confirm',
      ship_labeled_at: delivery.ship_labeled_at || Time.current
    )
    delivery.update(ltl_pro_number: pro_number) if pro_number.present?
    # The BOL / commercial invoice / combined-labels PDFs are the deliverable
    # here, so render them where the job runs. A failure must not lose the
    # load number we just recorded — log and still hand the operator back.
    #
    # Guarded on bol_pdf because super_fetch re-queues a job killed mid-flight
    # (a Sidekiq restart does exactly that) and a second pass would append a
    # duplicate document set. Delivery::GenerateLabels skips generate_bol_pdf
    # for Freightquote precisely so this flow owns it, so a present BOL means
    # this job already ran. Not transactional: a run that died BETWEEN the BOL
    # and the CI leaves the rest missing, same as the rescue below does today.
    if delivery.bol_pdf.blank?
      begin
        delivery.generate_asynch_labels
      rescue StandardError => e
        ErrorReporting.error(e)
        Rails.logger.error("GetFreightquoteLoadNumber: generate_asynch_labels failed for delivery #{delivery.id}: #{e.message}")
      end
    end
    store({ redirect_to: "/deliveries/#{delivery.id}/complete_asynch_print_bol" }
            .merge(print_labels_status(delivery.reload, options[:print_intent])))
  else # this means we have run out of time with no load number, we must have that to proceed
    error_message = "Timed out trying to get Freightquote carrier load number confirmation!"
    store redirect_to: "/deliveries/#{delivery.id}", error_message: error_message
  end
end