Ejemplo n.º 1
0
 def __call__(self, *args, **kwargs):
     """
     This overrides CeleryTask's __call__() method. We use this method
     for task state tracking of Pulp tasks.
     """
     # Check task status and skip running the task if task state is 'canceled'.
     task_status = TaskStatusManager.find_by_task_id(
         task_id=self.request.id)
     if task_status and task_status[
             'state'] == constants.CALL_CANCELED_STATE:
         logger.debug("Task cancel received for task-id : [%s]" %
                      self.request.id)
         return
     # Update start_time and set the task state to 'running' for asynchronous tasks.
     # Skip updating status for eagerly executed tasks, since we don't want to track
     # synchronous tasks in our database.
     if not self.request.called_directly:
         now = datetime.now(dateutils.utc_tz())
         start_time = dateutils.format_iso8601_datetime(now)
         # Using 'upsert' to avoid a possible race condition described in the apply_async method
         # above.
         TaskStatus.get_collection().update({'task_id': self.request.id}, {
             '$set': {
                 'state': constants.CALL_RUNNING_STATE,
                 'start_time': start_time
             }
         },
                                            upsert=True)
     # Run the actual task
     logger.debug("Running task : [%s]" % self.request.id)
     return super(Task, self).__call__(*args, **kwargs)
Ejemplo n.º 2
0
    def update_task_status(task_id, delta):
        """
        Updates status of the task with given task id. Only the following
        fields may be updated through this call:
        * state
        * result
        * traceback
        * start_time
        * finish_time
        * error
        * spawned_tasks
        * progress_report
        Other fields found in delta will be ignored.

        :param task_id: identity of the task this status corresponds to
        :type  task_id: basetring
        :param delta: list of attributes and their new values to change
        :type  delta: dict
        :return: updated serialized task status
        :rtype:  dict
        :raise MissingResource: if there is no task status corresponding to the given task_id
        """

        task_status = TaskStatus.get_collection().find_one({'task_id': task_id})
        if task_status is None:
            raise MissingResource(task_id)

        updatable_attributes = ['state', 'result', 'traceback', 'start_time', 'finish_time',
                                'error', 'spawned_tasks', 'progress_report']
        for key, value in delta.items():
            if key in updatable_attributes:
                task_status[key] = value

        TaskStatus.get_collection().save(task_status, safe=True)
        return task_status
Ejemplo n.º 3
0
    def apply_async(self, *args, **kwargs):
        """
        A wrapper around the Celery apply_async method. It allows us to accept a few more
        parameters than Celery does for our own purposes, listed below. It also allows us
        to create and update task status which can be used to track status of this task
        during it's lifetime.

        :param queue:       The queue that the task has been placed into (optional, defaults to
                            the general Celery queue.)
        :type  queue:       basestring
        :param tags:        A list of tags (strings) to place onto the task, used for searching for
                            tasks by tag
        :type  tags:        list
        :return:            An AsyncResult instance as returned by Celery's apply_async
        :rtype:             celery.result.AsyncResult
        """
        queue = kwargs.get('queue', defaults.NAMESPACES['CELERY']['DEFAULT_QUEUE'].default)
        tags = kwargs.pop('tags', [])
        async_result = super(Task, self).apply_async(*args, **kwargs)
        async_result.tags = tags

        # Create a new task status with the task id and tags.
        # To avoid the race condition where __call__ method below is called before
        # this change is propagated to all db nodes, using an 'upsert' here and setting
        # the task state to 'waiting' only on an insert.
        TaskStatus.get_collection().update(
            {'task_id': async_result.id},
            {'$setOnInsert': {'state':dispatch_constants.CALL_WAITING_STATE},
             '$set': {'queue': queue, 'tags': tags}},
            upsert=True)
        return async_result
Ejemplo n.º 4
0
 def __call__(self, *args, **kwargs):
     """
     This overrides CeleryTask's __call__() method. We use this method
     for task state tracking of Pulp tasks.
     """
     # Check task status and skip running the task if task state is 'canceled'.
     task_status = TaskStatusManager.find_by_task_id(task_id=self.request.id)
     if task_status and task_status['state'] == dispatch_constants.CALL_CANCELED_STATE:
         logger.debug("Task cancel received for task-id : [%s]" % self.request.id)
         return
     # Update start_time and set the task state to 'running' for asynchronous tasks.
     # Skip updating status for eagerly executed tasks, since we don't want to track
     # synchronous tasks in our database.
     if not self.request.called_directly:
         now = datetime.now(dateutils.utc_tz())
         start_time = dateutils.format_iso8601_datetime(now)
         # Using 'upsert' to avoid a possible race condition described in the apply_async method
         # above.
         TaskStatus.get_collection().update(
             {'task_id': self.request.id},
             {'$set': {'state': dispatch_constants.CALL_RUNNING_STATE,
                       'start_time': start_time}},
             upsert=True)
     # Run the actual task
     logger.debug("Running task : [%s]" % self.request.id)
     return super(Task, self).__call__(*args, **kwargs)
