Example #1
0
    def test_str(self):
        periodic = PeriodicTask(task="foo")
        periodic.interval = PeriodicTask.Interval(every=1, period="days")
        self.assertEqual("every day", str(periodic.interval))

        periodic.interval = PeriodicTask.Interval(every=2, period="days")
        self.assertEqual('every 2 days', str(periodic.interval))
    def test_get_from_database(self):
        from celerybeatmongo.models import PeriodicTask
        PeriodicTask.objects.create(name="a1", task="foo", enabled=True, interval=PeriodicTask.Interval(every=1, period="days"))
        PeriodicTask.objects.create(name="b1", task="foo", enabled=True, interval=PeriodicTask.Interval(every=2, period="days"))
        PeriodicTask.objects.create(name="c2", task="foo", enabled=False, interval=PeriodicTask.Interval(every=3, period="days"))

        scheduler = MongoScheduler(app=self.app)
        self.assertEqual(2, len(scheduler.get_from_database())
                         , "get_from_database should return just enabled tasks")
Example #3
0
    def create_periodic_task(cls, name, task, every,
                             period='seconds', args=[], kwargs={}):

        if cls.CELERY_BEAT_SCHEDULER == 'MONGODB':
            from celerybeatmongo.models import PeriodicTask
            PeriodicTask(
                name=name,
                task=task,
                enabled=True,
                args=args,
                kwargs=kwargs,
                interval=PeriodicTask.Interval(every=every, period=period),
            ).save()
        elif cls.CELERY_BEAT_SCHEDULER == 'REDIS':
            from celery.schedules import schedule
            from redbeat.schedulers import RedBeatSchedulerEntry
            if period != 'seconds':

                # do conversion... run_every should be a datetime.timedelta
                log.error("Unsupported period {} for redis beat", period)

            interval = schedule(run_every=every)  # seconds
            entry = RedBeatSchedulerEntry(
                name,
                task,
                interval,
                args=args,
                app=CeleryExt.celery_app
            )
            entry.save()

        else:
            log.error(
                "Unsupported celery-beat scheduler: {}", cls.CELERY_BEAT_SCHEDULER)
Example #4
0
 def test_cannot_define_both_interval_and_contrab(self):
     periodic = PeriodicTask(task="foo")
     periodic.interval = PeriodicTask.Interval(every=1, period="days")
     periodic.crontab = PeriodicTask.Crontab(minute="0",
                                             hour="*",
                                             day_of_week="*",
                                             day_of_month="10-15",
                                             month_of_year="*")
     with self.assertRaises(ValidationError) as err:
         periodic.save()
     self.assertTrue("Cannot define both interval and crontab schedule." in
                     err.exception.message)
def task_interval_add_one():
    if request.method == "POST":
        task = PeriodicTask(
            name=str(uuid1()),
            task=request.json["task"],
            enabled=True,
            run_immediately=True,
            interval=PeriodicTask.Interval(every=int(request.json["every"]),
                                           period="seconds"),
        )
        result = task.save()
        return result.to_json(), {"Content-Type": "application/json"}
Example #6
0
    def test_date_changed(self):
        periodic = PeriodicTask(task="foo")
        periodic.interval = PeriodicTask.Interval(every=1, period="days")

        self.assertIsNone(periodic.date_changed)
        periodic.save()
        self.assertIsNotNone(periodic.date_changed)

        date_changed = periodic.date_changed
        periodic.name = "I'm changing now"
        periodic.save()
        self.assertGreater(periodic.date_changed, date_changed)
