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 test_send_task(self, ts_mock): task = ProgressReportTask(self.func_mock) with patch("sys.stdout", new_callable=StringIO.StringIO) as std_mock: ts_mock().is_active = False task.send_task(self.course1.id) ts_mock.assert_called_with(task.task_name, self.course1.id) ts_mock().set_task_state.assert_called_once_with() self.func_mock.apply_async.assert_called_with( args=(ANY, unicode(self.course1.id)), task_id=ANY, expires=23.5 * 60 * 60, retry=False ) self.assertEquals(std_mock.getvalue(), "Send task (task_id: %s)\n" % (self.func_mock.apply_async().id)) with patch("sys.stdout", new_callable=StringIO.StringIO) as std_mock: ts_mock().is_active = True task.send_task(self.course1.id) self.assertEquals( std_mock.getvalue(), "Task is already running. (%s, %s)\n" % (task.task_name, self.course1.id) )
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.')
def test_send_task(self, ts_mock): task = ProgressReportTask(self.func_mock) with patch('sys.stdout', new_callable=StringIO.StringIO) as std_mock: ts_mock().is_active = False task.send_task(self.course1.id) ts_mock.assert_called_with(task.task_name, self.course1.id) ts_mock().set_task_state.assert_called_once_with() self.func_mock.apply_async.assert_called_with( args=(ANY, unicode(self.course1.id)), task_id=ANY, expires=23.5*60*60, retry=False ) self.assertEquals(std_mock.getvalue(), "Send task (task_id: %s)\n" % (self.func_mock.apply_async().id) ) with patch('sys.stdout', new_callable=StringIO.StringIO) as std_mock: ts_mock().is_active = True task.send_task(self.course1.id) self.assertEquals(std_mock.getvalue(), "Task is already running. (%s, %s)\n" % (task.task_name, self.course1.id) )