Ejemplo n.º 5
0
def cancel(task_id):
    """
    Cancel the task that is represented by the given task_id. This method cancels only the task
    with given task_id, not the spawned tasks. This also updates task's state to 'canceled'.

    :param task_id: The ID of the task you wish to cancel
    :type  task_id: basestring

    :raises MissingResource: if a task with given task_id does not exist
    :raises PulpCodedException: if given task is already in a complete state
    """
    task_status = TaskStatusManager.find_by_task_id(task_id)
    if task_status is None:
        raise MissingResource(task_id)
    if task_status['state'] in constants.CALL_COMPLETE_STATES:
        # If the task is already done, just stop
        msg = _('Task [%(task_id)s] already in a completed state: %(state)s')
        logger.info(msg % {'task_id': task_id, 'state': task_status['state']})
        return
    controller.revoke(task_id, terminate=True)
    TaskStatus.get_collection().find_and_modify(
        {'task_id': task_id, 'state': {'$nin': constants.CALL_COMPLETE_STATES}},
        {'$set': {'state': constants.CALL_CANCELED_STATE}})
    msg = _('Task canceled: %(task_id)s.')
    msg = msg % {'task_id': task_id}
    logger.info(msg)
Ejemplo n.º 6
0
 def __call__(self, *args, **kwargs):
     """
     This overrides CeleryTask's __call__() method. We use this method
     for task state tracking of Pulp tasks.
     """
     # Add task_id to the task context, so that agent and plugins have access to the task id.
     # There are a few other attributes in the context as defined by old dispatch system.
     # These are unused right now. These should be removed when we cleanup the dispatch folder
     # after the migration to celery is complete.
     task_context = dispatch_factory.context()
     task_context.call_request_id = self.request.id
     # Check task status and skip running the task if task state is 'canceled'.
     task_status = TaskStatusManager.find_by_task_id(task_id=self.request.id)
     if task_status and task_status['state'] == dispatch_constants.CALL_CANCELED_STATE:
         logger.debug("Task cancel received for task-id : [%s]" % self.request.id)
         return
     # Update start_time and set the task state to 'running' for asynchronous tasks.
     # Skip updating status for eagerly executed tasks, since we don't want to track
     # synchronous tasks in our database.
     if not self.request.called_directly:
         # Using 'upsert' to avoid a possible race condition described in the apply_async method
         # above.
         TaskStatus.get_collection().update(
             {'task_id': self.request.id},
             {'$set': {'state': dispatch_constants.CALL_RUNNING_STATE,
                       'start_time': dateutils.now_utc_timestamp()}},
             upsert=True)
     # Run the actual task
     logger.debug("Running task : [%s]" % self.request.id)
     return super(Task, self).__call__(*args, **kwargs)
Ejemplo n.º 7
0
def cancel(task_id):
    """
    Cancel the task that is represented by the given task_id. This method cancels only the task
    with given task_id, not the spawned tasks. This also updates task's state to 'canceled'.

    :param task_id: The ID of the task you wish to cancel
    :type  task_id: basestring

    :raises MissingResource: if a task with given task_id does not exist
    :raises PulpCodedException: if given task is already in a complete state
    """
    task_status = TaskStatusManager.find_by_task_id(task_id)
    if task_status is None:
        raise MissingResource(task_id)
    if task_status['state'] in constants.CALL_COMPLETE_STATES:
        # If the task is already done, just stop
        msg = _('Task [%(task_id)s] already in a completed state: %(state)s')
        logger.info(msg % {'task_id': task_id, 'state': task_status['state']})
        return
    controller.revoke(task_id, terminate=True)
    TaskStatus.get_collection().find_and_modify(
        {
            'task_id': task_id,
            'state': {
                '$nin': constants.CALL_COMPLETE_STATES
            }
        }, {'$set': {
            'state': constants.CALL_CANCELED_STATE
        }})
    msg = _('Task canceled: %(task_id)s.')
    msg = msg % {'task_id': task_id}
    logger.info(msg)
