Exemple #1
0
    def test_show_task_list(self):
        task = ProgressReportTask(self.func_mock)
        with nested(
            patch('sys.stdout', new_callable=StringIO.StringIO),
            patch('pgreport.tasks.celery')
        ) as (std_mock, cel_mock):
            stat_mock = MagicMock()
            stat_mock.active.return_value = {"queue": None}
            cel_mock.control.inspect.return_value = stat_mock
            task.show_task_list()    

        cel_mock.control.inspect.assert_called_once_with()
        self.assertEquals(std_mock.getvalue(),
            "*** Active queues ***\n{}: []\n".format("queue")
        )

        status = {
            "args": "argment",
            "id": "task_id",
            "name": "task_name",
            "worker_pid": "worker_pid",
        }
        with nested(
            patch('sys.stdout', new_callable=StringIO.StringIO),
            patch('pgreport.tasks.celery')
        ) as (std_mock, cel_mock):
            stat_mock = MagicMock()
            stat_mock.active.return_value = {"queue": [status]}
            cel_mock.control.inspect.return_value = stat_mock
            task.show_task_list()    

        self.assertEquals(std_mock.getvalue(),
            '*** Active queues ***\nqueue: [\n * Task id: {id}, Task args: {args},  Task name: {name}, Worker pid: {worker_pid}\n]\n'.format(**status)
        )
Exemple #2
0
    def test_show_task_list(self):
        task = ProgressReportTask(self.func_mock)
        with nested(patch("sys.stdout", new_callable=StringIO.StringIO), patch("pgreport.tasks.celery")) as (
            std_mock,
            cel_mock,
        ):
            stat_mock = MagicMock()
            stat_mock.active.return_value = {"queue": None}
            cel_mock.control.inspect.return_value = stat_mock
            task.show_task_list()

        cel_mock.control.inspect.assert_called_once_with()
        self.assertEquals(std_mock.getvalue(), "*** Active queues ***\n{}: []\n".format("queue"))

        status = {"args": "argment", "id": "task_id", "name": "task_name", "worker_pid": "worker_pid"}
        with nested(patch("sys.stdout", new_callable=StringIO.StringIO), patch("pgreport.tasks.celery")) as (
            std_mock,
            cel_mock,
        ):
            stat_mock = MagicMock()
            stat_mock.active.return_value = {"queue": [status]}
            cel_mock.control.inspect.return_value = stat_mock
            task.show_task_list()

        self.assertEquals(
            std_mock.getvalue(),
            "*** Active queues ***\nqueue: [\n * Task id: {id}, Task args: {args},  Task name: {name}, Worker pid: {worker_pid}\n]\n".format(
                **status
            ),
        )
    def handle(self, *args, **options):
        """Handle command options."""
        course_id = options['course_id']
        task_id = options['task_id']

        if course_id is not None:
            try:
                course_id = CourseLocator.from_string(course_id)
            except InvalidKeyError:
                raise CommandError(
                    "'{}' is an invalid course_id".format(course_id))
            if not modulestore().get_course(course_id):
                raise CommandError("The specified course does not exist.")

        if len(args) != 1:
            raise CommandError(
                'Required subcommand, create, list, status, revoke or clear_cache.'
            )
        command = args[0]
        task = ProgressReportTask(create_report_task)

        if command == "status":
            if task_id is None:
                raise CommandError('"status" subcommand required task_id.')
            task.show_task_status(task_id)
        elif command == "list":
            task.show_task_list()
        elif command == "revoke":
            if task_id is None:
                raise CommandError('"revoke" subcommand required task_id.')
            task.revoke_task(task_id)
        elif command == "create":
            if course_id is None:
                raise CommandError('"create" subcommand required course_id.')
            task.send_task(course_id)
            state = TaskState("pgreport.tasks.create_report_task", course_id)
            state.delete_task_state()
        elif command == "clear_cache":
            if course_id is None:
                raise CommandError(
                    '"clear_cache" subcommand required course_id.')
            state = TaskState("pgreport.tasks.create_report_task", course_id)
            state.delete_task_state()
        else:
            raise CommandError('Invalid subcommand.')
    def handle(self, *args, **options):
        """Handle command options."""
        course_id = options['course_id']
        task_id = options['task_id']

        if course_id is not None:
            try:
                course_id = CourseLocator.from_string(course_id)
            except InvalidKeyError:
                raise CommandError("'{}' is an invalid course_id".format(course_id))
            if not modulestore().get_course(course_id):
                raise CommandError("The specified course does not exist.")

        if len(args) != 1:
            raise CommandError(
                'Required subcommand, update, list, status, revoke or clear_cache.')
        command = args[0]
        task = ProgressReportTask(update_table_task)

        if command == "status":
            if task_id is None:
                raise CommandError('"status" subcommand required task_id.')
            task.show_task_status(task_id)
        elif command == "list":
            task.show_task_list()
        elif command == "revoke":
            if task_id is None:
                raise CommandError('"revoke" subcommand required task_id.')
            task.revoke_task(task_id)
        elif command == "update":
            if course_id:
                task.send_task(course_id.to_deprecated_string())
            else:
                task.send_tasks()
        elif command == "clear_cache":
            if course_id is None:
                raise CommandError('"clear_cache" subcommand required course_id.')
            state = TaskState("pgreport.tasks.update_table_task", course_id)
            state.delete_task_state()
        else:
            raise CommandError('Invalid subcommand.')