示例#1
0
 def enqueue(self, task: Task):
     # want to track more data so the task knows what endpoint queue it is living in
     # as well as whether it is currently running.  This is to distinguish the queued
     # from the running TaskState
     task.endpoint = self.endpoint
     task.status = TaskState.WAITING_FOR_EP
     self.redis_client.rpush(self.queue_name, task.task_id)
示例#2
0
def add_task():
    session_token = request.cookies.get("session_token")
    user = db.query(User).filter_by(session_token=session_token).first()
    img = db.query(Image).filter_by(author_id=user.id).first()
    tasks = db.query(Task).filter_by(author_id=user.id).all()

    if not user:
        return render_template("index.html")

    if request.method == "GET":
        return render_template("addtask.html", img=img, user=user)

    if request.method == "POST":

        text = request.form.get("text")
        notification = len(tasks)
        full_date = request.form.get("date")

        datee = datetime.strptime(full_date, "%Y-%m-%d")

        task_date = str(datee.day) + "." + " " + str(datee.month) + "."

        name_day = date.weekday(datetime.strptime(full_date, "%Y-%m-%d"))
        day = dayNameFromWeekday(name_day)

        Task.create(text=text,
                    author=user,
                    day=day,
                    task_date=task_date,
                    full_date=full_date)

    return redirect(url_for('tasks', notification=notification))
示例#3
0
def status_and_result(user_name, task_id):
    """Check the status of a task.  Return result if available.

    If the query param deserialize=True is passed, then we deserialize the result object.

    Parameters
    ----------
    user_name : str
        The primary identity of the user
    task_id : str
        The task uuid to look up

    Returns
    -------
    json
        The status of the task
    """
    rc = get_redis_client()

    if not Task.exists(rc, task_id):
        abort(400, "task_id not found")

    task = Task.from_id(rc, task_id)
    task_status = task.status
    task_result = task.result
    task_exception = task.exception
    task_completion_t = task.completion_time
    if task_result or task_exception:
        task.delete()

    deserialize = request.args.get("deserialize", False)
    if deserialize and task_result:
        task_result = deserialize_result(task_result)

    # TODO: change client to have better naming conventions
    # these fields like 'status' should be changed to 'task_status', because 'status' is normally
    # used for HTTP codes.
    response = {
        'task_id': task_id,
        'status': task_status,
        'result': task_result,
        'completion_t': task_completion_t,
        'exception': task_exception
    }

    # Note: this is for backwards compat, when we can't include a None result and have a
    # non-complete status, we must forgo the result field if task not complete.
    if task_result is None:
        del response['result']

    if task_exception is None:
        del response['exception']

    return jsonify(response)
示例#4
0
    def dequeue(self, timeout=1) -> Task:
        """Blocking dequeue for timeout in seconds"""
        # blpop returns a tuple of the list name (in case of popping across > 1 list) and the element
        res = self.redis_client.blpop(self.queue_name, timeout=timeout)
        if not res:
            raise queue.Empty

        # we only query 1 list, so we can ignore the list name
        _, task_id = res
        return Task.from_id(self.redis_client, task_id)
示例#5
0
def tasks(user):
    tasks = []
    sql = "SELECT * FROM tasks WHERE user_id = %s"
    values = [user.id]
    results = run_sql(sql, values)

    for row in results:
        task = Task(row['description'], user, row['duration'], row['completed'], row['id'])
        tasks.append(task)
    return tasks
示例#6
0
def select_all():
    tasks = []
    sql = "SELECT * FROM tasks"
    results = run_sql(sql)
    for row in results:
        user = user_repository.select(row['user_id'])
        task = Task(row['description'], user, row['duration'],
                    row['completed'], row['id'])
        tasks.append(task)
    return tasks
示例#7
0
def initiate_new_task(
    title: str,
    coords: tp.Tuple[float, float],
    description: str,
    correct_answer: str,
    hints: tp.List[str],
) -> Task:
    """Creates new task."""

    task = Task(
        title=title,
        coords=coords,
        description=description,
        correct_answer=correct_answer,
        hints=hints,
    )

    task.save()
    return task
