Exemple #1
0
 def test_task_input_valid_length(self):
     """
     Test allowed length of task_input field
     """
     task_input = 's' * TASK_INPUT_LENGTH
     with pytest.raises(AttributeError):
         InstructorTask.create(
             course_id='dummy_course_id',
             task_type='dummy type',
             task_key='dummy key',
             task_input=task_input,
             requester='dummy requester',
         )
Exemple #2
0
def _reserve_task(course_id, task_type, task_key, task_input, requester):
    """
    Creates a database entry to indicate that a task is in progress.

    Throws AlreadyRunningError if the task is already in progress.
    Includes the creation of an arbitrary value for task_id, to be
    submitted with the task call to celery.

    Note that there is a chance of a race condition here, when two users
    try to run the same task at almost exactly the same time.  One user
    could be after the check and before the create when the second user
    gets to the check.  At that point, both users are able to run their
    tasks simultaneously.  This is deemed a small enough risk to not
    put in further safeguards.
    """

    if _task_is_running(course_id, task_type, task_key):
        log.warning("Duplicate task found for task_type %s and task_key %s", task_type, task_key)
        error_message = generate_already_running_error_message(task_type)
        raise AlreadyRunningError(error_message)

    try:
        most_recent_id = InstructorTask.objects.latest('id').id
    except InstructorTask.DoesNotExist:
        most_recent_id = "None found"
    finally:
        log.warning(
            "No duplicate tasks found: task_type %s, task_key %s, and most recent task_id = %s",
            task_type,
            task_key,
            most_recent_id
        )

    # Create log entry now, so that future requests will know it's running.
    return InstructorTask.create(course_id, task_type, task_key, task_input, requester)
Exemple #3
0
def _reserve_task(course_id, task_type, task_key, task_input, requester):
    """
    Creates a database entry to indicate that a task is in progress.

    Throws AlreadyRunningError if the task is already in progress.
    Includes the creation of an arbitrary value for task_id, to be
    submitted with the task call to celery.

    Note that there is a chance of a race condition here, when two users
    try to run the same task at almost exactly the same time.  One user
    could be after the check and before the create when the second user
    gets to the check.  At that point, both users are able to run their
    tasks simultaneously.  This is deemed a small enough risk to not
    put in further safeguards.
    """

    if _task_is_running(course_id, task_type, task_key):
        log.warning("Duplicate task found for task_type %s and task_key %s",
                    task_type, task_key)
        error_message = generate_already_running_error_message(task_type)
        raise AlreadyRunningError(error_message)

    try:
        most_recent_id = InstructorTask.objects.latest('id').id
    except InstructorTask.DoesNotExist:
        most_recent_id = "None found"
    finally:
        log.warning(
            "No duplicate tasks found: task_type %s, task_key %s, and most recent task_id = %s",
            task_type, task_key, most_recent_id)

    # Create log entry now, so that future requests will know it's running.
    return InstructorTask.create(course_id, task_type, task_key, task_input,
                                 requester)
