Skip to content

Commit

Permalink
something
Browse files Browse the repository at this point in the history
  • Loading branch information
AlirezaYousefpourM committed Jan 22, 2025
1 parent 4b0beb1 commit 04f33e9
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 50 deletions.
54 changes: 29 additions & 25 deletions backend/backend_api/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@


class MailerThread(threading.Thread):
_MAXIMUM_RETRIES = 5
_SLEEP_INTERVAL_SECONDS = 5
_MAXIMUM_RETRIES = 10
_SLEEP_INTERVAL_SECONDS = 60
CHUNK_SIZE = 70
CHUNK_SLEEP_INTERVAL = 60 # In seconds

def __init__(self, subject: str, targets: list[str], html_body: str):
self.subject = subject
Expand All @@ -22,28 +24,30 @@ def run(self):
html_message = self.HTML_body
print('STARTING TO SEND MAILS')
print(self.targets)
for index, chunk in enumerate([self.targets[pointer:min(pointer+self.CHUNK_SIZE, len(self.targets))] for pointer in range(0, len(self.targets), self.CHUNK_SIZE)]):
email = EmailMessage(
subject=self.subject,
body=html_message,
from_email=settings.EMAIL_HOST_USER,
to=chunk,
reply_to=(settings.EMAIL_HOST_USER,)
)
email.content_subtype = "html"
if ENABLE_SENDING_EMAIL:
has_send = False
for i in range(self._MAXIMUM_RETRIES):
try:
email.send(fail_silently=False)
has_send = True
break
except Exception as e:
print(f"Exception raised while sending email: {e}")
sleep(self._SLEEP_INTERVAL_SECONDS)
if not has_send:
print(f"Failed to send email to {chunk} after {self._MAXIMUM_RETRIES} retries")
else:
print(f"Sent the following email (change ENABLE_SENDING_EMAIL to True to actually send the email):")
print(f"To: {email.bcc}\nSubject: {email.subject}\n\nBody: {email.body}\n\n")

email = EmailMessage(
subject=self.subject,
body=html_message,
from_email=settings.EMAIL_HOST_USER,
to=self.targets,
reply_to=(settings.EMAIL_HOST_USER,)
)
email.content_subtype = "html"
if ENABLE_SENDING_EMAIL:
has_send = False
for i in range(self._MAXIMUM_RETRIES):
try:
email.send(fail_silently=False)
has_send = True
break
except Exception as e:
print(f"Exception raised while sending email: {e}")
sleep(self._SLEEP_INTERVAL_SECONDS)
if not has_send:
print(f"Failed to send email to {self.targets} after {self._MAXIMUM_RETRIES} retries")
else:
print(f"Sent the following email (change ENABLE_SENDING_EMAIL to True to actually send the email):")
print(f"To: {email.bcc}\nSubject: {email.subject}\n\nBody: {email.body}\n\n")
sleep(self.CHUNK_SLEEP_INTERVAL)
print("SENDING DONE")
55 changes: 30 additions & 25 deletions backend/backend_api/sms.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@
LINE_NUMBER = settings.SMS_LINE_NUMBER

class SMSThread(threading.Thread):
CHUNK_SIZE = 99
CHUNK_SEND_INTERVAL = 60 # In seconds
def __init__(self, message_text: str, mobiles: list[str]):
super().__init__()
self.message_text = message_text
self.mobiles = mobiles
self.max_retries = 5
self.retry_interval = 5 # seconds
self.retry_interval = 60 # seconds

def send_sms(self):
@staticmethod
def send_sms(mobiles, message_text):
conn = http.client.HTTPSConnection("api.sms.ir")
payload = json.dumps({
"lineNumber": LINE_NUMBER,
"messageText": self.message_text,
"mobiles": self.mobiles,
"messageText": message_text,
"mobiles": mobiles,
"sendDateTime": None
})

Expand All @@ -35,25 +38,27 @@ def send_sms(self):
return res.status, res.read()

def run(self):
attempts = 0
while attempts < self.max_retries:
try:
attempts += 1
status, response = self.send_sms()

if status == 200:
print(f"SMS successfully sent to {self.mobiles}: {response.decode('utf-8')}")
break
else:
print(f"Failed to send SMS (Attempt {attempts}/{self.max_retries}): {response.decode('utf-8')}")

except Exception as e:
print(f"Error sending SMS to {self.mobiles} (Attempt {attempts}/{self.max_retries}): {str(e)}")

# Wait before retrying
if attempts < self.max_retries:
print(f"Retrying in {self.retry_interval} seconds...")
time.sleep(self.retry_interval)
else:
print(f"SMS sending failed after {self.max_retries} attempts.")
for index, chunk in enumerate([self.mobiles[pointer:min(pointer+self.CHUNK_SIZE, len(self.mobiles))] for pointer in range(0, len(self.mobiles), self.CHUNK_SIZE)]):
attempts = 0
while attempts < self.max_retries:
try:
attempts += 1
status, response = self.send_sms(self.message_text, chunk)

if status == 200:
print(f"SMS successfully sent to {self.mobiles}: {response.decode('utf-8')}")
break
else:
print(f"Failed to send SMS (Attempt {attempts}/{self.max_retries}): {response.decode('utf-8')}")

except Exception as e:
print(f"Error sending SMS to {self.mobiles} (Attempt {attempts}/{self.max_retries}): {str(e)}")

# Wait before retrying
if attempts < self.max_retries:
print(f"Retrying in {self.retry_interval} seconds...")
time.sleep(self.retry_interval)
else:
print(f"SMS sending failed after {self.max_retries} attempts. chunk index: {index}")
time.sleep(self.CHUNK_SEND_INTERVAL)

0 comments on commit 04f33e9

Please sign in to comment.