示例#8
0
def select(id):
    task = None
    sql = "SELECT * FROM tasks WHERE id = %s"
    values = [id]
    result = run_sql(sql, values)[0]
    if result is not None:
        user = user_repository.select(result['user_id'])
        task = Task(result['description'], user, result['duration'],
                    result['completed'], result['id'])
    return task
示例#9
0
 def insert_id():
     """
     Insert the job id to  tasks table
     :return: task id
     """
     task_id = uuid.uuid4().hex
     db.session.add(
         Task(task_id=task_id,
              task_status=constants.INPROGRESS,
              task_error=""))
     db.session.commit()
     return task_id
示例#10
0
def create_task():
    json = request.get_json(force=True)

    error =  params_task_schema.validate(json)
    if error:
        return bad_request(error['description'])

    task = Task.new(json['title'], json['description'])
    if task.save():
        return response(task_schema.dump(task))
    
    return bad_request()
示例#11
0
def status(user_name, task_id):
    """Check the status of a task.

    Parameters
    ----------
    user_name
    task_id

    Returns
    -------
    json
        'status' : task status
    """
    rc = get_redis_client()

    if not Task.exists(rc, task_id):
        abort(400, "task_id not found")
    task = Task.from_id(rc, task_id)

    return jsonify({
        'status': task.status
    })
示例#12
0
def get_tasks_from_redis(task_ids):
    all_tasks = {}

    rc = get_redis_client()
    for task_id in task_ids:
        # Get the task from redis
        if not Task.exists(rc, task_id):
            all_tasks[task_id] = {
                'status': 'failed',
                'reason': 'unknown task id'
            }
            continue

        task = Task.from_id(rc, task_id)
        task_status = task.status
        task_result = task.result
        task_exception = task.exception
        task_completion_t = task.completion_time
        if task_result or task_exception:
            task.delete()

        all_tasks[task_id] = {
            'task_id': task_id,
            'status': task_status,
            'result': task_result,
            'completion_t': task_completion_t,
            'exception': task_exception
        }

        # Note: this is for backwards compat, when we can't include a None result and have a
        # non-complete status, we must forgo the result field if task not complete.
        if task_result is None:
            del all_tasks[task_id]['result']

        # Note: this is for backwards compat, when we can't include a None result and have a
        # non-complete status, we must forgo the result field if task not complete.
        if task_exception is None:
            del all_tasks[task_id]['exception']
    return all_tasks
示例#13
0
    def create(self, data: TaskCreationSchema, user_id: int) -> Task:
        """Task creation method.

        Args:
            data (TaskCreationSchema): Data for a new task.
            user_id (int): User id.

        Returns:
            Created task.
        """
        task = Task(**data.dict(), user_id=user_id)
        self.session.add(task)
        self.session.commit()
        return task
示例#14
0
def get_task_result(task_id, delete=True):
    """Check the status of a task. Return result if available.

    If the query param deserialize=True is passed, then we deserialize the result object.

    Parameters
    ----------
    task_id : str
        The task uuid to look up
    delete : bool
        Whether or not to remove the task from the database

    Returns
    -------
    json
        The task as a dict
    """
    rc = get_redis_client()

    if not Task.exists(rc, task_id):
        abort(400, "task_id not found")

    task_dict = {}

    task = Task.from_id(rc, task_id)
    task_dict['status'] = convert_automate_status(task.status)
    task_dict['result'] = task.result
    task_dict['exception'] = task.exception
    task_dict['completion_t'] = task.completion_time
    if (task_dict['result'] or task_dict['exception']) and delete:
        task.delete()

    if task_dict['result']:
        task_dict['result'] = deserialize_result(task_dict['result'])

    return task_dict
示例#15
0
文件: tasks.py 项目: exams/eagle_eye
    def post(self):
        args = post_parser.parse_args()
        task = Task()
        # task name 根据试卷名称生成
        task.name = args.name
        task.paper_id = args.paper_id
        # 路径根据操作系统路径前缀和试卷id生成
        if platform_os_type.lower().index('windows') >= 0:
            task.path = windows_temp_store_prefix + args.paper_id
        elif platform_os_type.lower().index('linux') >= 0:
            task.path = linux_temp_store_prefix + args.paper_id

        # 将ftp目录拷贝至生成的临时目录

        # 读取新生成的目录下的文件数量
        task.pieces = len(os.listdir(task.path))

        task = task.save()
        # 任务保存完成后,启动线程处理任务
        t = EagleScanThread(task)
        t.start()

        return task
