Пример #1
0
    def test_execute_no_job(self):
        """Tests calling SpawnDeleteFilesJob.execute with the id of a job that does not exist"""

        job_type_id = JobType.objects.values_list('id', flat=True).get(name='scale-delete-files')
        job_id = 1234574223462
        # Make the message
        message = create_spawn_delete_files_job(job_id=job_id, trigger_id=self.event.id,
                                                source_file_id=self.file_1.id, purge=True)

        # Capture message that creates job
        result = message.execute()
        self.assertTrue(result)

        # Check that no message was created
        self.assertEqual(len(message.new_messages), 0)
Пример #2
0
    def execute(self):
        """See :meth:`messaging.messages.message.CommandMessage.execute`
        """

        # Check to see if a force stop was placed on this purge process
        results = PurgeResults.objects.get(trigger_event=self.trigger_id)
        if results.force_stop_purge:
            return True

        job_inputs = JobInputFile.objects.filter(
            input_file=self.source_file_id,
            job__recipe__isnull=True).select_related('job')
        recipe_inputs = RecipeInputFile.objects.filter(
            input_file=self.source_file_id,
            recipe__is_superseded=False).select_related('recipe')

        # Kick off spawn_delete_job_files for jobs that are not in a recipe and have the given source_file as input
        for job_input in job_inputs:
            from job.messages.spawn_delete_files_job import create_spawn_delete_files_job
            self.new_messages.append(
                create_spawn_delete_files_job(
                    job_id=job_input.job.id,
                    trigger_id=self.trigger_id,
                    source_file_id=self.source_file_id,
                    purge=True))

        # Kick off purge_recipe for recipes that are not superseded and have the given source_file as input
        for recipe_input in recipe_inputs:
            from recipe.messages.purge_recipe import create_purge_recipe_message
            self.new_messages.append(
                create_purge_recipe_message(
                    recipe_id=recipe_input.recipe.id,
                    trigger_id=self.trigger_id,
                    source_file_id=self.source_file_id))

        # Delete Ingest and ScaleFile
        if not job_inputs and not recipe_inputs:
            Ingest.objects.filter(source_file=self.source_file_id).delete()
            ScaleFile.objects.filter(id=self.source_file_id).delete()

            # Update results
            results.purge_completed = timezone.now()
            results.save()

        return True
Пример #3
0
    def test_execute(self):
        """Tests calling SpawnDeleteFilesJob.execute successfully"""

        job_type_id = JobType.objects.values_list('id', flat=True).get(name='scale-delete-files')

        # Make the message
        message = create_spawn_delete_files_job(job_id=self.job.pk, trigger_id=self.event.id,
                                                source_file_id=self.file_1.id, purge=True)

        # Capture message that creates job
        result = message.execute()
        self.assertTrue(result)

        for msg in message.new_messages:
            msg.execute()

        # Check that job is created
        self.assertEqual(Job.objects.filter(job_type_id=job_type_id, event_id=self.event.id).count(), self.count)
Пример #4
0
    def test_execute_force_stop(self):
        """Tests calling SpawnDeleteFilesJob.execute with the force stop flag set"""

        file_2 = storage_test_utils.create_file(file_type='SOURCE')
        trigger = trigger_test_utils.create_trigger_event()
        PurgeResults.objects.create(source_file_id=file_2.id, trigger_event=trigger, force_stop_purge=True)

        job_type_id = JobType.objects.values_list('id', flat=True).get(name='scale-delete-files')
        job_id = 1234574223462
        # Make the message
        message = create_spawn_delete_files_job(job_id=job_id, trigger_id=trigger.id,
                                                source_file_id=self.file_1.id, purge=True)

        # Capture message that creates job
        result = message.execute()
        self.assertTrue(result)

        # Check that no message was created
        self.assertEqual(len(message.new_messages), 0)
Пример #5
0
    def test_json(self):
        """Tests coverting a SpawnDeleteFilesJob message to and from JSON"""

        # Make the message
        message = create_spawn_delete_files_job(job_id=self.job.pk, trigger_id=self.event.id,
                                                source_file_id=self.file_1.id, purge=True)

        # Convert message to JSON and back, and then execute
        message_json_dict = message.to_json()
        new_message = SpawnDeleteFilesJob.from_json(message_json_dict)
        result = new_message.execute()
        self.assertTrue(result)
        self.assertEqual(new_message.job_id, self.job.pk)
        self.assertEqual(new_message.trigger_id, self.event.id)

        # Check for create_jobs messages
        self.assertEqual(len(new_message.new_messages), self.count)
        for msg in new_message.new_messages:
            self.assertEqual(msg.type, 'create_jobs')
