Skip to content

Commit 9f4737e

Browse files
authored
Merge pull request #5577 from bcgov/feat/5383-4
fix(5575): add retry with delay for Mailchimp email update errors
2 parents e004bcd + db2c75c commit 9f4737e

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

helm/tools/dags/_update_mailchimp_list.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import time
2+
import json
3+
from bson.objectid import ObjectId
14
import mailchimp_marketing as MailchimpMarketing
25
from mailchimp_marketing.api_client import ApiClientError
36
from _utils import generate_md5_hash
47
from _projects import get_mongo_db
5-
from bson.objectid import ObjectId
6-
import json
78

89

910
def fetch_unique_emails(mongo_conn_id):
@@ -108,13 +109,31 @@ def remove_tag_from_emails(self, emails, tag_name):
108109
def wait_for_batch_complete(self, batch_id):
109110
if batch_id is None:
110111
return
112+
113+
max_exceptions = 20
114+
exception_count = 0
115+
111116
while True:
112-
response = self.client.batches.status(batch_id)
113-
status = response.get("status")
114-
print(f"batch status check: '{batch_id}' - '{status}'")
117+
try:
118+
response = self.client.batches.status(batch_id)
119+
status = response.get("status")
120+
print(f"batch status check: '{batch_id}' - '{status}'")
121+
except ApiClientError as e:
122+
exception_count += 1
123+
print(f"Error while checking batch status (attempt {exception_count}): {e}")
124+
if exception_count > max_exceptions:
125+
raise RuntimeError(f"Too many errors while checking batch status (>{max_exceptions})") from e
126+
time.sleep(5)
127+
continue
128+
129+
# Reset counter after a successful call
130+
exception_count = 0
131+
115132
if status == "finished":
116133
break
117134

135+
time.sleep(5)
136+
118137
def get_all_members(self):
119138
members = []
120139
offset = 0

0 commit comments

Comments
 (0)