
import logging
from datetime import timedelta
from django.core.management.base import BaseCommand, CommandError
from django.contrib.sessions.models import Session
from django.utils import timezone
from myuw.logger.timer import Timer

logger = logging.getLogger(__name__)
begin_delta = 1920
log_format = "Deleted django sessions expired before {}, Time={} seconds"


class Command(BaseCommand):

    def handle(self, *args, **options):
        now = timezone.now()
        for ddelta in range(begin_delta, 0, -1):
            timer = Timer()
            cut_off_dt = now - timedelta(days=ddelta)
            try:
                qset = Session.objects.filter(expire_date__lt=cut_off_dt)
                if qset.exists():
                    qset.delete()
                    logger.info(log_format.format(cut_off_dt.date(),
                                                  timer.get_elapsed()))

            except Exception as ex:
                logger.error(str(ex))


import logging
from datetime import timedelta
import time
from django.core.management.base import BaseCommand, CommandError
from django.contrib.sessions.models import Session
from django.utils import timezone
from myuw.logger.timer import Timer

logger = logging.getLogger(__name__)
begin_delta = 1920
log_format = "Deleted django sessions expired before {}, Time={} seconds"


class Command(BaseCommand):

    def handle(self, *args, **options):
        now = timezone.now()
        for ddelta in range(begin_delta, 0, -1):
            timer = Timer()
            cut_off_dt = now - timedelta(days=ddelta)
            try:
                qset = Session.objects.filter(expire_date__lt=cut_off_dt)
                if qset.exists():
                    qset.delete()
                    logger.info(log_format.format(cut_off_dt.date(),
                                                  timer.get_elapsed()))
                    time.sleep(5)
            except Exception as ex:
                logger.error(str(ex))

Add a 5 second pause
Add a 5 second pause

  
  import logging
  from datetime import timedelta
+ import time
  from django.core.management.base import BaseCommand, CommandError
  from django.contrib.sessions.models import Session
  from django.utils import timezone
  from myuw.logger.timer import Timer
  
  logger = logging.getLogger(__name__)
  begin_delta = 1920
  log_format = "Deleted django sessions expired before {}, Time={} seconds"
  
  
  class Command(BaseCommand):
  
      def handle(self, *args, **options):
          now = timezone.now()
          for ddelta in range(begin_delta, 0, -1):
              timer = Timer()
              cut_off_dt = now - timedelta(days=ddelta)
              try:
                  qset = Session.objects.filter(expire_date__lt=cut_off_dt)
                  if qset.exists():
                      qset.delete()
                      logger.info(log_format.format(cut_off_dt.date(),
                                                    timer.get_elapsed()))
- 
+                     time.sleep(5)
              except Exception as ex:
                  logger.error(str(ex))
  
Add a 5 second pause
## Code Before:

import logging
from datetime import timedelta
from django.core.management.base import BaseCommand, CommandError
from django.contrib.sessions.models import Session
from django.utils import timezone
from myuw.logger.timer import Timer

logger = logging.getLogger(__name__)
begin_delta = 1920
log_format = "Deleted django sessions expired before {}, Time={} seconds"


class Command(BaseCommand):

    def handle(self, *args, **options):
        now = timezone.now()
        for ddelta in range(begin_delta, 0, -1):
            timer = Timer()
            cut_off_dt = now - timedelta(days=ddelta)
            try:
                qset = Session.objects.filter(expire_date__lt=cut_off_dt)
                if qset.exists():
                    qset.delete()
                    logger.info(log_format.format(cut_off_dt.date(),
                                                  timer.get_elapsed()))

            except Exception as ex:
                logger.error(str(ex))

## Instruction:
Add a 5 second pause
## Code After:

import logging
from datetime import timedelta
import time
from django.core.management.base import BaseCommand, CommandError
from django.contrib.sessions.models import Session
from django.utils import timezone
from myuw.logger.timer import Timer

logger = logging.getLogger(__name__)
begin_delta = 1920
log_format = "Deleted django sessions expired before {}, Time={} seconds"


class Command(BaseCommand):

    def handle(self, *args, **options):
        now = timezone.now()
        for ddelta in range(begin_delta, 0, -1):
            timer = Timer()
            cut_off_dt = now - timedelta(days=ddelta)
            try:
                qset = Session.objects.filter(expire_date__lt=cut_off_dt)
                if qset.exists():
                    qset.delete()
                    logger.info(log_format.format(cut_off_dt.date(),
                                                  timer.get_elapsed()))
                    time.sleep(5)
            except Exception as ex:
                logger.error(str(ex))