Example #7
0
    def create_periodic_task(
        cls,
        name: str,
        task: str,
        every: Union[str, int, timedelta],
        period: AllowedTimedeltaPeriods = "seconds",
        args: List[Any] = None,
        kwargs: Dict[str, Any] = None,
    ) -> None:
        if args is None:
            args = []
        if kwargs is None:
            kwargs = {}

        if cls.CELERYBEAT_SCHEDULER == "MONGODB":
            from celerybeatmongo.models import PeriodicTask

            PeriodicTask(
                name=name,
                task=task,
                enabled=True,
                args=args,
                kwargs=kwargs,
                interval=PeriodicTask.Interval(every=every, period=period),
            ).save()
        elif cls.CELERYBEAT_SCHEDULER == "REDIS":
            from celery.schedules import schedule
            from redbeat.schedulers import RedBeatSchedulerEntry

            # convert strings and integers to timedeltas
            if isinstance(every, str) and every.isdigit():
                every = get_timedelta(int(every), period)
            elif isinstance(every, int):
                every = get_timedelta(every, period)

            if not isinstance(every, timedelta):
                t = type(every).__name__
                raise AttributeError(
                    f"Invalid input parameter every = {every} (type {t})")
            interval = schedule(run_every=every)
            entry = RedBeatSchedulerEntry(name,
                                          task,
                                          interval,
                                          args=args,
                                          app=CeleryExt.celery_app)
            entry.save()

        else:
            raise AttributeError(
                f"Unsupported celery-beat scheduler: {cls.CELERYBEAT_SCHEDULER}"
            )
Example #8
0
    def test_creation_date(self):
        periodic = PeriodicTask(task="foo")
        periodic.interval = PeriodicTask.Interval(every=1, period="days")
        self.assertIsNone(
            periodic.date_creation,
            "date_creation should be none on the first object instantion")
        periodic.save()
        self.assertIsNotNone(periodic.date_creation)
        date_creation = periodic.date_creation

        periodic.name = "I'm changing"
        periodic.save()
        self.assertEqual(
            date_creation, periodic.date_creation,
            "Update object should not change date_creation value")
Example #9
0
 def test_cannot_save_interval_schduler_with_a_invalid_period(self):
     periodic = PeriodicTask(task="foo")
     with self.assertRaises(ValidationError):
         periodic.interval = PeriodicTask.Interval(every=1,
                                                   period="days111")
         periodic.save()
Example #10
0
 def test_scheduler(self):
     periodic = PeriodicTask(task="foo")
     periodic.interval = PeriodicTask.Interval(every=1, period="days")
     periodic.save()
     self.assertIsNotNone(periodic.schedule)
    def manage_task(self,
                    run_immediately_if_new: bool = False,
                    **task_data) -> None:
        periodic = PeriodicTask.objects(name=task_data["name"])
        if periodic:
            logger.debug("Existing Schedule")
            isChanged = False
            periodic_document = periodic.get(name=task_data["name"])
            for key, value in task_data.items():
                if key == "interval":
                    if not periodic_document.interval == PeriodicTask.Interval(
                            **task_data["interval"]):
                        periodic_document.interval = PeriodicTask.Interval(
                            **task_data["interval"])
                        isChanged = True
                elif key == "crontab":
                    if not periodic_document.crontab == PeriodicTask.Crontab(
                            **task_data["crontab"]):
                        periodic_document.crontab = PeriodicTask.Crontab(
                            **task_data["crontab"])
                    isChanged = True
                elif key == "target":
                    pass
                elif key == "total_run_count":
                    periodic_document[key] = task_data[key]
                else:
                    if key in periodic_document:
                        if not periodic_document[key] == task_data[key]:
                            periodic_document[key] = task_data[key]
                            isChanged = True
                    else:
                        periodic_document[key] = task_data[key]
                        isChanged = True

        else:
            logger.debug("New Schedule")
            isChanged = True
            periodic_document = PeriodicTask(task=task_data["task"])
            periodic_document.name = task_data["name"]
            periodic_document.args = task_data["args"]
            periodic_document.kwargs = task_data["kwargs"]
            if "interval" in task_data:
                periodic_document.interval = PeriodicTask.Interval(
                    **task_data["interval"])
            else:
                periodic_document.crontab = PeriodicTask.Crontab(
                    **task_data["crontab"])
            periodic_document.enabled = task_data["enabled"]
            periodic_document.run_immediately = task_data.get(
                "run_immediately", run_immediately_if_new)
            if "total_run_count" in task_data:
                periodic_document["total_run_count"] = task_data[
                    "total_run_count"]
            if "target" in task_data:
                periodic_document["target"] = task_data["target"]
            if "options" in task_data:
                periodic_document["options"] = task_data["options"]

        logger.info(
            f"Periodic document to save: {periodic_document.to_json()}")
        if isChanged:
            periodic_document.save()