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))
Example #2
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)
Example #3
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 #4
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 #5
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 #6
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()