def task_crontab_add_one(): if request.method == "POST": if "app.mod_task.tasks.send_email_of_logs" in request.json["task"]: task = PeriodicTask( name=str(uuid1()), task=request.json["task"].split(":")[0], enabled=True, crontab=PeriodicTask.Crontab( minute=request.json["minute"], hour=request.json["hour"], day_of_month=request.json["day_of_month"], month_of_year=request.json["month_of_year"], day_of_week=request.json["day_of_week"], ), args=[request.json["task"].split(":")[1]], ) result = task.save() return result.to_json(), {"Content-Type": "application/json"} task = PeriodicTask( name=str(uuid1()), task=request.json["task"], enabled=True, crontab=PeriodicTask.Crontab( minute=request.json["minute"], hour=request.json["hour"], day_of_month=request.json["day_of_month"], month_of_year=request.json["month_of_year"], day_of_week=request.json["day_of_week"], ), ) result = task.save() return result.to_json(), {"Content-Type": "application/json"}
def test_str(self): periodic = PeriodicTask(task="foo") periodic.crontab = PeriodicTask.Crontab(minute="0", hour="*", day_of_week="*", day_of_month="10-15", month_of_year="*") self.assertEqual("0 * * 10-15 * (m/h/d/dM/MY)", str(periodic.crontab))
def create_crontab_task( cls, name: str, task: str, minute: str, hour: str, day_of_week: str = "*", day_of_month: str = "*", month_of_year: str = "*", 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, crontab=PeriodicTask.Crontab( minute=minute, hour=hour, day_of_week=day_of_week, day_of_month=day_of_month, month_of_year=month_of_year, ), ).save() elif cls.CELERYBEAT_SCHEDULER == "REDIS": from celery.schedules import crontab from redbeat.schedulers import RedBeatSchedulerEntry interval = crontab( minute=minute, hour=hour, day_of_week=day_of_week, day_of_month=day_of_month, month_of_year=month_of_year, ) entry = RedBeatSchedulerEntry(name, task, interval, args=args, app=CeleryExt.celery_app) entry.save() else: raise AttributeError( f"Unsupported celery-beat scheduler: {cls.CELERYBEAT_SCHEDULER}" )
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 create_crontab_task( cls, name, task, minute, hour, day_of_week="*", day_of_month="*", month_of_year="*", args=[], kwargs={}, ): if cls.CELERY_BEAT_SCHEDULER == 'MONGODB': from celerybeatmongo.models import PeriodicTask PeriodicTask( name=name, task=task, enabled=True, args=args, kwargs=kwargs, crontab=PeriodicTask.Crontab( minute=minute, hour=hour, day_of_week=day_of_week, day_of_month=day_of_month, month_of_year=month_of_year, ), ).save() elif cls.CELERY_BEAT_SCHEDULER == 'REDIS': from celery.schedules import crontab from redbeat.schedulers import RedBeatSchedulerEntry interval = crontab( minute=minute, hour=hour, day_of_week=day_of_week, day_of_month=day_of_month, month_of_year=month_of_year ) entry = RedBeatSchedulerEntry( name, task, interval, args=args, app=CeleryExt.celery_app ) entry.save() else: log.error( "Unsupported celery-beat scheduler: {}", cls.CELERY_BEAT_SCHEDULER)
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()