Ejemplo n.º 8
0
    def delete_task_status(task_id):
        """
        Deletes the task status with given task id.

        :param task_id: identity of the task this status corresponds to
        :type  task_id: basestring
        :raise MissingResource: if the given task status does not exist
        :raise InvalidValue: if task_id is invalid
        """
        task_status = TaskStatus.get_collection().find_one({'task_id': task_id})
        if task_status is None:
            raise MissingResource(task_id)

        TaskStatus.get_collection().remove({'task_id': task_id}, safe=True)
Ejemplo n.º 9
0
    def delete_task_status(task_id):
        """
        Deletes the task status with given task id.

        :param task_id: identity of the task this status corresponds to
        :type  task_id: basestring
        :raise MissingResource: if the given task status does not exist
        :raise InvalidValue: if task_id is invalid
        """
        task_status = TaskStatus.get_collection().find_one(
            {'task_id': task_id})
        if task_status is None:
            raise MissingResource(task_id)

        TaskStatus.get_collection().remove({'task_id': task_id}, safe=True)
Ejemplo n.º 10
0
    def set_task_succeeded(task_id, result=None, timestamp=None):
        """
        Update a task's state to reflect that it has succeeded.
        :param task_id: The identity of the task to be updated.
        :type  task_id: basestring
        :param result: The optional value returned by the task execution.
        :type result: anything
        :param timestamp: The (optional) ISO-8601 finished timestamp (UTC).
        :type timestamp: str
        """
        collection = TaskStatus.get_collection()

        if not timestamp:
            now = datetime.now(dateutils.utc_tz())
            finished = dateutils.format_iso8601_datetime(now)
        else:
            finished = timestamp

        update = {
            '$set': {
                'finish_time': finished,
                'state': constants.CALL_FINISHED_STATE,
                'result': result
            }
        }

        collection.update({'task_id': task_id}, update, safe=True)
Ejemplo n.º 11
0
    def set_task_started(task_id, timestamp=None):
        """
        Update a task's state to reflect that it has started running.
        :param task_id: The identity of the task to be updated.
        :type  task_id: basestring
        :param timestamp: The (optional) ISO-8601 finished timestamp (UTC).
        :type timestamp: str
        """
        collection = TaskStatus.get_collection()

        if not timestamp:
            now = datetime.now(dateutils.utc_tz())
            started = dateutils.format_iso8601_datetime(now)
        else:
            started = timestamp

        select = {
            'task_id': task_id
        }
        update = {
            '$set': {'start_time': started}
        }

        collection.update(select, update, safe=True)

        select = {
            'task_id': task_id,
            'state': {'$in': [constants.CALL_WAITING_STATE, constants.CALL_ACCEPTED_STATE]}
        }
        update = {
            '$set': {'state': constants.CALL_RUNNING_STATE}
        }

        collection.update(select, update, safe=True)
Ejemplo n.º 12
0
    def set_task_failed(task_id, traceback=None, timestamp=None):
        """
        Update a task's state to reflect that it has succeeded.
        :param task_id: The identity of the task to be updated.
        :type  task_id: basestring
        :ivar traceback: A string representation of the traceback resulting from the task execution.
        :type traceback: basestring
        :param timestamp: The (optional) ISO-8601 finished timestamp (UTC).
        :type timestamp: str
        """
        collection = TaskStatus.get_collection()

        if not timestamp:
            now = datetime.now(dateutils.utc_tz())
            finished = dateutils.format_iso8601_datetime(now)
        else:
            finished = timestamp

        update = {
            '$set': {
                'finish_time': finished,
                'state': constants.CALL_ERROR_STATE,
                'traceback': traceback
            }
        }

        collection.update({'task_id': task_id}, update, safe=True)
