print_control.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. """Stopping a print on a user's behalf.
  2. Every place a user can stop a print goes through here, so none can send the
  3. stop without the "stopped by the user" mark: the printer card, the queue and
  4. the webhook routes call stop_print_by_user; the HMS error dialog's "Stop
  5. Printing" sends its own stop payload and calls mark_stopped_by_user. Without
  6. the mark the printer's resulting "failed"/"aborted" status is taken at face
  7. value: a "print failed" notification goes out, and on some printers the
  8. cancel sequence's own error is reported as a fault such as a layer shift.
  9. """
  10. import logging
  11. from backend.app.services.printer_manager import printer_manager
  12. logger = logging.getLogger(__name__)
  13. def mark_stopped_by_user(printer_id: int) -> None:
  14. """Record that the user stopped the print on this printer (see module docstring)."""
  15. try:
  16. from backend.app.main import mark_printer_stopped_by_user
  17. mark_printer_stopped_by_user(printer_id)
  18. except Exception as e:
  19. logger.warning("Failed to mark printer %s as user-stopped: %s", printer_id, e)
  20. def stop_print_by_user(printer_id: int, *, mark_when_unsent: bool = False) -> bool:
  21. """Send the stop command; returns whether it was sent.
  22. The printer is marked as stopped by the user when the command was sent,
  23. or always with ``mark_when_unsent`` (the queue, which closes the job even
  24. when the printer is offline, so a "failed" it reports later still reads
  25. as the user's cancel). Errors sending the command propagate to the caller.
  26. """
  27. try:
  28. sent = printer_manager.stop_print(printer_id)
  29. except Exception:
  30. if mark_when_unsent:
  31. mark_stopped_by_user(printer_id)
  32. raise
  33. if sent or mark_when_unsent:
  34. mark_stopped_by_user(printer_id)
  35. return sent