Example #1
0
    def template_get_locked_task(project_id,
                                 user_id=None,
                                 user_ip=None,
                                 external_uid=None,
                                 limit=1,
                                 offset=0,
                                 orderby='priority_0',
                                 desc=True,
                                 rand_within_priority=False,
                                 present_gold_task=False,
                                 gold_only=False):
        if offset > 2:
            raise BadRequest('')
        if offset > 0:
            return None
        task_id, lock_seconds = get_task_id_and_duration_for_project_user(
            project_id, user_id)
        if lock_seconds > 10:
            task = session.query(Task).get(task_id)
            if task:
                return [task]
        user_count = get_active_user_count(project_id, sentinel.master)
        current_app.logger.info(
            "Project {} - number of current users: {}".format(
                project_id, user_count))

        sql = query_factory(project_id,
                            user_id=user_id,
                            user_ip=user_ip,
                            external_uid=external_uid,
                            limit=limit,
                            offset=offset,
                            orderby=orderby,
                            desc=desc,
                            rand_within_priority=rand_within_priority,
                            present_gold_task=present_gold_task,
                            gold_only=gold_only)
        rows = session.execute(
            sql,
            dict(project_id=project_id, user_id=user_id, limit=user_count + 5))

        for task_id, taskcount, n_answers, calibration, timeout in rows:
            timeout = timeout or TIMEOUT
            remaining = float('inf') if calibration else n_answers - taskcount
            if acquire_lock(task_id, user_id, remaining, timeout):
                rows.close()
                save_task_id_project_id(task_id, project_id, 2 * timeout)
                register_active_user(project_id,
                                     user_id,
                                     sentinel.master,
                                     ttl=timeout)

                task_type = 'gold task' if calibration else 'task'
                current_app.logger.info(
                    'Project {} - user {} obtained {} {}, timeout: {}'.format(
                        project_id, user_id, task_type, task_id, timeout))
                return [session.query(Task).get(task_id)]

        return []
Example #2
0
def _lock_task_for_user(task_id, project_id, user_id, timeout, calibration=False):
    save_task_id_project_id(task_id, project_id, 2 * timeout)
    register_active_user(project_id, user_id, sentinel.master, ttl=timeout)

    task_type = 'gold task' if calibration else 'task'
    current_app.logger.info(
        'Project {} - user {} obtained {} {}, timeout: {}'
        .format(project_id, user_id, task_type, task_id, timeout))
    return [session.query(Task).get(task_id)]
Example #3
0
    def template_get_locked_task(project_id,
                                 user_id=None,
                                 user_ip=None,
                                 external_uid=None,
                                 limit=1,
                                 offset=0,
                                 orderby='priority_0',
                                 desc=True):
        if offset > 2:
            raise BadRequest()
        if offset == 1:
            return None
        user_count = get_active_user_count(project_id, sentinel.slave)
        current_app.logger.info(
            "Project {} - number of current users: {}".format(
                project_id, user_count))

        sql = query_factory(project_id, user_id, user_ip, external_uid, limit,
                            offset, orderby, desc)

        rows = session.execute(
            sql,
            dict(project_id=project_id, user_id=user_id, limit=user_count + 5))

        for task_id, taskcount, n_answers, timeout in rows:
            timeout = timeout or TIMEOUT
            remaining = n_answers - taskcount
            if acquire_lock(project_id, task_id, user_id, remaining, timeout):
                rows.close()
                register_active_user(project_id,
                                     user_id,
                                     sentinel.master,
                                     ttl=timeout)
                current_app.logger.info(
                    'Project {} - user {} obtained task {}, timeout: {}'.
                    format(project_id, user_id, task_id, timeout))
                return [session.query(Task).get(task_id)]

        return []