From f6ea2b2cf521bba08e7d96d26487d41bdf99859d Mon Sep 17 00:00:00 2001 From: Alexandros Milaios Date: Wed, 20 Sep 2023 12:47:20 +0300 Subject: [PATCH] feat: reset session in every sleep --- .../source_facebook_marketing/api.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/airbyte-integrations/connectors/source-facebook-marketing/source_facebook_marketing/api.py b/airbyte-integrations/connectors/source-facebook-marketing/source_facebook_marketing/api.py index 8897ace2b60e..906e48846408 100644 --- a/airbyte-integrations/connectors/source-facebook-marketing/source_facebook_marketing/api.py +++ b/airbyte-integrations/connectors/source-facebook-marketing/source_facebook_marketing/api.py @@ -43,11 +43,15 @@ class Throttle: # Insights async jobs throttle _ads_insights_throttle: Throttle + _access_token = None @property def ads_insights_throttle(self) -> Throttle: return self._ads_insights_throttle + def set_ads_insights_throttle(self, ads_insights_throttle): + self._ads_insights_throttle = ads_insights_throttle + @staticmethod def _parse_call_rate_header(headers): usage = 0 @@ -88,6 +92,10 @@ def _parse_call_rate_header(headers): return usage, pause_interval + @classmethod + def set_access_token(cls, access_token): + cls._access_token = access_token + def _compute_pause_interval(self, usage, pause_interval): """The sleep time will be calculated based on usage consumed.""" if usage >= self.MAX_RATE: @@ -123,6 +131,10 @@ def _handle_call_rate_limit(self, response, params): sleep_time = self._compute_pause_interval(usage=usage, pause_interval=pause_interval) logger.warning(f"Utilization is too high ({usage})%, pausing for {sleep_time}") sleep(sleep_time.total_seconds()) + logger.warning("resetting session after sleep") + api = MyFacebookAdsApi.init(access_token=MyFacebookAdsApi._access_token, crash_log=False) + api.set_ads_insights_throttle(self._ads_insights_throttle) + FacebookAdsApi.set_default_api(api) def _update_insights_throttle_limit(self, response: FacebookResponse): """ @@ -164,6 +176,7 @@ def __init__(self, account_id: str, access_token: str): self._account_id = account_id # design flaw in MyFacebookAdsApi requires such strange set of new default api instance self.api = MyFacebookAdsApi.init(access_token=access_token, crash_log=False) + self.api.set_access_token(access_token) FacebookAdsApi.set_default_api(self.api) @cached_property