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)
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")
def insert(self, task_name, task, args, cron, offset): ''' insert a periodic task into database params: task_name : string task: a registered task in celery workers args: task arguments cron: crontab object offset: UTC offset ''' task = str(task) task_name = str(task_name) # the celery scheduler runs on UTC time, add offset so that it # emulates the user timezone cron.hour = set(map(lambda x: (x + (offset)) % 24, cron.hour)) # if the integer overflows should a day advance? or will the overflow # only when UTC time is > 12 therefore the next time a time < 12 # occurs is on the next day? cronArr = [cron.minute, cron.hour, cron.day_of_week, cron. day_of_month, cron.month_of_year] # cron.minute return {number}, change to 'number' for celery schedule for i in range(len(cronArr)): cronArr[i] = str(cronArr[i]).replace('{', '') cronArr[i] = cronArr[i].replace('}', '') # following mongocelerybeat document schema for schedules addTask = PeriodicTask() addTask.name = task_name addTask.task = task addTask.crontab = addTask.Crontab(minute=cronArr[0], hour=cronArr[1], day_of_week=cronArr[2], day_of_month=cronArr[3], month_of_year=cronArr[4]) addTask.enabled = True addTask.args = args addTask.save()
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()