def create_berry(name, typ, time_mode=BerryModel.TIME_MODE_NOW, dt_start=None, input_params={}, operator=None): ''' 创建任务 params: dict ''' query = { 'sign': typ, } typ_obj = BerryTypeModel.objects.filter(**query).first() if not typ_obj: raise errors.CommonError('任务类型不存在') if not BerryModel.check_choices('time_mode', time_mode): raise errors.CommonError('时间模式值不正确') now = time_utils.now() countdown = 0 if time_mode == BerryModel.TIME_MODE_CRONTAB: if not dt_start: raise errors.CommonError('定时任务必须传开始时间') if dt_start <= now: raise errors.CommonError('不能指定过去的时间') countdown = dt_start - now countdown = int(countdown.total_seconds()) if countdown < 5 * 60: raise errors.CommonError('定时任务必须指定五分钟以后的时间') else: dt_start = time_utils.now() user_id = None if operator: user_id = operator.id data = { 'name': name, 'typ_id': typ_obj.id, 'time_mode': time_mode, 'dt_start': dt_start, 'input_params': json.dumps(input_params), 'user_id': user_id, } berry_obj = base_ctl.create_obj(BerryModel, data) result = berry_tasks.apply_task.apply_async(countdown=countdown, args=[berry_obj.id]) data = { 'task_id': result.task_id, } base_ctl.update_obj(BerryModel, berry_obj.id, data)
def apply_task(berry_id): try: berry_obj = base_ctl.get_obj(BerryModel, berry_id) # 一进来就更改任务状态到进行中 data = { 'status': BerryModel.ST_RUNNING, } base_ctl.update_obj(BerryModel, berry_id, data) sync_list = [ 'sync_ecs', 'sync_slb', 'sync_rds', 'sync_redis', 'sync_mongo', 'sync_rocket', 'sync_domain', 'sync_gitlab', 'sync_jenkins', 'sync_ldap_user' ] # 如果是同步任务,则走此处理方式 if berry_obj.typ.sign in sync_list: sync_task_route(berry_obj) except Exception as e: # 如果出现异常,则更改任务状态,并且记录错误日志 log = traceback.format_exc() dt_end = time_utils.now() duration = dt_end - berry_obj.dt_start duration = int(duration.total_seconds()) data = { 'status': BerryModel.ST_FAILURE, 'error_log': log, 'dt_end': dt_end, 'duration': duration, } base_ctl.update_obj(BerryModel, berry_id, data)
def prepare_predict_X(cls, car_deal, mean_value_dict=None): car_model = car_type_service.get_car_model_by_car_deal(car_deal) car_model['guide_price'] = cls.process_guide_price(car_model['guide_price']) if len(car_model) == 0: return None feature_values = [ car_deal.card_time, time_utils.now(), car_deal.kilometer ] if mean_value_dict is None: feature_values.append(car_model['guide_price']) return [feature_values] for key in car_type_service.CAR_MODEL_FIELDS_SEQUENCE: value = -1 if key in car_model and car_model[key] is not None: float_value = -1 try: float_value = float(car_model[key]) except ValueError: float_value = mean_value_dict[key] if float_value > 0: value = float_value feature_values.append(value) return [feature_values]
def get_reviews_count_of_n_days_before(cls, session, user_id, checklist_ids, days_before=30): """ 获取 checklist_ids 过去 days_before 天的打卡情况 :param session: :param user_id: :param checklist_ids: :param days_before: 多少天前 :return: """ end_time = time_utils.now() start_time = time_utils.add_days(end_time, -days_before) res_raw = session.query( ChecklistReview.checklist_id, func.count().label("punch_count")).filter( cls.user_id == user_id, cls.created_at.between(start_time, end_time), cls.checklist_id.in_(checklist_ids)).group_by( ChecklistReview.checklist_id).all() checklist_count_map = dict() for res in res_raw: checklist_count_map[res.checklist_id] = res.punch_count return checklist_count_map
def delete_obj(obj_model, obj_id, operator=None): ''' 删除对象 ''' obj = get_obj(obj_model, obj_id) obj.is_deleted = True obj.dt_update = time_utils.now() obj.save() return obj
def validate_and_get_car_deal(uwsgi_env): query_dict = string_utils.parse_url_query_to_dict(uwsgi_env) # check whether required key is disappeared. required_keys = ("series_id", "sale_name", "car_time", "kilometer", "province_id") for required_key in required_keys: if required_key not in query_dict: return None, {"err_msg": "Invalid argument: " + required_key} if int(query_dict["car_time"]) > time_utils.now(): return None, {"err_msg": "Invalid argument: car_time"} car_deal = CarDeal(query_dict) return car_deal, None
def delete_objs(obj_model, obj_ids, operator=None): ''' 批量删除对象 ''' if not obj_ids: return data = { 'is_deleted': True, 'dt_update': time_utils.now(), } obj_model.objects.filter(id__in=list(obj_ids)).update(**data)
def update_obj(obj_model, obj_id, data, operator=None): ''' 更新对象 ''' obj = get_obj(obj_model, obj_id) change_list = [] for key, value in data.items(): new_value = value old_value = getattr(obj, key) if new_value != old_value: change_data = { 'key': key, 'name': obj_model.field_verbose_name(key), 'old_value': old_value, 'new_value': new_value, } change_list.append(change_data) setattr(obj, key, new_value) if change_list: obj.dt_update = time_utils.now() obj.save() return obj
def sync_task_route(berry_obj): ''' 同步任务 ''' if berry_obj.typ.sign == 'sync_ecs': ecs_sync.sync_ecses() elif berry_obj.typ.sign == 'sync_slb': slb_sync.sync_slbs() elif berry_obj.typ.sign == 'sync_rds': rds_sync.sync_rdses() elif berry_obj.typ.sign == 'sync_redis': redis_sync.sync_redises() elif berry_obj.typ.sign == 'sync_mongo': mongo_sync.sync_mongos() elif berry_obj.typ.sign == 'sync_domain': domain_sync.sync_domains() elif berry_obj.typ.sign == 'sync_rocket': rocket_sync.sync_rockets() elif berry_obj.typ.sign == 'sync_gitlab': gitlab_sync.sync_gitlabs() elif berry_obj.typ.sign == 'sync_jenkins': jenkins_sync.sync_jenkins() elif berry_obj.typ.sign == 'sync_ldap_user': account_sync.sync_ldap_user() else: raise errors.CommonError( f'任务{berry_obj.id}: 不存在的任务类型{berry_obj.typ.sign}') # 执行成功 dt_end = time_utils.now() duration = dt_end - berry_obj.dt_start duration = int(duration.total_seconds()) data = { 'status': BerryModel.ST_SUCCESS, 'dt_end': dt_end, 'duration': duration, } base_ctl.update_obj(BerryModel, berry_obj.id, data)
def update_objs(obj_model, obj_ids, data, operator=None): ''' 批量更新 ''' data['dt_update'] = time_utils.now() obj_model.objects.filter(id__in=list(obj_ids)).update(**data)