Exemplo n.º 1
0
def _validate_and_get_resources(resources):
    resource_set = set()
    for r in resources:
        if isinstance(r, str):
            resource_set.add(r)
        elif isinstance(r, Model):
            resource_set.add(util.get_url(r))
        else:
            raise ValueError(_("Must be (str|Model)"))
    return list(resource_set)
Exemplo n.º 2
0
def enqueue_with_reservation(func,
                             resources,
                             args=None,
                             kwargs=None,
                             options=None):
    """
    Enqueue a message to Pulp workers with a reservation.

    This method provides normal enqueue functionality, while also requesting necessary locks for
    serialized urls. No two tasks that claim the same resource can execute concurrently. It
    accepts resources which it transforms into a list of urls (one for each resource).

    This does not dispatch the task directly, but instead promises to dispatch it later by
    encapsulating the desired task through a call to a :func:`_queue_reserved_task` task. See
    the docblock on :func:`_queue_reserved_task` for more information on this.

    This method creates a :class:`pulpcore.app.models.Task` object. Pulp expects to poll on a
    task just after calling this method, so a Task entry needs to exist for it
    before it returns.

    Args:
        func (callable): The function to be run by RQ when the necessary locks are acquired.
        resources (list): A list of resources to reserve guaranteeing that only one task
            reserves these resources
        args (tuple): The positional arguments to pass on to the task.
        kwargs (dict): The keyword arguments to pass on to the task.
        options (dict): The options to be passed on to the task.

    Returns (rq.job.job): An RQ Job instance as returned by RQ's enqueue function
    """
    if not args:
        args = tuple()
    if not kwargs:
        kwargs = dict()
    if not options:
        options = dict()

    resources = {util.get_url(resource) for resource in resources}
    inner_task_id = str(uuid.uuid4())
    Task.objects.create(pk=inner_task_id, state=TASK_STATES.WAITING)
    redis_conn = connection.get_redis_connection()
    q = Queue('resource_manager', connection=redis_conn)
    task_args = (func, inner_task_id, list(resources), args, kwargs, options)
    q.enqueue(_queue_reserved_task, args=task_args, timeout=TASK_TIMEOUT)
    return Job(id=inner_task_id, connection=redis_conn)
Exemplo n.º 3
0
def enqueue_with_reservation(func, resources, args=None, kwargs=None, options=None):
    """
    Enqueue a message to Pulp workers with a reservation.

    This method provides normal enqueue functionality, while also requesting necessary locks for
    serialized urls. No two tasks that claim the same resource can execute concurrently. It
    accepts resources which it transforms into a list of urls (one for each resource).

    This does not dispatch the task directly, but instead promises to dispatch it later by
    encapsulating the desired task through a call to a :func:`_queue_reserved_task` task. See
    the docblock on :func:`_queue_reserved_task` for more information on this.

    This method creates a :class:`pulpcore.app.models.Task` object. Pulp expects to poll on a
    task just after calling this method, so a Task entry needs to exist for it
    before it returns.

    Args:
        func (callable): The function to be run by RQ when the necessary locks are acquired.
        resources (list): A list of resources to reserve guaranteeing that only one task
            reserves these resources
        args (tuple): The positional arguments to pass on to the task.
        kwargs (dict): The keyword arguments to pass on to the task.
        options (dict): The options to be passed on to the task.

    Returns (rq.job.job): An RQ Job instance as returned by RQ's enqueue function
    """
    if not args:
        args = tuple()
    if not kwargs:
        kwargs = dict()
    if not options:
        options = dict()

    resources = {util.get_url(resource) for resource in resources}
    inner_task_id = str(uuid.uuid4())
    Task.objects.create(pk=inner_task_id, state=TASK_STATES.WAITING)
    redis_conn = connection.get_redis_connection()
    q = Queue('resource_manager', connection=redis_conn)
    task_args = (func, inner_task_id, list(resources), args, kwargs, options)
    q.enqueue(_queue_reserved_task, args=task_args, timeout=TASK_TIMEOUT)
    return Job(id=inner_task_id, connection=redis_conn)
Exemplo n.º 4
0
 def as_url(r):
     if isinstance(r, str):
         return r
     if isinstance(r, Model):
         return util.get_url(r)
     raise ValueError(_('Must be (str|Model)'))