Exemple #4
0
 def test_send_email_retried_subtask(self):
     # test at a lower level, to ensure that the course gets checked down below too.
     entry = InstructorTask.create(self.course.id, "task_type", "task_key",
                                   "task_input", self.instructor)
     entry_id = entry.id
     subtask_id = "subtask-id-value"
     initialize_subtask_info(entry, "emailed", 100, [subtask_id])
     subtask_status = SubtaskStatus.create(subtask_id,
                                           state=RETRY,
                                           retried_nomax=2)
     update_subtask_status(entry_id, subtask_id, subtask_status)
     bogus_email_id = 1001
     to_list = ['*****@*****.**']
     global_email_context = {'course_title': 'dummy course'}
     # try running with a clean subtask:
     new_subtask_status = SubtaskStatus.create(subtask_id)
     with self.assertRaisesRegex(DuplicateTaskException, 'already retried'):
         send_course_email(entry_id, bogus_email_id,
                           to_list, global_email_context,
                           new_subtask_status.to_dict())
     # try again, with a retried subtask with lower count:
     new_subtask_status = SubtaskStatus.create(subtask_id,
                                               state=RETRY,
                                               retried_nomax=1)
     with self.assertRaisesRegex(DuplicateTaskException, 'already retried'):
         send_course_email(entry_id, bogus_email_id,
                           to_list, global_email_context,
                           new_subtask_status.to_dict())
 def test_send_email_undefined_subtask(self):
     # test at a lower level, to ensure that the course gets checked down below too.
     entry = InstructorTask.create(self.course.id, "task_type", "task_key", "task_input", self.instructor)
     entry_id = entry.id
     to_list = ['*****@*****.**']
     global_email_context = {'course_title': 'dummy course'}
     subtask_id = "subtask-id-value"
     subtask_status = SubtaskStatus.create(subtask_id)
     email_id = 1001
     with self.assertRaisesRegexp(DuplicateTaskException, 'unable to find subtasks of instructor task'):
         send_course_email(entry_id, email_id, to_list, global_email_context, subtask_status.to_dict())
 def test_send_email_undefined_subtask(self):
     # test at a lower level, to ensure that the course gets checked down below too.
     entry = InstructorTask.create(self.course.id, "task_type", "task_key", "task_input", self.instructor)
     entry_id = entry.id
     to_list = ['*****@*****.**']
     global_email_context = {'course_title': 'dummy course'}
     subtask_id = "subtask-id-value"
     subtask_status = SubtaskStatus.create(subtask_id)
     email_id = 1001
     with self.assertRaisesRegexp(DuplicateTaskException, 'unable to find subtasks of instructor task'):
         send_course_email(entry_id, email_id, to_list, global_email_context, subtask_status.to_dict())
 def test_nonexistent_course(self):
     """
     Tests exception when the course in the email doesn't exist
     """
     course_id = SlashSeparatedCourseKey("I", "DONT", "EXIST")
     email = CourseEmail(course_id=course_id)
     email.save()
     entry = InstructorTask.create(course_id, "task_type", "task_key", "task_input", self.instructor)
     task_input = {"email_id": email.id}
     # (?i) is a regex for ignore case
     with self.assertRaisesRegexp(ValueError, r"(?i)course not found"):
         perform_delegate_email_batches(entry.id, course_id, task_input, "action_name")
 def test_nonexistent_course(self):
     """
     Tests exception when the course in the email doesn't exist
     """
     course_id = CourseLocator("I", "DONT", "EXIST")
     email = CourseEmail(course_id=course_id)
     email.save()
     entry = InstructorTask.create(course_id, "task_type", "task_key", "task_input", self.instructor)
     task_input = {"email_id": email.id}
     # (?i) is a regex for ignore case
     with self.assertRaisesRegexp(ValueError, r"(?i)course not found"):
         perform_delegate_email_batches(entry.id, course_id, task_input, "action_name")
 def test_send_email_missing_subtask(self):
     # test at a lower level, to ensure that the course gets checked down below too.
     entry = InstructorTask.create(self.course.id, "task_type", "task_key", "task_input", self.instructor)
     entry_id = entry.id
     to_list = ['*****@*****.**']
     global_email_context = {'course_title': 'dummy course'}
     subtask_id = "subtask-id-value"
     initialize_subtask_info(entry, "emailed", 100, [subtask_id])
     different_subtask_id = "bogus-subtask-id-value"
     subtask_status = SubtaskStatus.create(different_subtask_id)
     bogus_email_id = 1001
     with self.assertRaisesRegexp(DuplicateTaskException, 'unable to find status for subtask of instructor task'):
         send_course_email(entry_id, bogus_email_id, to_list, global_email_context, subtask_status.to_dict())
Exemple #10
0
 def test_nonexistent_course(self):
     """
     Tests exception when the course in the email doesn't exist
     """
     course_id = CourseLocator("I", "DONT", "EXIST")
     email = CourseEmail(course_id=course_id)
     email.save()
     entry = InstructorTask.create(course_id, "task_type", "task_key",
                                   "task_input", self.instructor)
     task_input = {"email_id": email.id}
     with pytest.raises(CourseRunNotFound):
         perform_delegate_email_batches(entry.id, course_id, task_input,
                                        "action_name")
