from datetime import datetime
from importlib import import_module

import pytz
from django.conf import settings

from app.schedule.celery import celery_app
from app.schedule.libs.sms import DeviceNotFoundError


@celery_app.task(bind=True)
def send_message(self, to, message, sg_user, sg_password):
    def load_sms_class():
        module_name, class_name = settings.APP_MESSENGER_CLASS.rsplit(".", 1)
        return getattr(import_module(module_name), class_name)

    sms_class = load_sms_class()
    try:
        messenger = sms_class(sg_user, sg_password)
        messenger.send_message(to, message)
    except DeviceNotFoundError as e:
        # Workaround for Celery issue. Remove after next version is released.
        tz = pytz.timezone(settings.TIME_ZONE)
        self.request.expires = tz.localize(datetime.strptime(self.request.expires[:-6], '%Y-%m-%dT%H:%M:%S'))
        self.retry(exc=e, max_retries=50)

from datetime import datetime
from importlib import import_module

import pytz
from django.conf import settings

from app.schedule.celery import celery_app
from app.schedule.libs.sms import DeviceNotFoundError


@celery_app.task(bind=True)
def send_message(self, to, message, sg_user, sg_password):
    def load_sms_class():
        module_name, class_name = settings.APP_MESSENGER_CLASS.rsplit(".", 1)
        return getattr(import_module(module_name), class_name)

    sms_class = load_sms_class()
    try:
        messenger = sms_class(sg_user, sg_password)
        messenger.send_message(to, message)
    except DeviceNotFoundError as e:
        # Workaround for Celery issue. Remove after next version is released.
        tz = pytz.timezone(settings.TIME_ZONE)
        self.request.expires = tz.localize(datetime.strptime(self.request.expires[:-6], '%Y-%m-%dT%H:%M:%S'))
        self.retry(exc=e, max_retries=2000, countdown=60 * 5)

Increase retry count and add countdown to retry method
Increase retry count and add countdown to retry method

  from datetime import datetime
  from importlib import import_module
  
  import pytz
  from django.conf import settings
  
  from app.schedule.celery import celery_app
  from app.schedule.libs.sms import DeviceNotFoundError
  
  
  @celery_app.task(bind=True)
  def send_message(self, to, message, sg_user, sg_password):
      def load_sms_class():
          module_name, class_name = settings.APP_MESSENGER_CLASS.rsplit(".", 1)
          return getattr(import_module(module_name), class_name)
  
      sms_class = load_sms_class()
      try:
          messenger = sms_class(sg_user, sg_password)
          messenger.send_message(to, message)
      except DeviceNotFoundError as e:
          # Workaround for Celery issue. Remove after next version is released.
          tz = pytz.timezone(settings.TIME_ZONE)
          self.request.expires = tz.localize(datetime.strptime(self.request.expires[:-6], '%Y-%m-%dT%H:%M:%S'))
-         self.retry(exc=e, max_retries=50)
+         self.retry(exc=e, max_retries=2000, countdown=60 * 5)
  
Increase retry count and add countdown to retry method
## Code Before:
from datetime import datetime
from importlib import import_module

import pytz
from django.conf import settings

from app.schedule.celery import celery_app
from app.schedule.libs.sms import DeviceNotFoundError


@celery_app.task(bind=True)
def send_message(self, to, message, sg_user, sg_password):
    def load_sms_class():
        module_name, class_name = settings.APP_MESSENGER_CLASS.rsplit(".", 1)
        return getattr(import_module(module_name), class_name)

    sms_class = load_sms_class()
    try:
        messenger = sms_class(sg_user, sg_password)
        messenger.send_message(to, message)
    except DeviceNotFoundError as e:
        # Workaround for Celery issue. Remove after next version is released.
        tz = pytz.timezone(settings.TIME_ZONE)
        self.request.expires = tz.localize(datetime.strptime(self.request.expires[:-6], '%Y-%m-%dT%H:%M:%S'))
        self.retry(exc=e, max_retries=50)

## Instruction:
Increase retry count and add countdown to retry method
## Code After:
from datetime import datetime
from importlib import import_module

import pytz
from django.conf import settings

from app.schedule.celery import celery_app
from app.schedule.libs.sms import DeviceNotFoundError


@celery_app.task(bind=True)
def send_message(self, to, message, sg_user, sg_password):
    def load_sms_class():
        module_name, class_name = settings.APP_MESSENGER_CLASS.rsplit(".", 1)
        return getattr(import_module(module_name), class_name)

    sms_class = load_sms_class()
    try:
        messenger = sms_class(sg_user, sg_password)
        messenger.send_message(to, message)
    except DeviceNotFoundError as e:
        # Workaround for Celery issue. Remove after next version is released.
        tz = pytz.timezone(settings.TIME_ZONE)
        self.request.expires = tz.localize(datetime.strptime(self.request.expires[:-6], '%Y-%m-%dT%H:%M:%S'))
        self.retry(exc=e, max_retries=2000, countdown=60 * 5)
