From 7521d5ddf28f8bd7641a3f99eb1b8557a9a701f9 Mon Sep 17 00:00:00 2001 From: Kain Date: Sat, 11 Jul 2026 17:56:34 -0700 Subject: [PATCH] refactor: improve device activity polling with exponential backoff MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace the linear polling in `_wait_for_device_activity_completion` with exponential backoff and extract magic numbers into parameters. - Remove unconditional 10s sleep before the first status check - Switch from linear `10 * (retry + 1)` to exponential `2 ** (retry + 1)` backoff: 2s → 4s → 8s → 16s → 32s (62s total, down from 150s) - Add `backoff_factor` and `max_retries` keyword parameters with sensible defaults for configurability --- pyaxm/client.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/pyaxm/client.py b/pyaxm/client.py index c6455be..2c116fd 100644 --- a/pyaxm/client.py +++ b/pyaxm/client.py @@ -104,12 +104,16 @@ def get_device_server_assignment(self, device_id: str) -> OrgDeviceAssignedServe response = self.abm.get_device_server_assignment(device_id, self.token_manager.get_token_value()) return response.data - def _wait_for_device_activity_completion(self, activity_id: str) -> OrgDeviceActivity: - time.sleep(10) + def _wait_for_device_activity_completion( + self, + activity_id: str, + backoff_factor: int = 2, + max_retries: int = 5, + ) -> OrgDeviceActivity: activity_response = self.abm.get_device_activity(activity_id, self.token_manager.get_token_value()) retry = 0 - while activity_response.data.attributes.status == 'IN_PROGRESS' and retry < 5: - time.sleep(10 * retry) + while activity_response.data.attributes.status == 'IN_PROGRESS' and retry < max_retries: + time.sleep(backoff_factor ** (retry + 1)) retry += 1 activity_response = self.abm.get_device_activity(activity_id, self.token_manager.get_token_value()) return activity_response.data