Skip to content

Commit

Permalink
Merge pull request #56 from GlodoUK/GH115206
Browse files Browse the repository at this point in the history
GH115206 - Split tracking job into batches
  • Loading branch information
ellbristow authored Jun 7, 2024
2 parents 720ef5a + 50101c5 commit 7bb0490
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions delivery_parcelhub_whistl/models/delivery.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,8 +551,8 @@ def whistl_cancel_shipment(self, pickings):
)
picking.state = "cancel"

def whistl_tracking_state_update_scheduled(self):
pickings = self.env["stock.picking"].search(
def _whistl_get_waiting_pickings(self, limit=100):
return self.env["stock.picking"].search(
[
("carrier_id", "=", self.id),
("state", "=", "done"),
Expand All @@ -565,12 +565,18 @@ def whistl_tracking_state_update_scheduled(self):
"|",
("carrier_consignment_ref", "!=", False),
("carrier_tracking_ref", "!=", False),
]
],
limit=limit, # Added this to fix timeouts etc
)

def whistl_tracking_state_update_scheduled(self, batch_size=100):
pickings = self._whistl_get_waiting_pickings(limit=batch_size)
for picking in pickings:
if picking.delivery_state in ["customer_delivered", "warehouse_delivered"]:
continue
self.whistl_tracking_state_update(picking)
if self._whistl_get_waiting_pickings():
self.with_delay().whistl_tracking_state_update_scheduled()

def whistl_tracking_state_update(self, picking):
request_url = self._get_whistl_tracking_url(
Expand Down Expand Up @@ -598,7 +604,19 @@ def whistl_tracking_state_update(self, picking):
).format(status_code=response.status_code, message=message)
)

tracking_response = ET.fromstring(response.content)
try:
tracking_response = ET.fromstring(response.content)
except Exception as e:
raise ValidationError(
_(
"Failed to get tracking information for picking %(picking_name)s\n\n"
"Response:\n%(response_content)s"
)
% {
"picking_name": picking.name,
"response_content": response.content,
}
) from e
events = tracking_response.findall(".//TrackingEvent")

sorted_events = []
Expand Down

0 comments on commit 7bb0490

Please sign in to comment.