Пример #6
0
    def execute(self):
        """See :meth:`messaging.messages.message.CommandMessage.execute`
        """

        job_inputs = JobInputFile.objects.filter(
            input_file=self.source_file_id,
            job__recipe__isnull=True).select_related('job')
        recipe_inputs = RecipeInputFile.objects.filter(
            input_file=self.source_file_id,
            recipe__is_superseded=False).select_related('recipe')

        # Kick off spawn_delete_job_files for jobs that are not in a recipe and have the given source_file as input
        for job_input in job_inputs:
            from job.messages.spawn_delete_files_job import create_spawn_delete_files_job
            self.new_messages.append(
                create_spawn_delete_files_job(
                    job_id=job_input.job.id,
                    trigger_id=self.trigger_id,
                    source_file_id=self.source_file_id,
                    purge=True))

        # Kick off purge_recipe for recipes that are not superseded and have the given source_file as input
        for recipe_input in recipe_inputs:
            from recipe.messages.purge_recipe import create_purge_recipe_message
            self.new_messages.append(
                create_purge_recipe_message(
                    recipe_id=recipe_input.recipe.id,
                    trigger_id=self.trigger_id,
                    source_file_id=self.source_file_id))

        # Delete Ingest and ScaleFile
        if not job_inputs and not recipe_inputs:
            Ingest.objects.filter(source_file=self.source_file_id).delete()
            ScaleFile.objects.filter(id=self.source_file_id).delete()

        return True
Пример #7
0
    def execute(self):
        """See :meth:`messaging.messages.message.CommandMessage.execute`
        """

        # Check to see if a force stop was placed on this purge process
        results = PurgeResults.objects.get(trigger_event=self.trigger_id)
        if results.force_stop_purge:
            return True

        recipe = Recipe.objects.select_related('superseded_recipe').get(
            id=self.recipe_id)

        # Kick off purge_source_file for the source file
        self.new_messages.append(
            create_purge_source_file_message(
                source_file_id=self.source_file_id,
                trigger_id=self.trigger_id))

        recipe_inst = Recipe.objects.get_recipe_instance(self.recipe_id)
        recipe_nodes = recipe_inst.get_original_leaf_nodes(
        )  # {Node_Name: Node}
        parent_recipes = RecipeNode.objects.filter(sub_recipe=recipe,
                                                   is_original=True)
        if recipe_nodes:
            # Kick off a delete_files job for leaf node jobs
            leaf_jobs = [
                node for node in recipe_nodes.values()
                if node.node_type == JobNodeDefinition.NODE_TYPE
            ]
            for node in leaf_jobs:
                self.new_messages.append(
                    create_spawn_delete_files_job(
                        job_id=node.job.id,
                        trigger_id=self.trigger_id,
                        source_file_id=self.source_file_id,
                        purge=True))

            # Kick off a purge_recipe for leaf node recipes
            leaf_recipes = [
                node for node in recipe_nodes.values()
                if node.node_type == RecipeNodeDefinition.NODE_TYPE
            ]
            for node in leaf_recipes:
                self.new_messages.append(
                    create_purge_recipe_message(
                        recipe_id=node.recipe.id,
                        trigger_id=self.trigger_id,
                        source_file_id=self.source_file_id))
        else:
            # Kick off a purge_recipe for a parent recipe
            if parent_recipes:
                for parent_recipe in parent_recipes:
                    self.new_messages.append(
                        create_purge_recipe_message(
                            recipe_id=parent_recipe.recipe.id,
                            trigger_id=self.trigger_id,
                            source_file_id=self.source_file_id))
                    RecipeNode.objects.filter(sub_recipe=recipe).delete()

            # Kick off purge_recipe for a superseded recipe
            elif recipe.superseded_recipe:
                self.new_messages.append(
                    create_purge_recipe_message(
                        recipe_id=recipe.superseded_recipe.id,
                        trigger_id=self.trigger_id,
                        source_file_id=self.source_file_id))

            # Delete RecipeNode, RecipeInputFile, and Recipe
            RecipeNode.objects.filter(Q(recipe=recipe)
                                      | Q(sub_recipe=recipe)).delete()
            RecipeInputFile.objects.filter(recipe=recipe).delete()
            recipe.delete()

            # Update results
            PurgeResults.objects.filter(trigger_event=self.trigger_id).update(
                num_recipes_deleted=F('num_recipes_deleted') + 1)

        return True