Пример #1
0
    def test_loops_and_sleeps_waiting_for_available_worker(self):
        self.mock_get_worker_for_reservation.side_effect = NoWorkers()
        self.mock_get_unreserved_worker.side_effect = NoWorkers()

        class BreakOutException(Exception):
            pass

        def side_effect(*args):
            def second_call(*args):
                raise BreakOutException()

            self.mock_time.sleep.side_effect = second_call
            return None

        self.mock_time.sleep.side_effect = side_effect

        try:
            tasks._queue_reserved_task('task_name', 'my_task_id',
                                       'my_resource_id', [1, 2], {'a': 2})
        except BreakOutException:
            pass
        else:
            self.fail(
                '_queue_reserved_task should have raised a BreakOutException')

        self.mock_time.sleep.assert_has_calls(
            [mock.call(0.25), mock.call(0.25)])
Пример #2
0
def _get_unreserved_worker():
    """
    Return the Worker instance that has no reserved_resource entries
    associated with it. If there are no unreserved workers a
    pulp.server.exceptions.NoWorkers exception is raised.

    :raises NoWorkers: If all workers have reserved_resource entries associated with them.

    :returns:          The Worker instance that has no reserved_resource
                       entries associated with it.
    :rtype:            pulp.server.db.model.resources.Worker
    """

    # Build a mapping of queue names to Worker objects
    workers_dict = dict(
        (worker['name'], worker) for worker in Worker.objects.get_online())
    worker_names = workers_dict.keys()
    reserved_names = [r['worker_name'] for r in ReservedResource.objects.all()]

    # Find an unreserved worker using set differences of the names, and filter
    # out workers that should not be assigned work.
    # NB: this is a little messy but set comprehensions are in python 2.7+
    unreserved_workers = set(filter(_is_worker,
                                    worker_names)) - set(reserved_names)

    try:
        return workers_dict[unreserved_workers.pop()]
    except KeyError:
        # All workers are reserved
        raise NoWorkers()
Пример #3
0
 def test_get_unreserved_worker_breaks_out_of_loop(self):
     self.mock_get_worker_for_reservation.side_effect = NoWorkers()
     self.mock_get_unreserved_worker.return_value = Worker(
         name='worker1', last_heartbeat=datetime.utcnow())
     tasks._queue_reserved_task('task_name', 'my_task_id', 'my_resource_id',
                                [1, 2], {'a': 2})
     self.assertTrue(not self.mock_time.sleep.called)
Пример #4
0
def get_unreserved_worker():
    """
    Return the Worker instance that has no reserved_resource entries associated with it. If there
    are no unreserved workers a pulp.server.exceptions.NoWorkers exception is raised.

    :raises NoWorkers: If all workers have reserved_resource entries associated with them.

    :returns:          The Worker instance that has no reserved_resource entries associated with it.
    :rtype:            pulp.server.db.model.resources.Worker
    """
    # Build a mapping of queue names to Worker objects
    workers_dict = dict((worker['name'], worker)
                        for worker in filter_workers(criteria.Criteria()))
    worker_names = [name for name in workers_dict.keys()]
    reserved_names = [
        r['worker_name']
        for r in resources.ReservedResource.get_collection().find()
    ]

    # Find an unreserved worker using set differences of the names
    unreserved_workers = set(worker_names) - set(reserved_names)
    try:
        return workers_dict[unreserved_workers.pop()]
    except KeyError:
        # All workers are reserved
        raise NoWorkers()
Пример #5
0
def get_worker_for_reservation(resource_id):
    """
    Return the Worker instance that is associated with a reservation of type resource_id. If
    there are no workers with that reservation_id type a pulp.server.exceptions.NoWorkers exception
    is raised.

    :param resource_id:    The name of the resource you wish to reserve for your task.

    :raises NoWorkers:     If all workers have reserved_resource entries associated with them.

    :type resource_id:     basestring
    :returns:              The Worker instance that has a reserved_resource entry of type
                           `resource_id` associated with it.
    :rtype:                pulp.server.db.model.resources.Worker
    """
    reservation = resources.ReservedResource.get_collection().find_one(
        {'resource_id': resource_id})
    if reservation:
        find_worker_by_name = criteria.Criteria(
            {'_id': reservation['worker_name']})
        worker_bson = resources.Worker.get_collection().query(
            find_worker_by_name)[0]
        return resources.Worker.from_bson(worker_bson)
    else:
        raise NoWorkers()
Пример #6
0
def get_worker_for_reservation(resource_id):
    """
    Return the Worker instance that is associated with a reservation of type resource_id. If
    there are no workers with that reservation_id type a pulp.server.exceptions.NoWorkers
    exception is raised.

    :param resource_id:    The name of the resource you wish to reserve for your task.

    :raises NoWorkers:     If all workers have reserved_resource entries associated with them.

    :type resource_id:     basestring
    :returns:              The Worker instance that has a reserved_resource entry of type
                           `resource_id` associated with it.
    :rtype:                pulp.server.db.model.resources.Worker
    """
    reservation = ReservedResource.objects(resource_id=resource_id).first()
    if reservation:
        return Worker.objects(name=reservation['worker_name']).first()
    else:
        raise NoWorkers()