示例#16
0
def auth_and_launch(user_id, function_uuid, endpoints, input_data, app, token, serialize=None):
    """ Here we do basic authz for (user, fn, endpoint(s)) and launch the functions

    Parameters
    ==========

    user_id : str
       user id
    function_uuid : str
       uuid string for functions
    endpoints : [str]
       endpoint_uuid as list
    input_data: [string_buffers]
       input_data as a list in case many function launches are to be made
    app : app object
    token : globus token
    serialize : bool
        Whether or not to serialize the input using the serialization service. This is used
        when the input is not already serialized by the SDK.

    Returns:
       json object
    """
    # Check if the user is allowed to access the function
    if not authorize_function(user_id, function_uuid, token):
        return {'status': 'Failed',
                'reason': f'Unauthorized access to function: {function_uuid}'}

    try:
        fn_code, fn_entry, container_uuid = resolve_function(user_id, function_uuid)
    except Exception as e:
        return {'status': 'Failed',
                'reason': f'Function UUID:{function_uuid} could not be resolved. {e}'}

    # Make sure the user is allowed to use the function on this endpoint
    for ep in endpoints:
        if not authorize_endpoint(user_id, ep, function_uuid, token):
            return {'status': 'Failed',
                    'reason': f'Unauthorized access to endpoint: {ep}'}

    app.logger.debug(f"Got function container_uuid :{container_uuid}")

    # We should replace this with container_hdr = ";ctnr={container_uuid}"
    if not container_uuid:
        container_uuid = 'RAW'

    # We should replace this with serialize_hdr = ";srlz={container_uuid}"
    # TODO: this is deprecated.
    serializer = "ANY"

    # TODO: Store redis connections in g
    rc = get_redis_client()

    if isinstance(input_data, list):
        input_data_items = input_data
    else:
        input_data_items = [input_data]

    task_ids = []

    db_logger = get_db_logger()
    ep_queue = {}
    for ep in endpoints:
        redis_task_queue = EndpointQueue(
            ep,
            hostname=app.config['REDIS_HOST'],
            port=app.config['REDIS_PORT']
        )
        redis_task_queue.connect()
        ep_queue[ep] = redis_task_queue

    for input_data in input_data_items:
        if serialize:
            res = serialize_inputs(input_data)
            if res:
                input_data = res

        # At this point the packed function body and the args are concatable strings
        payload = fn_code + input_data
        task_id = str(uuid.uuid4())
        task = Task(rc, task_id, container_uuid, serializer, payload)

        for ep in endpoints:
            ep_queue[ep].enqueue(task)
            app.logger.debug(f"Task:{task_id} placed on queue for endpoint:{ep}")

            # TODO: creating these connections each will be slow.
            # increment the counter
            rc.incr('funcx_invocation_counter')
            # add an invocation to the database
            # log_invocation(user_id, task_id, function_uuid, ep)
            db_logger.log(user_id, task_id, function_uuid, ep, deferred=True)

        task_ids.append(task_id)
    db_logger.commit()

    return {'status': 'Success',
            'task_uuids': task_ids}
示例#17
0
# task_1 = Task("Walk Dog", "Jack Jarvis", 60)
# print(task_1.__dict__)
# task_repository.save(task_1)

# task_2 = Task("Feed Cat", "Victor McDade", 5)
# task_repository.save(task_2)

# # res = task_repository.select_all()
# # for task in res:
# #     print(task.__dict__)

# found_task = task_repository.select(1).__dict__

# task_1.mark_complete()
# task_repository.update()
task_repository.delete_all()
user_repository.delete_all()

user_2 = User('Halle', 'Berry')
user_repository.save(user_2)
user_1 = User('Maddie', 'Wood')
user_repository.save(user_1)
user_repository.select_all()
user_repository.update(user_2)
user_repository.select_all()

task = Task("Walk Dog", user_2, 10)
task_repository.save(task)

pdb.set_trace()
示例#18
0
def get_tasks():
    page = int(request.args.get('page', 1))
    tasks = Task.get_by_page(page)
    return response(tasks_schema.dump(tasks))