Ejemplo n.º 13
0
    def save_update_defaults(self):
        """
        Test the save method with default arguments when the object is already in the database.
        """
        task_id = 'a_task_id'
        worker_name = 'worker_name'
        tags = ['tag_1', 'tag_2']
        state = 'a state'
        spawned_tasks = ['foo']
        error = 'some_error'
        progress_report = {
            'what do we want?': 'progress!',
            'when do we want it?': 'now!'
        }
        task_type = 'some.task'
        start_time = datetime.now()
        finish_time = start_time + timedelta(minutes=5)
        result = None
        ts = TaskStatus(task_id,
                        worker_name,
                        tags,
                        state,
                        spawned_tasks=spawned_tasks,
                        error=error,
                        progress_report=progress_report,
                        task_type=task_type,
                        start_time=start_time,
                        finish_time=finish_time,
                        result=result)
        # Let's go ahead and insert the object
        ts.save()
        # Now let's alter it a bit, and make sure the alteration makes it to the DB correctly.
        new_state = 'some_altered_state_of_consciousness'
        ts.state = new_state

        # This should update ts in the database
        ts.save()

        ts = TaskStatus.get_collection().find()
        # There should only be one TaskStatus in the db
        self.assertEqual(len(ts), 1)
        ts = ts[0]
        # Make sure all the attributes are correct
        self.assertEqual(ts['task_id'], task_id)
        self.assertEqual(ts['worker_name'], worker_name)
        self.assertEqual(ts['tags'], tags)
        # The state should have been updated
        self.assertEqual(ts['state'], new_state)
        self.assertEqual(ts['error'], error)
        self.assertEqual(ts['spawned_tasks'], spawned_tasks)
        self.assertEqual(ts['progress_report'], progress_report)
        self.assertEqual(ts['task_type'], task_type)
        self.assertEqual(ts['start_time'], start_time)
        self.assertEqual(ts['finish_time'], finish_time)
        self.assertEqual(ts['result'], result)
        # These are always None
        self.assertEqual(ts['traceback'], None)
        self.assertEqual(ts['exception'], None)
Ejemplo n.º 14
0
    def find_all():
        """
        Returns serialized versions of all task statuses in the database.

        :return: pymongo cursor for the list of serialized task statuses
        :rtype:  pymongo.cursor.Cursor
        """
        all_task_statuses = TaskStatus.get_collection().find()
        return all_task_statuses
Ejemplo n.º 15
0
    def find_all():
        """
        Returns serialized versions of all task statuses in the database.

        :return: pymongo cursor for the list of serialized task statuses
        :rtype:  pymongo.cursor.Cursor
        """
        all_task_statuses = TaskStatus.get_collection().find()
        return all_task_statuses
Ejemplo n.º 16
0
    def find_by_task_id(task_id):
        """
        Returns a serialized version the status of given task, if it exists.
        If a task status cannot be found with the given task_id, None is returned.

        :return: serialized task status
        :rtype:  dict or None
        """
        task_status = TaskStatus.get_collection().find_one({'task_id': task_id})
        return task_status
Ejemplo n.º 17
0
    def create_task_status(task_id, worker_name=None, tags=None, state=None):
        """
        Creates a new task status for given task_id.

        :param task_id:           identity of the task this status corresponds to
        :type  task_id:           basestring
        :param worker_name:       The name of the worker that the Task is in
        :type  worker_name:       basestring
        :param tags:              custom tags on the task
        :type  tags:              list of basestrings or None
        :param state:             state of callable in its lifecycle
        :type  state:             basestring
        :return:                  task status document
        :rtype:                   dict
        :raise DuplicateResource: if there is already a task status entry with the requested task id
        :raise InvalidValue:      if any of the fields are unacceptable
        """
        invalid_values = []
        if task_id is None:
            invalid_values.append('task_id')
        if worker_name is not None and not isinstance(worker_name, basestring):
            invalid_values.append('worker_name')
        if tags is not None and not isinstance(tags, list):
            invalid_values.append('tags')
        if state is not None and not isinstance(state, basestring):
            invalid_values.append('state')
        if invalid_values:
            raise InvalidValue(invalid_values)

        if not state:
            state = constants.CALL_WAITING_STATE

        task_status = TaskStatus(task_id=task_id,
                                 worker_name=worker_name,
                                 tags=tags,
                                 state=state)
        try:
            TaskStatus.get_collection().save(task_status, safe=True)
        except DuplicateKeyError:
            raise DuplicateResource(task_id)

        created = TaskStatus.get_collection().find_one({'task_id': task_id})
        return created
