Class: OverdueToShipSweep

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Job
Defined in:
app/workers/overdue_to_ship_sweep.rb

Overview

Daily sweep: Amazon orders released to the warehouse that blew their ship-by
deadline without shipping. Emails orders@ and the origin warehouse so someone
ships the parcel before the date Amazon promised the buyer slips — the San
Clemente miss (ship-by 7/10, shipped 7/13) that dinged OTDR. A warning only;
it never re-shops the label.

The delivery estimate is fixed at import assuming a same-day ship and nothing
recalculates it when we don't, so a slipped order looks fine in our own
numbers while Amazon scores it late.

Gated on a Rails.cache flag per order (ALERT_CACHE_TTL) so a persistently
stuck order re-nudges every few days rather than every run — the
ProblematicDeliverySweep convention.

Scheduled daily via sidekiq-scheduler — see config/sidekiq_production_schedule.yml
and config/sidekiq_staging_schedule.yml.

Constant Summary collapse

ALERT_CACHE_TTL =

Constant.

3.days
MAX_ALERTS_PER_RUN =

Blast-radius cap: a warehouse-wide backlog must not enqueue an unbounded
flood of emails. Uncapped orders wait for the next daily run; the cache
claim keeps already-alerted orders from re-sending.

200

Instance Method Summary collapse

Instance Method Details

#performObject

Runs the job.

Returns:

  • (Object)

    the result



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/workers/overdue_to_ship_sweep.rb', line 39

def perform
  alerted = 0
  Order.overdue_to_ship.find_each do |order|
    if alerted >= MAX_ALERTS_PER_RUN
      Rails.logger.warn "[OverdueToShipSweep] per-run cap (#{MAX_ALERTS_PER_RUN}) reached — " \
                        'remaining deferred to the next run'
      break
    end
    next unless claim_alert!(order)

    alerted += 1
    OverdueToShipAlertMailer.alert(order).deliver_later
    Rails.logger.warn "[OverdueToShipSweep] overdue to ship: order #{order.reference_number} " \
                      "(ship-by #{order.requested_ship_before}, state=#{order.state})"
  rescue StandardError => e
    # Release the claim so a failed send isn't suppressed for ALERT_CACHE_TTL;
    # the next run re-claims. One bad order must not stop the sweep.
    Rails.cache.delete(cache_key(order))
    Rails.logger.error "[OverdueToShipSweep] failed for order #{order.id}: #{e.class}: #{e.message}"
  end
end