예제 #1
0
def stop_task():
    """
    关闭任务,关闭任务之后将用户任务信息进行持久化到数据库,包括结束任务时间
    :return: 
    """
    post_data = request.get_json(force=True)
    if has_dict_value_blank(post_data, ["task_id"]):
        return jsonify(status=400,
                       message="结束任务失败",
                       data={"extra_info": "task_id缺失,无法结束任务"})
    try:
        post_task_id = int(post_data.get("task_id"))
        TaskService.update(fields=({
            Task.task_status: TaskStatus.KILLED
        }),
                           where=(Task.id == post_task_id))
        RedisService.stop_task(post_task_id)
        RedisService.clean_urlclassifications(post_task_id)
        return jsonify(status=200,
                       message="结束任务成功",
                       data={"extra_info": "该任务由管理员关闭"})
    except Exception as e:
        logger.exception("stop_task exception")
        return jsonify(status=500,
                       message="未知异常",
                       data={"extra_info": "创建任务时出现未知异常,请联系管理员查看异常日志"})
예제 #2
0
def stop_task():
    """
    关闭任务,关闭任务之后将用户任务信息进行持久化到数据库,包括结束任务时间
    
    :return: 
    """
    post_data = request.get_json(force=True)
    if json_utils.has_dict_value_blank(post_data, ["task_id"]):
        return jsonify(status=400,
                       message="结束任务失败",
                       data={"extra_info": "task_id缺失,无法结束任务"})

    post_task_id = int(post_data.get("task_id"))
    current_user_name = session["user_name"]
    try:
        user = RedisService.get_user(current_user_name)
        if UserTaskService.count(
                where=(UserTask.user_id == user["id"],
                       UserTask.task_id == post_task_id)) == 0:
            return jsonify(status=403,
                           message="结束任务失败",
                           data={"extra_info": "请勿尝试非法关闭非自己权限任务"})
        task = TaskService.get_fields_by_where(
            fields=(Task.task_status), where=(Task.id == post_task_id))[0]
        if task.task_status in [TaskStatus.DONE, TaskStatus.KILLED]:
            return jsonify(status=200,
                           message="结束任务成功",
                           data={
                               "fullname": user["full_name"],
                               "extra_info": "该任务早已经结束,请登录后台查看扫描结果",
                               "stop_time": get_current_time()
                           })
        TaskService.update(fields=({
            Task.task_status: TaskStatus.KILLED,
            Task.killed_time: datetime.datetime.now()
        }),
                           where=(Task.id == post_task_id))
        UserService.update(fields=({
            User.recent_operation_time:
            datetime.datetime.now()
        }),
                           where=(User.user_name == current_user_name))
        scan_celery.delay(post_data, post_task_id, current_user_name,
                          TaskStatus.KILLED)
        RedisService.stop_task(post_task_id)
        RedisService.clean_urlclassifications(post_task_id)
        return jsonify(status=200,
                       message="结束任务成功",
                       data={
                           "full_name":
                           user["full_name"],
                           "extra_info":
                           "请登录后台查看扫描结果",
                           "stop_time":
                           datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
                       })
    except Exception as e:
        logger.exception("stop_task exception")
        if isinstance(e, UserNotFoundInRedisException):
            return jsonify(status=403,
                           message="结束任务失败",
                           data={
                               "extra_info": "认证失败,请重新登录进行授权",
                               "auth_site": ""
                           })
        return jsonify(status=500,
                       message="未知异常",
                       data={"extra_info": "创建任务时出现未知异常,请联系管理员查看异常日志"})