def test_CrontabSchedule_unicode(self): assert str(CrontabSchedule( minute=3, hour=3, day_of_week=None, )) == '3 3 * * * (m/h/dM/MY/d) UTC' assert str(CrontabSchedule( minute=3, hour=3, day_of_week='tue', day_of_month='*/2', month_of_year='4,6', )) == '3 3 */2 4,6 tue (m/h/dM/MY/d) UTC'
def test_CrontabSchedule_unicode(self): assert text_t(CrontabSchedule( minute=3, hour=3, day_of_week=None, )) == '3 3 * * * (m/h/d/dM/MY)' assert text_t(CrontabSchedule( minute=3, hour=3, day_of_week='tue', day_of_month='*/2', month_of_year='4,6', )) == '3 3 tue */2 4,6 (m/h/d/dM/MY)'
def _get_schedule(self): schedule = CrontabSchedule.from_schedule( crontab(*self.schedule.split())) schedule.timezone = self.timezone if schedule.pk is None: schedule.save() return schedule
def test_save_raises_for_multiple_schedules(self): schedules = [('crontab', CrontabSchedule()), ('interval', IntervalSchedule()), ('solar', SolarSchedule()), ('clocked', ClockedSchedule())] for i, options in enumerate(combinations(schedules, 2)): with self.assertRaises(ValidationError): PeriodicTask(name='task{}'.format(i), **dict(options)).save()
def test_validate_unique_raises_for_multiple_schedules(self): schedules = [('crontab', CrontabSchedule()), ('interval', IntervalSchedule()), ('solar', SolarSchedule()), ('clocked', ClockedSchedule())] for options in combinations(schedules, 2): with self.assertRaises(ValidationError): PeriodicTask(**dict(options)).validate_unique()
def create(self, validated_data): instance = super(Audit_JobSerializer, self).create(validated_data) datetime = validated_data['plan_time'] is_static_job = validated_data.get('is_static_job', False) schedule = CrontabSchedule.objects.filter((Q(minute=datetime.minute)) & (Q(hour=datetime.hour)) & (Q(day_of_month=datetime.day)) & (Q(month_of_year=datetime.month))) if not schedule: schedule = CrontabSchedule(minute=datetime.minute, hour=datetime.hour, day_of_month=datetime.day, month_of_year=datetime.month) schedule.save() else: schedule = schedule[0] task = PeriodicTask(crontab=schedule, name=str(uuid.uuid4()), task=ANALYSIS_FUNCTION, args=json.dumps([str(instance.id)]), description='audit_job') task.save() instance.task = task instance.save() return instance
def create_sched_obj(**kwargs): """ :param kwargs: :return: """ name = kwargs.get('name', None) sched_type = kwargs.get('type', None) value = kwargs.get('value', None) if not all(( name, value, sched_type, )): raise ParameterIsEmptyException( u'"name, value, sched_type" parameters cannot be empty !') if sched_type: sched_type = int(sched_type) close_old_connections() if sched_type == 1: tri = _get_interval(seconds=value) if not tri: value = int(value) tri = IntervalSchedule(every=value * 60, period='seconds') tri.save() else: tri = _get_crontab(cron_expression=value) if not tri: minute, hour, day_of_week, day_of_month, month_of_year = value.split( ' ') tri = CrontabSchedule( minute=minute, hour=hour, day_of_week=day_of_week, day_of_month=day_of_month, month_of_year=month_of_year, timezone='Asia/Shanghai', ) tri.save() sched = SchedInfo( name=name, type=int(sched_type), ) if sched_type == 1: sched.interval = tri else: sched.crontab = tri sched.save() return sched
def test_CrontabSchedule_schedule(self): s = CrontabSchedule( minute='3, 7', hour='3, 4', day_of_week='*', day_of_month='1, 16', month_of_year='1, 7', ) assert s.schedule.minute == {3, 7} assert s.schedule.hour == {3, 4} assert s.schedule.day_of_week == {0, 1, 2, 3, 4, 5, 6} assert s.schedule.day_of_month == {1, 16} assert s.schedule.month_of_year == {1, 7}
def test_validate_unique_raises_for_multiple_schedules(self): schedules = [('crontab', CrontabSchedule()), ('interval', IntervalSchedule()), ('solar', SolarSchedule()), ('clocked', ClockedSchedule())] expected_error_msg = ( 'Only one of clocked, interval, crontab, or solar ' 'must be set') for i, options in enumerate(combinations(schedules, 2)): name = 'task{}'.format(i) options_dict = dict(options) with self.assertRaises(ValidationError) as cm: PeriodicTask(name=name, **options_dict).validate_unique() errors = cm.exception.args[0] self.assertEqual(errors.keys(), options_dict.keys()) for error_msg in errors.values(): self.assertEqual(error_msg, [expected_error_msg])
def test_CrontabSchedule_long_schedule(self): s = CrontabSchedule(minute=str(list(range(60)))[1:-1], hour=str(list(range(24)))[1:-1], day_of_week=str(list(range(7)))[1:-1], day_of_month=str(list(range(1, 32)))[1:-1], month_of_year=str(list(range(1, 13)))[1:-1]) assert s.schedule.minute == set(range(60)) assert s.schedule.hour == set(range(24)) assert s.schedule.day_of_week == set(range(7)) assert s.schedule.day_of_month == set(range(1, 32)) assert s.schedule.month_of_year == set(range(1, 13)) fields = [ 'minute', 'hour', 'day_of_week', 'day_of_month', 'month_of_year' ] for field in fields: str_length = len(str(getattr(s.schedule, field))) field_length = s._meta.get_field(field).max_length assert str_length <= field_length
def test_validate_unique_not_raises(self): PeriodicTask(crontab=CrontabSchedule()).validate_unique() PeriodicTask(interval=IntervalSchedule()).validate_unique() PeriodicTask(solar=SolarSchedule()).validate_unique()
def create_model_crontab(self, schedule, **kwargs): crontab = CrontabSchedule.from_schedule(schedule) crontab.save() return self.create_model(crontab=crontab, **kwargs)
def test_validate_unique_not_raises(self): PeriodicTask(crontab=CrontabSchedule()).validate_unique() PeriodicTask(interval=IntervalSchedule()).validate_unique() PeriodicTask(solar=SolarSchedule()).validate_unique() PeriodicTask(clocked=ClockedSchedule(), one_off=True).validate_unique()
def _task(): schedule = crontab(minute='*/10') c = CrontabSchedule.from_schedule(schedule) c.save() return PeriodicTask.objects.create(name='t{0}'.format(next(_ids)), crontab=c)
def task_schedule_save(request): try: data = json.loads(request.body) _id = data.get('id') name = data.get('name') crontab = data.get('crontab') task_type = data.get('task_type') task_name = data.get('task_name') # 处理crontab表格式 if len(crontab.strip().split()) != 5: return HttpResponse(json.dumps({ 'code': 1, 'msg': 'crontab格式错误' }), content_type="application/json") else: minute, hour, day_of_week, day_of_month, month_of_year = crontab.strip( ).split() orm_crontab = CrontabSchedule.objects.filter( Q(minute=minute) & Q(hour=hour) & Q(day_of_week=day_of_week) & Q(day_of_month=day_of_month) & Q(month_of_year=month_of_year)) if orm_crontab: crontab_id = orm_crontab[0].id else: orm_crontab = CrontabSchedule(minute=minute, hour=hour, day_of_week=day_of_week, day_of_month=day_of_month, month_of_year=month_of_year, timezone=TIME_ZONE) orm_crontab.save() crontab_id = orm_crontab.id # 处理任务表格式 if task_type == '单任务': task = 'task_schedule.tasks.run_task' elif task_type == '任务组': task = 'task_schedule.tasks.run_tasks' args = f'["{task_name}"]' # 添加和编辑 if not _id: orm = PeriodicTask(name=name, task=task, args=args, crontab_id=crontab_id) else: orm = PeriodicTask.objects.get(id=_id) orm.name = name orm.task = task orm.args = args orm.crontab_id = crontab_id orm.save() return HttpResponse(json.dumps({ 'code': 0, 'msg': '操作成功' }), content_type="application/json") except Exception as e: return HttpResponse(json.dumps({ 'code': 1, 'msg': e }), content_type="application/json")