Ejemplo n.º 18
0
    def save_update_with_set_on_insert(self):
        """
        Test the save method with set on insert arguments when the object is already in the
        database.
        """
        task_id = 'a_task_id'
        queue = 'some_queue'
        tags = ['tag_1', 'tag_2']
        state = 'a state'
        spawned_tasks = ['foo']
        error = 'some_error'
        progress_report = {'what do we want?': 'progress!', 'when do we want it?': 'now!'}
        task_type = 'some.task'
        start_time = datetime.now()
        finish_time = start_time + timedelta(minutes=5)
        result = None
        ts = TaskStatus(
            task_id, queue, tags, state, spawned_tasks=spawned_tasks, error=error,
            progress_report=progress_report, task_type=task_type, start_time=start_time,
            finish_time=finish_time, result=result)
        # Put the object in the database, and then change some of it settings.
        ts.save()
        new_queue = 'a_different_queue'
        new_state = 'some_other_state'
        new_start_time = start_time + timedelta(minutes=10)
        ts.queue = new_queue
        ts.state = new_state
        ts.start_time = new_start_time

        # This should update the queue on ts in the database, but should not update the state or
        # start_time
        ts.save(fields_to_set_on_insert=['state', 'start_time'])

        ts = TaskStatus.get_collection().find()
        # There should only be one TaskStatus in the db
        self.assertEqual(len(ts), 1)
        ts = ts[0]
        # Make sure all the attributes are correct
        self.assertEqual(ts['task_id'], task_id)
        # Queue should have been updated
        self.assertEqual(ts['queue'], new_queue)
        self.assertEqual(ts['tags'], tags)
        # state should not have been updated
        self.assertEqual(ts['state'], state)
        self.assertEqual(ts['error'], error)
        self.assertEqual(ts['spawned_tasks'], spawned_tasks)
        self.assertEqual(ts['progress_report'], progress_report)
        self.assertEqual(ts['task_type'], task_type)
        # start_time should not have been updated
        self.assertEqual(ts['start_time'], start_time)
        self.assertEqual(ts['finish_time'], finish_time)
        self.assertEqual(ts['result'], result)
        # These are always None
        self.assertEqual(ts['traceback'], None)
        self.assertEqual(ts['exception'], None)
Ejemplo n.º 19
0
    def find_by_criteria(criteria):
        """
        Return a list of task statuses that match the provided criteria.

        :param criteria:    A Criteria object representing a search you want
                            to perform
        :type  criteria:    pulp.server.db.model.criteria.Criteria
        :return:    pymongo cursor for the TaskStatus instances satisfying the query
        :rtype:     pymongo.cursor.Cursor
        """
        return TaskStatus.get_collection().query(criteria)
Ejemplo n.º 20
0
    def find_by_task_id(task_id):
        """
        Returns a serialized version the status of given task, if it exists.
        If a task status cannot be found with the given task_id, None is returned.

        :return: serialized task status
        :rtype:  dict or None
        """
        task_status = TaskStatus.get_collection().find_one(
            {'task_id': task_id})
        return task_status
Ejemplo n.º 21
0
    def find_by_criteria(criteria):
        """
        Return a list of task statuses that match the provided criteria.

        :param criteria:    A Criteria object representing a search you want
                            to perform
        :type  criteria:    pulp.server.db.model.criteria.Criteria
        :return:    pymongo cursor for the TaskStatus instances satisfying the query
        :rtype:     pymongo.cursor.Cursor
        """
        return TaskStatus.get_collection().query(criteria)
Ejemplo n.º 22
0
    def save_insert_with_set_on_insert(self):
        """
        Test the save method with set on insert arguments when the object is not already in the
        database.
        """
        task_id = 'a_task_id'
        worker_name = 'some_worker'
        tags = ['tag_1', 'tag_2']
        state = 'a state'
        spawned_tasks = ['foo']
        error = 'some_error'
        progress_report = {
            'what do we want?': 'progress!',
            'when do we want it?': 'now!'
        }
        task_type = 'some.task'
        start_time = datetime.now()
        finish_time = start_time + timedelta(minutes=5)
        result = None
        ts = TaskStatus(task_id,
                        worker_name,
                        tags,
                        state,
                        spawned_tasks=spawned_tasks,
                        error=error,
                        progress_report=progress_report,
                        task_type=task_type,
                        start_time=start_time,
                        finish_time=finish_time,
                        result=result)

        # This should cause ts to be in the database
        ts.save(fields_to_set_on_insert=['state', 'start_time'])

        ts = TaskStatus.get_collection().find()
        # There should only be one TaskStatus in the db
        self.assertEqual(len(ts), 1)
        ts = ts[0]
        # Make sure all the attributes are correct
        self.assertEqual(ts['task_id'], task_id)
        self.assertEqual(ts['worker_name'], worker_name)
        self.assertEqual(ts['tags'], tags)
        self.assertEqual(ts['state'], state)
        self.assertEqual(ts['error'], error)
        self.assertEqual(ts['spawned_tasks'], spawned_tasks)
        self.assertEqual(ts['progress_report'], progress_report)
        self.assertEqual(ts['task_type'], task_type)
        self.assertEqual(ts['start_time'], start_time)
        self.assertEqual(ts['finish_time'], finish_time)
        self.assertEqual(ts['result'], result)
        # These are always None
        self.assertEqual(ts['traceback'], None)
        self.assertEqual(ts['exception'], None)
