Пример #1
0
def fetch_lock(task_id):
    """Fetch the time (in seconds) until the current user's
    lock on a task expires.
    """
    if not current_user.is_authenticated:
        return abort(401)

    task = task_repo.get_task(task_id)

    if not task:
        return abort(400)

    scheduler, timeout = get_project_scheduler_and_timeout(task.project_id)

    ttl = None
    if scheduler in (Schedulers.locked, Schedulers.user_pref):
        task_locked_by_user = has_lock(task.id, current_user.id, timeout)
        if task_locked_by_user:
            locks = get_locks(task.id, timeout)
            ttl = locks.get(str(current_user.id))

    if not ttl:
        return abort(404)

    seconds_to_expire = float(ttl) - time()
    res = json.dumps({'success': True, 'expires': seconds_to_expire})

    return Response(res, 200, mimetype='application/json')
Пример #2
0
 def test_acquire_lock_no_pipeline(self):
     task_id = 1
     user_id = 1
     limit = 1
     timeout = 100
     acquire_lock(task_id, user_id, limit, timeout)
     assert has_lock(task_id, user_id, limit)
Пример #3
0
def get_service_request(task_id, service_name, major_version, minor_version):
    """Proxy service call"""
    proxy_service_config = current_app.config.get('PROXY_SERVICE_CONFIG', None)
    task = task_repo.get_task(task_id)
    project = project_repo.get(task.project_id)

    if not (task and proxy_service_config and service_name and major_version
            and minor_version):
        return abort(400)

    timeout = project.info.get('timeout', ContributionsGuard.STAMP_TTL)
    task_locked_by_user = has_lock(task.id, current_user.id, timeout)
    payload = request.json if isinstance(request.json, dict) else None

    if payload and task_locked_by_user:
        service = _get_valid_service(task_id, service_name, payload,
                                     proxy_service_config)
        if isinstance(service, dict):
            url = '{}/{}/{}/{}'.format(proxy_service_config['uri'],
                                       service_name, major_version,
                                       minor_version)
            headers = service.get('headers')
            ret = requests.post(url, headers=headers, json=payload['data'])
            return Response(ret.content, 200, mimetype="application/json")

    current_app.logger.info(
        'Task id {} with lock-status {} by user {} with this payload {} failed.'
        .format(task_id, task_locked_by_user, current_user.id, payload))
    return abort(403)
Пример #4
0
def cancel_task(task_id=None):
    """Unlock task upon cancel so that same task can be presented again."""
    if not current_user.is_authenticated:
        return abort(401)

    data = request.json
    projectname = data.get('projectname', None)
    project = project_repo.get_by_shortname(projectname)
    if not project:
        return abort(400)

    user_id = current_user.id
    scheduler, timeout = get_scheduler_and_timeout(project)
    if scheduler in (Schedulers.locked, Schedulers.user_pref,
                     Schedulers.task_queue):
        task_locked_by_user = has_lock(task_id, user_id, timeout)
        if task_locked_by_user:
            release_lock(task_id, user_id, timeout)
            current_app.logger.info(
                'Project {} - user {} cancelled task {}'.format(
                    project.id, current_user.id, task_id))
            release_reserve_task_lock_by_id(project.id,
                                            task_id,
                                            current_user.id,
                                            timeout,
                                            expiry=EXPIRE_LOCK_DELAY)

    return Response(json.dumps({'success': True}),
                    200,
                    mimetype="application/json")
Пример #5
0
def validate_task(project, task_id, user_id):
    """Confirm task payload is valid and user is authorized to access task."""
    task = task_repo.get_task(task_id)

    if not task or task.project_id != project['id']:
        raise BadRequest('Task does not exist')

    if current_user.admin:
        return True

    if has_lock(task_id, user_id,
                project['info'].get('timeout', ContributionsGuard.STAMP_TTL)):
        return True

    if user_id in project['owners_ids']:
        return True

    raise Forbidden('FORBIDDEN')
Пример #6
0
def check_allowed(user_id, task_id, project, file_url):
    task = task_repo.get_task(task_id)

    if not task or task.project_id != project['id']:
        raise BadRequest('Task does not exist')

    if file_url not in task.info.values():
        raise Forbidden('Invalid task content')

    if current_user.admin:
        return True

    if has_lock(task_id, user_id,
                project['info'].get('timeout', ContributionsGuard.STAMP_TTL)):
        return True

    if user_id in project['owners_ids']:
        return True

    raise Forbidden('FORBIDDEN')
Пример #7
0
    def test_user_01_release_tasks(self):
        """ Test SCHED newtask returns a Task for John Doe User"""
        user = UserFactory.create(id=500)
        project = ProjectFactory.create(owner=user, info={'sched': 'default'})
        TaskFactory.create_batch(2, project=project, n_answers=2)
        self.signin_user(user)
        url = 'api/project/%s/newtask' % project.id
        self.set_proj_passwd_cookie(project, user=user)
        res = self.app.get(url)
        data = json.loads(res.data)
        task_id = data['id']
        print data
        assert task_id, data
        assert has_lock(task_id, user.id, TIMEOUT)

        released_task_ids = release_user_locks_for_project(user.id, project.id)
        # TODO: Figure out how to test this
        # assert released_task_ids == [task_id]
        # assert not has_lock(task_id, user.id, TIMEOUT)
        self.signout()