Exemple #11
0
 def test_wrong_course_id_in_email(self):
     """
     Tests exception when the course_id in CourseEmail is not the same as one explicitly passed in.
     """
     email = CourseEmail.create(CourseLocator("bogus", "course", "id"),
                                self.instructor, [SEND_TO_MYSELF],
                                "re: subject", "dummy body goes here")
     entry = InstructorTask.create(self.course.id, "task_type", "task_key",
                                   "task_input", self.instructor)
     task_input = {"email_id": email.id}
     with self.assertRaisesRegex(ValueError, 'does not match email value'):
         perform_delegate_email_batches(entry.id, self.course.id,
                                        task_input, "action_name")
 def test_send_email_missing_subtask(self):
     # test at a lower level, to ensure that the course gets checked down below too.
     entry = InstructorTask.create(self.course.id, "task_type", "task_key", "task_input", self.instructor)
     entry_id = entry.id
     to_list = ['*****@*****.**']
     global_email_context = {'course_title': 'dummy course'}
     subtask_id = "subtask-id-value"
     initialize_subtask_info(entry, "emailed", 100, [subtask_id])
     different_subtask_id = "bogus-subtask-id-value"
     subtask_status = SubtaskStatus.create(different_subtask_id)
     bogus_email_id = 1001
     with self.assertRaisesRegexp(DuplicateTaskException, 'unable to find status for subtask of instructor task'):
         send_course_email(entry_id, bogus_email_id, to_list, global_email_context, subtask_status.to_dict())
 def test_send_email_running_subtask(self):
     # test at a lower level, to ensure that the course gets checked down below too.
     entry = InstructorTask.create(self.course.id, "task_type", "task_key", "task_input", self.instructor)
     entry_id = entry.id
     subtask_id = "subtask-id-value"
     initialize_subtask_info(entry, "emailed", 100, [subtask_id])
     subtask_status = SubtaskStatus.create(subtask_id)
     update_subtask_status(entry_id, subtask_id, subtask_status)
     check_subtask_is_valid(entry_id, subtask_id, subtask_status)
     bogus_email_id = 1001
     to_list = ['*****@*****.**']
     global_email_context = {'course_title': 'dummy course'}
     with self.assertRaisesRegexp(DuplicateTaskException, 'already being executed'):
         send_course_email(entry_id, bogus_email_id, to_list, global_email_context, subtask_status.to_dict())
Exemple #14
0
 def test_send_email_running_subtask(self):
     # test at a lower level, to ensure that the course gets checked down below too.
     entry = InstructorTask.create(self.course.id, "task_type", "task_key", "task_input", self.instructor)
     entry_id = entry.id
     subtask_id = "subtask-id-value"
     initialize_subtask_info(entry, "emailed", 100, [subtask_id])
     subtask_status = SubtaskStatus.create(subtask_id)
     update_subtask_status(entry_id, subtask_id, subtask_status)
     check_subtask_is_valid(entry_id, subtask_id, subtask_status)
     bogus_email_id = 1001
     to_list = ['*****@*****.**']
     global_email_context = {'course_title': 'dummy course'}
     with self.assertRaisesRegexp(DuplicateTaskException, 'already being executed'):
         send_course_email(entry_id, bogus_email_id, to_list, global_email_context, subtask_status.to_dict())
 def test_send_email_undefined_email(self):
     # test at a lower level, to ensure that the course gets checked down below too.
     entry = InstructorTask.create(self.course.id, "task_type", "task_key", "task_input", self.instructor)
     entry_id = entry.id
     to_list = ['*****@*****.**']
     global_email_context = {'course_title': 'dummy course'}
     subtask_id = "subtask-id-undefined-email"
     initialize_subtask_info(entry, "emailed", 100, [subtask_id])
     subtask_status = SubtaskStatus.create(subtask_id)
     bogus_email_id = 1001
     with self.assertRaises(CourseEmail.DoesNotExist):
         # we skip the call that updates subtask status, since we've not set up the InstructorTask
         # for the subtask, and it's not important to the test.
         with patch('bulk_email.tasks.update_subtask_status'):
             send_course_email(entry_id, bogus_email_id, to_list, global_email_context, subtask_status.to_dict())
 def test_send_email_with_locked_instructor_task(self):
     # test at a lower level, to ensure that the course gets checked down below too.
     entry = InstructorTask.create(self.course.id, "task_type", "task_key", "task_input", self.instructor)
     entry_id = entry.id
     subtask_id = "subtask-id-locked-model"
     initialize_subtask_info(entry, "emailed", 100, [subtask_id])
     subtask_status = SubtaskStatus.create(subtask_id)
     bogus_email_id = 1001
     to_list = ['*****@*****.**']
     global_email_context = {'course_title': 'dummy course'}
     with patch('lms.djangoapps.instructor_task.subtasks.InstructorTask.save') as mock_task_save:
         mock_task_save.side_effect = DatabaseError
         with self.assertRaises(DatabaseError):
             send_course_email(entry_id, bogus_email_id, to_list, global_email_context, subtask_status.to_dict())
         self.assertEquals(mock_task_save.call_count, MAX_DATABASE_LOCK_RETRIES)