Ejemplo n.º 23
0
    def create_task_status(task_id, worker_name=None, tags=None, state=None):
        """
        Creates a new task status for given task_id.

        :param task_id:           identity of the task this status corresponds to
        :type  task_id:           basestring
        :param worker_name:       The name of the worker that the Task is in
        :type  worker_name:       basestring
        :param tags:              custom tags on the task
        :type  tags:              list of basestrings or None
        :param state:             state of callable in its lifecycle
        :type  state:             basestring
        :return:                  task status document
        :rtype:                   dict
        :raise DuplicateResource: if there is already a task status entry with the requested task id
        :raise InvalidValue:      if any of the fields are unacceptable
        """
        invalid_values = []
        if task_id is None:
            invalid_values.append('task_id')
        if worker_name is not None and not isinstance(worker_name, basestring):
            invalid_values.append('worker_name')
        if tags is not None and not isinstance(tags, list):
            invalid_values.append('tags')
        if state is not None and not isinstance(state, basestring):
            invalid_values.append('state')
        if invalid_values:
            raise InvalidValue(invalid_values)

        if not state:
            state = constants.CALL_WAITING_STATE

        task_status = TaskStatus(task_id=task_id, worker_name=worker_name, tags=tags, state=state)
        try:
            TaskStatus.get_collection().save(task_status, safe=True)
        except DuplicateKeyError:
            raise DuplicateResource(task_id)

        created = TaskStatus.get_collection().find_one({'task_id': task_id})
        return created
Ejemplo n.º 24
0
    def update_task_status(task_id, delta):
        """
        Updates status of the task with given task id. Only the following
        fields may be updated through this call:
        * state
        * result
        * traceback
        * start_time
        * finish_time
        * error
        * spawned_tasks
        * progress_report
        Other fields found in delta will be ignored.

        :param task_id: identity of the task this status corresponds to
        :type  task_id: basetring
        :param delta: list of attributes and their new values to change
        :type  delta: dict
        :return: updated serialized task status
        :rtype:  dict
        :raise MissingResource: if there is no task status corresponding to the given task_id
        """

        task_status = TaskStatus.get_collection().find_one(
            {'task_id': task_id})
        if task_status is None:
            raise MissingResource(task_id)

        updatable_attributes = [
            'state', 'result', 'traceback', 'start_time', 'finish_time',
            'error', 'spawned_tasks', 'progress_report'
        ]
        for key, value in delta.items():
            if key in updatable_attributes:
                task_status[key] = value

        TaskStatus.get_collection().save(task_status, safe=True)
        return task_status
Ejemplo n.º 25
0
    def test_create_task_status_defaults(self):
        """
        Tests create_task_status() with minimal information, to ensure that defaults are handled
        properly.
        """
        task_id = self.get_random_uuid()

        TaskStatusManager.create_task_status(task_id)

        task_statuses = list(TaskStatus.get_collection().find())
        self.assertEqual(1, len(task_statuses))
        self.assertEqual(task_id, task_statuses[0]['task_id'])
        self.assertEqual(None, task_statuses[0]['worker_name'])
        self.assertEqual([], task_statuses[0]['tags'])
        self.assertEqual('waiting', task_statuses[0]['state'])
    def test_create_task_status_defaults(self):
        """
        Tests create_task_status() with minimal information, to ensure that defaults are handled
        properly.
        """
        task_id = self.get_random_uuid()

        TaskStatusManager.create_task_status(task_id)

        task_statuses = list(TaskStatus.get_collection().find())
        self.assertEqual(1, len(task_statuses))
        self.assertEqual(task_id, task_statuses[0]['task_id'])
        self.assertEqual(None, task_statuses[0]['worker_name'])
        self.assertEqual([], task_statuses[0]['tags'])
        self.assertEqual('waiting', task_statuses[0]['state'])
