Beispiel #1
0
    def handle(self, *args: Any, **options: Any) -> None:
        """Run the do_command method and log the output."""
        self.no_truncate = options["no_truncate"]
        disabled_signals = MODEL_SIGNALS if options[
            "disable_model_signals"] else ()

        log = ManagementCommandLog(
            app_name=self.app_name,
            command_name=self.command_name,
            truncate_at=self.truncate_at,
        )
        log.start()

        try:
            # Note: only disable signals during self.do_command():
            # --disable-model-signals should leave any signals generated by
            # ManagementCommandLog intact.
            with disable_signals(disabled_signals):
                output = self.do_command(*args, **options)
            log.stop(output=str(output), exit_code=log.EXIT_CODE_SUCCESS)
        except PartialCompletionError as ex:
            logger.warning("Command partially completed")
            log.stop(output=ex.output,
                     exit_code=log.EXIT_CODE_PARTIAL,
                     error=ex)
        except Exception as ex:  # noqa: B902
            logger.exception("Error running management command: %s", log)
            log.stop(output="", exit_code=log.EXIT_CODE_FAILURE, error=ex)
Beispiel #2
0
 def test_signals_reenabled_on_exception(self, signal: Signal) -> None:
     try:
         with disable_signals([signal]):
             raise ValueError("test")
     except ValueError:
         pass
     signal.send(sender=None, message="re-enabled")
     assert self.messages == ["re-enabled"]
Beispiel #3
0
    def test_other_signals_always_enabled(self, signal: Signal) -> None:
        self.other_message = None

        def listener(signal: Signal, sender, message: str) -> None:
            self.other_message = message

        other_signal = Signal()
        other_signal.connect(listener)

        with disable_signals([signal]):
            other_signal.send(sender=None, message="always_enabled")

        assert len(self.messages) == 0
        assert self.other_message == "always_enabled"
Beispiel #4
0
 def test_signals_reenabled_after(self, signal: Signal) -> None:
     assert len(self.messages) == 0
     with disable_signals([signal]):
         pass
     signal.send(sender=None, message="re-enabled")
     assert self.messages == ["re-enabled"]
Beispiel #5
0
 def test_signals_disabled_during(self, signal: Signal) -> None:
     assert len(self.messages) == 0
     with disable_signals([signal]):
         signal.send(sender=None, message="disabled")
     assert len(self.messages) == 0