Exemple #17
0
 def test_wrong_course_id_in_email(self):
     """
     Tests exception when the course_id in CourseEmail is not the same as one explicitly passed in.
     """
     email = CourseEmail.create(
         CourseLocator("bogus", "course", "id"),
         self.instructor,
         [SEND_TO_MYSELF],
         "re: subject",
         "dummy body goes here"
     )
     entry = InstructorTask.create(self.course.id, "task_type", "task_key", "task_input", self.instructor)
     task_input = {"email_id": email.id}
     with self.assertRaisesRegexp(ValueError, 'does not match email value'):
         perform_delegate_email_batches(entry.id, self.course.id, task_input, "action_name")
 def test_nonexistent_email(self, mock_log, result):
     """
     Tests retries when the email doesn't exist
     """
     # create an InstructorTask object to pass through
     course_id = self.course.id
     entry = InstructorTask.create(course_id, "task_type", "task_key", "task_input", self.instructor)
     task_input = {"email_id": -1}
     with self.assertRaises(CourseEmail.DoesNotExist):
         perform_delegate_email_batches(entry.id, course_id, task_input, "action_name")
     ((log_str, __, email_id), __) = mock_log.warning.call_args
     self.assertTrue(mock_log.warning.called)
     self.assertIn('Failed to get CourseEmail with id', log_str)
     self.assertEqual(email_id, -1)
     self.assertFalse(result.called)
Exemple #19
0
 def test_nonexistent_email(self, mock_log, result):
     """
     Tests retries when the email doesn't exist
     """
     # create an InstructorTask object to pass through
     course_id = self.course.id
     entry = InstructorTask.create(course_id, "task_type", "task_key", "task_input", self.instructor)
     task_input = {"email_id": -1}
     with self.assertRaises(CourseEmail.DoesNotExist):
         perform_delegate_email_batches(entry.id, course_id, task_input, "action_name")
     ((log_str, __, email_id), __) = mock_log.warning.call_args
     self.assertTrue(mock_log.warning.called)
     self.assertIn('Failed to get CourseEmail with id', log_str)
     self.assertEqual(email_id, -1)
     self.assertFalse(result.called)
Exemple #20
0
 def test_send_email_undefined_email(self):
     # test at a lower level, to ensure that the course gets checked down below too.
     entry = InstructorTask.create(self.course.id, "task_type", "task_key", "task_input", self.instructor)
     entry_id = entry.id
     to_list = ['*****@*****.**']
     global_email_context = {'course_title': 'dummy course'}
     subtask_id = "subtask-id-undefined-email"
     initialize_subtask_info(entry, "emailed", 100, [subtask_id])
     subtask_status = SubtaskStatus.create(subtask_id)
     bogus_email_id = 1001
     with self.assertRaises(CourseEmail.DoesNotExist):
         # we skip the call that updates subtask status, since we've not set up the InstructorTask
         # for the subtask, and it's not important to the test.
         with patch('bulk_email.tasks.update_subtask_status'):
             send_course_email(entry_id, bogus_email_id, to_list, global_email_context, subtask_status.to_dict())