Ejemplo n.º 27
0
    def save_update_defaults(self):
        """
        Test the save method with default arguments when the object is already in the database.
        """
        task_id = 'a_task_id'
        queue = 'some_queue'
        tags = ['tag_1', 'tag_2']
        state = 'a state'
        spawned_tasks = ['foo']
        error = 'some_error'
        progress_report = {'what do we want?': 'progress!', 'when do we want it?': 'now!'}
        task_type = 'some.task'
        start_time = datetime.now()
        finish_time = start_time + timedelta(minutes=5)
        result = None
        ts = TaskStatus(
            task_id, queue, tags, state, spawned_tasks=spawned_tasks, error=error,
            progress_report=progress_report, task_type=task_type, start_time=start_time,
            finish_time=finish_time, result=result)
        # Let's go ahead and insert the object
        ts.save()
        # Now let's alter it a bit, and make sure the alteration makes it to the DB correctly.
        new_state = 'some_altered_state_of_consciousness'
        ts.state = new_state

        # This should update ts in the database
        ts.save()

        ts = TaskStatus.get_collection().find()
        # There should only be one TaskStatus in the db
        self.assertEqual(len(ts), 1)
        ts = ts[0]
        # Make sure all the attributes are correct
        self.assertEqual(ts['task_id'], task_id)
        self.assertEqual(ts['queue'], queue)
        self.assertEqual(ts['tags'], tags)
        # The state should have been updated
        self.assertEqual(ts['state'], new_state)
        self.assertEqual(ts['error'], error)
        self.assertEqual(ts['spawned_tasks'], spawned_tasks)
        self.assertEqual(ts['progress_report'], progress_report)
        self.assertEqual(ts['task_type'], task_type)
        self.assertEqual(ts['start_time'], start_time)
        self.assertEqual(ts['finish_time'], finish_time)
        self.assertEqual(ts['result'], result)
        # These are always None
        self.assertEqual(ts['traceback'], None)
        self.assertEqual(ts['exception'], None)
Ejemplo n.º 28
0
    def set_task_accepted(task_id):
        """
        Update a task's state to reflect that it has been accepted.
        :param task_id: The identity of the task to be updated.
        :type  task_id: basestring
        """
        collection = TaskStatus.get_collection()

        select = {
            'task_id': task_id,
            'state': constants.CALL_WAITING_STATE
        }
        update = {
            '$set': {'state': constants.CALL_ACCEPTED_STATE}
        }

        collection.update(select, update, safe=True)
Ejemplo n.º 29
0
    def save_insert_with_set_on_insert(self):
        """
        Test the save method with set on insert arguments when the object is not already in the
        database.
        """
        task_id = 'a_task_id'
        queue = 'some_queue'
        tags = ['tag_1', 'tag_2']
        state = 'a state'
        spawned_tasks = ['foo']
        error = 'some_error'
        progress_report = {'what do we want?': 'progress!', 'when do we want it?': 'now!'}
        task_type = 'some.task'
        start_time = datetime.now()
        finish_time = start_time + timedelta(minutes=5)
        result = None
        ts = TaskStatus(
            task_id, queue, tags, state, spawned_tasks=spawned_tasks, error=error,
            progress_report=progress_report, task_type=task_type, start_time=start_time,
            finish_time=finish_time, result=result)

        # This should cause ts to be in the database
        ts.save(fields_to_set_on_insert=['state', 'start_time'])

        ts = TaskStatus.get_collection().find()
        # There should only be one TaskStatus in the db
        self.assertEqual(len(ts), 1)
        ts = ts[0]
        # Make sure all the attributes are correct
        self.assertEqual(ts['task_id'], task_id)
        self.assertEqual(ts['queue'], queue)
        self.assertEqual(ts['tags'], tags)
        self.assertEqual(ts['state'], state)
        self.assertEqual(ts['error'], error)
        self.assertEqual(ts['spawned_tasks'], spawned_tasks)
        self.assertEqual(ts['progress_report'], progress_report)
        self.assertEqual(ts['task_type'], task_type)
        self.assertEqual(ts['start_time'], start_time)
        self.assertEqual(ts['finish_time'], finish_time)
        self.assertEqual(ts['result'], result)
        # These are always None
        self.assertEqual(ts['traceback'], None)
        self.assertEqual(ts['exception'], None)
