# Path: carrot/models.py
# class ScheduledTask(models.Model):
#     """
#     A model for scheduling tasks to run at a certain interval
#     """
#     INTERVAL_CHOICES = (
#         ('seconds', 'seconds'),
#         ('minutes', 'minutes'),
#         ('hours', 'hours'),
#         ('days', 'days'),
#     )
# 
#     interval_type = models.CharField(max_length=200, choices=INTERVAL_CHOICES, default='seconds')
#     interval_count = models.PositiveIntegerField(default=1, validators=[MinValueValidator(1)])
# 
#     exchange = models.CharField(max_length=200, blank=True, null=True)
#     routing_key = models.CharField(max_length=200, blank=True, null=True)
#     queue = models.CharField(max_length=200, blank=True, null=True)
#     task = models.CharField(max_length=200)
#     task_args = models.TextField(null=True, blank=True, verbose_name='Positional arguments')
#     content = models.TextField(null=True, blank=True, verbose_name='Keyword arguments')
# 
#     active = models.BooleanField(default=True)
# 
#     task_name = models.CharField(max_length=200, unique=True)
# 
#     def get_absolute_url(self) -> str:
#         return reverse('edit-scheduled-task', args=[self.pk])
# 
#     @property
#     def interval_display(self) -> str:
#         return 'Every %i %s' % (self.interval_count, self.interval_type if self.interval_count > 1 else
#             self.interval_type[:-1])
# 
#     @property
#     def multiplier(self) -> int:
#         if self.interval_type == 'minutes':
#             return 60
# 
#         if self.interval_type == 'hours':
#             return 60 * 60
# 
#         if self.interval_type == 'days':
#             return 86400
# 
#         return 1
# 
#     @property
#     def positional_arguments(self) -> tuple:
#         if self.task_args:
#             return tuple([a.strip() for a in self.task_args.split(',') if a])
#         else:
#             return ()
# 
#     def publish(self, priority: int = 0) -> MessageLog:
#         from carrot.utilities import publish_message
#         kwargs = json.loads(self.content or '{}')
#         if isinstance(kwargs, str):
#             kwargs = {}
#         return publish_message(self.task, *self.positional_arguments, priority=priority, queue=self.queue,
#                                exchange=self.exchange or '', routing_key=self.routing_key or self.queue,
#                                **kwargs)
# 
#     def __str__(self) -> models.CharField:
#         return self.task


import threading
import time
from typing import List
from django.core.exceptions import ObjectDoesNotExist
from carrot.models import ScheduledTask
        self.id = scheduled_task.pk
        self.queue = scheduled_task.routing_key
        self.scheduled_task = scheduled_task
        self.run_now = run_now
        self.active = True
        self.filters = filters
        self.inactive_reason = ''

    def run(self) -> None:
        """
        Initiates a timer, then once the timer is equal to the ScheduledTask's interval, the scheduler
        checks to make sure that the task has not been deactivated/deleted in the mean time, and that the manager
        has not been stopped, then publishes it to  the queue
        """
        interval = self.scheduled_task.multiplier * self.scheduled_task.interval_count

        count = 0
        if self.run_now:
            self.scheduled_task.publish()

        while True:
            while count < interval:
                if not self.active:
                    if self.inactive_reason:
                        print('Thread stop has been requested because of the following reason: %s.\n Stopping the '
                              'thread' % self.inactive_reason)

                    return

                try:

# Path: carrot/models.py
# class ScheduledTask(models.Model):
#     """
#     A model for scheduling tasks to run at a certain interval
#     """
#     INTERVAL_CHOICES = (
#         ('seconds', 'seconds'),
#         ('minutes', 'minutes'),
#         ('hours', 'hours'),
#         ('days', 'days'),
#     )
# 
#     interval_type = models.CharField(max_length=200, choices=INTERVAL_CHOICES, default='seconds')
#     interval_count = models.PositiveIntegerField(default=1, validators=[MinValueValidator(1)])
# 
#     exchange = models.CharField(max_length=200, blank=True, null=True)
#     routing_key = models.CharField(max_length=200, blank=True, null=True)
#     queue = models.CharField(max_length=200, blank=True, null=True)
#     task = models.CharField(max_length=200)
#     task_args = models.TextField(null=True, blank=True, verbose_name='Positional arguments')
#     content = models.TextField(null=True, blank=True, verbose_name='Keyword arguments')
# 
#     active = models.BooleanField(default=True)
# 
#     task_name = models.CharField(max_length=200, unique=True)
# 
#     def get_absolute_url(self) -> str:
#         return reverse('edit-scheduled-task', args=[self.pk])
# 
#     @property
#     def interval_display(self) -> str:
#         return 'Every %i %s' % (self.interval_count, self.interval_type if self.interval_count > 1 else
#             self.interval_type[:-1])
# 
#     @property
#     def multiplier(self) -> int:
#         if self.interval_type == 'minutes':
#             return 60
# 
#         if self.interval_type == 'hours':
#             return 60 * 60
# 
#         if self.interval_type == 'days':
#             return 86400
# 
#         return 1
# 
#     @property
#     def positional_arguments(self) -> tuple:
#         if self.task_args:
#             return tuple([a.strip() for a in self.task_args.split(',') if a])
#         else:
#             return ()
# 
#     def publish(self, priority: int = 0) -> MessageLog:
#         from carrot.utilities import publish_message
#         kwargs = json.loads(self.content or '{}')
#         if isinstance(kwargs, str):
#             kwargs = {}
#         return publish_message(self.task, *self.positional_arguments, priority=priority, queue=self.queue,
#                                exchange=self.exchange or '', routing_key=self.routing_key or self.queue,
#                                **kwargs)
# 
#     def __str__(self) -> models.CharField:
#         return self.task


# Path: carrot/scheduler.py
import threading
import time
from typing import List
from django.core.exceptions import ObjectDoesNotExist
from carrot.models import ScheduledTask

        self.id = scheduled_task.pk
        self.queue = scheduled_task.routing_key
        self.scheduled_task = scheduled_task
        self.run_now = run_now
        self.active = True
        self.filters = filters
        self.inactive_reason = ''

    def run(self) -> None:
        """
        Initiates a timer, then once the timer is equal to the ScheduledTask's interval, the scheduler
        checks to make sure that the task has not been deactivated/deleted in the mean time, and that the manager
        has not been stopped, then publishes it to  the queue
        """
        interval = self.scheduled_task.multiplier * self.scheduled_task.interval_count

        count = 0
        if self.run_now:
            self.scheduled_task.publish()

        while True:
            while count < interval:
                if not self.active:
                    if self.inactive_reason:
                        print('Thread stop has been requested because of the following reason: %s.\n Stopping the '
                              'thread' % self.inactive_reason)

                    return

                try:

                    self.scheduled_task = ScheduledTask.objects.get(pk=self.scheduled_task.pk, **self.filters)