Exemple #21
0
 def test_send_email_with_locked_instructor_task(self):
     # test at a lower level, to ensure that the course gets checked down below too.
     entry = InstructorTask.create(self.course.id, "task_type", "task_key", "task_input", self.instructor)
     entry_id = entry.id
     subtask_id = "subtask-id-locked-model"
     initialize_subtask_info(entry, "emailed", 100, [subtask_id])
     subtask_status = SubtaskStatus.create(subtask_id)
     bogus_email_id = 1001
     to_list = ['*****@*****.**']
     global_email_context = {'course_title': 'dummy course'}
     with patch('lms.djangoapps.instructor_task.subtasks.InstructorTask.save') as mock_task_save:
         mock_task_save.side_effect = DatabaseError
         with self.assertRaises(DatabaseError):
             send_course_email(entry_id, bogus_email_id, to_list, global_email_context, subtask_status.to_dict())
         self.assertEquals(mock_task_save.call_count, MAX_DATABASE_LOCK_RETRIES)
Exemple #22
0
def schedule_task(request, task_type, course_key, task_input, task_key,
                  schedule):
    """
    Helper function to schedule a background task.

    Reserves the requested task and stores it until the task is ready for execution. We also create an instance of a
    InstructorTaskSchedule object responsible for maintaining the details of _when_ a task should be executed. Extracts
    arguments important to the task from the originating server request and stores them as part of the schedule object.
    Sets the `task_status` to SCHEDULED to indicate this task will be executed in the future.

    Args:
        request (WSGIRequest): The originating web request associated with this task request.
        task_type (String): Text describing the type of task (e.g. 'bulk_course_email' or 'grade_course')
        course_key (CourseKey): The CourseKey of the course-run the task belongs to.
        task_input (dict): Task input arguments stores as JSON-serialized dictionary.
        task_key (String): Encoded input arguments used during task execution.
        schedule (DateTime): DateTime (in UTC) describing when the task should be executed.
    """
    instructor_task = None
    try:
        log.info(
            f"Creating a scheduled instructor task of type '{task_type}' for course '{course_key}' requested by "
            f"user with id '{request.user.id}'")
        instructor_task = InstructorTask.create(course_key, task_type,
                                                task_key, task_input,
                                                request.user)

        task_id = instructor_task.task_id
        task_args = _get_xmodule_instance_args(request, task_id)
        log.info(
            f"Creating a task schedule associated with instructor task '{instructor_task.id}' and due after "
            f"'{schedule}'")
        InstructorTaskSchedule.objects.create(
            task=instructor_task,
            task_args=json.dumps(task_args),
            task_due=schedule,
        )

        log.info(
            f"Updating task state of instructor task '{instructor_task.id}' to '{SCHEDULED}'"
        )
        instructor_task.task_state = SCHEDULED
        instructor_task.save()
    except Exception as error:  # pylint: disable=broad-except
        log.error(f"Error occurred during task or schedule creation: {error}")
        # Set any orphaned instructor tasks to the FAILURE state.
        if instructor_task:
            _handle_instructor_task_failure(instructor_task, error)
Exemple #23
0
 def test_send_email_retried_subtask(self):
     # test at a lower level, to ensure that the course gets checked down below too.
     entry = InstructorTask.create(self.course.id, "task_type", "task_key", "task_input", self.instructor)
     entry_id = entry.id
     subtask_id = "subtask-id-value"
     initialize_subtask_info(entry, "emailed", 100, [subtask_id])
     subtask_status = SubtaskStatus.create(subtask_id, state=RETRY, retried_nomax=2)
     update_subtask_status(entry_id, subtask_id, subtask_status)
     bogus_email_id = 1001
     to_list = ['*****@*****.**']
     global_email_context = {'course_title': 'dummy course'}
     # try running with a clean subtask:
     new_subtask_status = SubtaskStatus.create(subtask_id)
     with self.assertRaisesRegexp(DuplicateTaskException, 'already retried'):
         send_course_email(entry_id, bogus_email_id, to_list, global_email_context, new_subtask_status.to_dict())
     # try again, with a retried subtask with lower count:
     new_subtask_status = SubtaskStatus.create(subtask_id, state=RETRY, retried_nomax=1)
     with self.assertRaisesRegexp(DuplicateTaskException, 'already retried'):
         send_course_email(entry_id, bogus_email_id, to_list, global_email_context, new_subtask_status.to_dict())