Ejemplo n.º 30
0
    def test_create_task_status(self):
        """
        Tests that create_task_status() with valid data is successful.
        """
        task_id = self.get_random_uuid()
        queue = 'a_queue'
        tags = ['test-tag1', 'test-tag2']
        state = 'waiting'

        created = TaskStatusManager.create_task_status(task_id, queue, tags, state)

        task_statuses = list(TaskStatus.get_collection().find())
        self.assertEqual(1, len(task_statuses))

        task_status = task_statuses[0]
        self.assertEqual(task_id, task_status['task_id'])
        self.assertEqual(queue, task_status['queue'])
        self.assertEqual(tags, task_status['tags'])
        self.assertEqual(state, task_status['state'])

        self.assertEqual(task_id, created['task_id'])
        self.assertEqual(tags, created['tags'])
        self.assertEqual(state, created['state'])
Ejemplo n.º 31
0
    def test_create_task_status(self):
        """
        Tests that create_task_status() with valid data is successful.
        """
        task_id = self.get_random_uuid()
        queue = 'a_queue'
        tags = ['test-tag1', 'test-tag2']
        state = 'waiting'

        created = TaskStatusManager.create_task_status(task_id, queue, tags,
                                                       state)

        task_statuses = list(TaskStatus.get_collection().find())
        self.assertEqual(1, len(task_statuses))

        task_status = task_statuses[0]
        self.assertEqual(task_id, task_status['task_id'])
        self.assertEqual(queue, task_status['queue'])
        self.assertEqual(tags, task_status['tags'])
        self.assertEqual(state, task_status['state'])

        self.assertEqual(task_id, created['task_id'])
        self.assertEqual(tags, created['tags'])
        self.assertEqual(state, created['state'])
Ejemplo n.º 32
0
 def tearDown(self):
     PulpServerTests.tearDown(self)
     TaskStatus.get_collection().remove()
Ejemplo n.º 33
0
 def tearDown(self):
     """
     Remove the TaskStatus objects that were generated by these tests.
     """
     TaskStatus.get_collection().remove()
Ejemplo n.º 34
0
 def clean(self):
     super(TaskStatusManagerTests, self).clean()
     TaskStatus.get_collection().remove(safe=True)
Ejemplo n.º 35
0
 def tearDown(self):
     super(TestTaskResource, self).tearDown()
     TaskStatus.get_collection().remove()
Ejemplo n.º 36
0
 def setUp(self):
     super(TestTaskResource, self).setUp()
     TaskStatus.get_collection().remove()
     self.task_resource = dispatch_controller.TaskResource()
Ejemplo n.º 37
0
 def tearDown(self):
     Worker.get_collection().remove()
     ReservedResource.get_collection().remove()
     TaskStatus.get_collection().remove()
Ejemplo n.º 38
0
Archivo: base.py Proyecto: signull/pulp
 def tearDown(self):
     super(PulpWebserviceTests, self).tearDown()
     User.get_collection().remove()
     TaskStatus.get_collection().remove()
Ejemplo n.º 39
0
 def tearDown(self):
     super(TestTaskResource, self).tearDown()
     TaskStatus.get_collection().remove()
Ejemplo n.º 40
0
 def tearDown(self):
     super(PulpWebserviceTests, self).tearDown()
     User.get_collection().remove(safe=True)
     TaskStatus.get_collection().remove(safe=True)
Ejemplo n.º 41
0
 def setUp(self):
     super(TestTaskResource, self).setUp()
     TaskStatus.get_collection().remove()
     self.task_resource = dispatch_controller.TaskResource()
Ejemplo n.º 42
0
 def setUp(self):
     PulpServerTests.setUp(self)
     TaskStatus.get_collection().remove()
Ejemplo n.º 43
0
 def tearDown(self):
     Worker.get_collection().remove()
     ReservedResource.get_collection().remove()
     TaskStatus.get_collection().remove()
Ejemplo n.º 44
0
Archivo: base.py Proyecto: signull/pulp
 def tearDown(self):
     AvailableQueue.get_collection().remove()
     ReservedResource.get_collection().remove()
     TaskStatus.get_collection().remove()
Ejemplo n.º 45
0
 def clean(self):
     super(TaskStatusManagerTests, self).clean()
     TaskStatus.get_collection().remove(safe=True)
Ejemplo n.º 46
0
 def tearDown(self):
     PulpServerTests.tearDown(self)
     TaskStatus.get_collection().remove()
Ejemplo n.º 47
0
 def setUp(self):
     PulpServerTests.setUp(self)
     TaskStatus.get_collection().remove()