test_forget_printer.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. """What Bambuddy keeps per printer id goes when the printer is deleted.
  2. SQLite used to give a deleted printer's id to the next printer created, which
  3. inherited it all: the old print's owner (its prints passed the kill switch and
  4. were billed to that user), the plate-clear gate, pending notifications, the old
  5. camera feed, the old printer's plate-detection references. Ids are no longer
  6. reused, but nothing kept for a printer that is gone should stay.
  7. """
  8. import ast
  9. import asyncio
  10. from pathlib import Path
  11. import pytest
  12. from backend.app import main
  13. pytestmark = pytest.mark.asyncio
  14. GONE, KEPT = 5, 6
  15. # Module-level maps in main.py that are not keyed by a printer, or are kept on
  16. # purpose. Anything else main.py adds must be forgotten with the printer.
  17. NOT_FORGOTTEN = {
  18. "_print_ams_mappings": "keyed by archive (dropped with the printer's expected/active prints)",
  19. "_print_cost_center_ids": "keyed by archive (dropped with the printer's expected/active prints)",
  20. "_print_plate_ids": "keyed by archive (dropped with the printer's expected/active prints)",
  21. "_owned_print_archive_ids": "keyed by archive (dropped with the printer's expected/active prints)",
  22. "_ams_assignment_locks": "a lock carries nothing over; popping one a task holds would let a second be made",
  23. "_fallback_recovery_locks": "a lock carries nothing over (see its comment on popping)",
  24. "_HMS_FAILURE_REASONS": "constant",
  25. "_ACTIVE_PRINT_STATES": "constant",
  26. "_SENSOR_TARGET_KEYS": "constant",
  27. "PUBLIC_API_ROUTES": "constant",
  28. "_HTML_CACHE_HEADERS": "constant",
  29. }
  30. def _module_maps() -> set[str]:
  31. """Names of main.py's module-level dicts and sets."""
  32. tree = ast.parse(Path(main.__file__).read_text())
  33. names = set()
  34. for node in tree.body:
  35. if isinstance(node, ast.AnnAssign) and isinstance(node.target, ast.Name):
  36. kind = ast.unparse(node.annotation)
  37. if kind.startswith(("dict", "set")):
  38. names.add(node.target.id)
  39. elif isinstance(node, ast.Assign) and len(node.targets) == 1 and isinstance(node.targets[0], ast.Name):
  40. if isinstance(node.value, (ast.Dict, ast.Set)):
  41. names.add(node.targets[0].id)
  42. return names
  43. def _sleeper() -> asyncio.Task:
  44. return asyncio.ensure_future(asyncio.sleep(3600))
  45. def _entries(printer_id: int) -> dict[str, tuple]:
  46. """For each forgotten map: (key, value) for this printer."""
  47. return {
  48. "_active_prints": ((printer_id, "cube.3mf"), 100 + printer_id),
  49. "_expected_prints": ((printer_id, "cube.3mf"), 200 + printer_id),
  50. "_expected_print_creators": ((printer_id, "cube.3mf"), 1),
  51. "_expected_print_registered_at": ((printer_id, "cube.3mf"), 1.0),
  52. "_stage22_finish_frames": (printer_id, b"jpg"),
  53. "_stage22_finish_in_flight": (printer_id, asyncio.Event()),
  54. "_inprint_frame_bank": (printer_id, b"jpg"),
  55. "_inprint_frame_bank_ts": (printer_id, 1.0),
  56. "_printer_reconciled_since_connect": (printer_id, True),
  57. "_printer_kprofiles_primed_since_connect": (printer_id, True),
  58. "_last_progress_milestone": (printer_id, 50),
  59. "_first_layer_notified": (printer_id, True),
  60. "_unauthorized_print_kill_sent": (printer_id, None),
  61. "_kill_switch_notification_tasks": (printer_id, _sleeper()),
  62. "_notified_hms_errors": (printer_id, {"0300_0001": 1.0}),
  63. "_hms_print_state": (printer_id, "RUNNING"),
  64. "_hms_forget_when_gone": (printer_id, {"0300_0001"}),
  65. "_timelapse_baselines": (printer_id, {"video.mp4"}),
  66. "_bed_cool_waiters": (printer_id, {"threshold": 35.0, "filename": "cube", "registered_at": 1.0}),
  67. "_user_stopped_printers": (printer_id, None),
  68. "_kill_switch_stopped": (printer_id, None),
  69. "_printer_last_connected": (printer_id, True),
  70. "_printer_offline_published": (printer_id, None),
  71. "_printer_offline_notify_tasks": (printer_id, _sleeper()),
  72. "_unknown_tag_last_broadcast": (printer_id, {(0, 0): ("uid", "x")}),
  73. "_last_status_broadcast": (printer_id, "hash"),
  74. "_nozzle_count_updated": (printer_id, None),
  75. "_runtime_seen_connected": (printer_id, None),
  76. "_fallback_3mf_retry_tasks": (printer_id, _sleeper()),
  77. "_connection_watchdog_last_attempt": (printer_id, 1.0),
  78. "_ams_index_only_logged": ((printer_id, 0), None),
  79. "_ams_alarm_cooldown": (f"{printer_id}:0:humidity", None),
  80. }
  81. def _put(name: str, key, value) -> None:
  82. target = getattr(main, name)
  83. if isinstance(target, set):
  84. target.add(key)
  85. else:
  86. target[key] = value
  87. def _has(name: str, key) -> bool:
  88. return key in getattr(main, name)
  89. async def test_every_printer_keyed_map_in_main_is_forgotten():
  90. covered = set(_entries(GONE))
  91. unclassified = _module_maps() - covered - set(NOT_FORGOTTEN)
  92. assert not unclassified, (
  93. f"main.py maps neither forgotten with a deleted printer nor listed in NOT_FORGOTTEN: {sorted(unclassified)}"
  94. )
  95. async def test_forget_printer_clears_main_and_keeps_other_printers():
  96. gone, kept = _entries(GONE), _entries(KEPT)
  97. for name in gone:
  98. _put(name, *gone[name])
  99. _put(name, *kept[name])
  100. main._print_ams_mappings[100 + GONE] = [0]
  101. main._print_cost_center_ids[200 + GONE] = 1
  102. main._print_plate_ids[200 + GONE] = 2
  103. pending = {name: gone[name][1] for name in ("_printer_offline_notify_tasks", "_fallback_3mf_retry_tasks")}
  104. try:
  105. await main.forget_printer(GONE)
  106. left = [name for name in gone if _has(name, gone[name][0])]
  107. assert left == []
  108. lost = [name for name in kept if not _has(name, kept[name][0])]
  109. assert lost == []
  110. assert 100 + GONE not in main._print_ams_mappings
  111. assert 200 + GONE not in main._print_cost_center_ids
  112. assert 200 + GONE not in main._print_plate_ids
  113. await asyncio.sleep(0)
  114. assert all(task.cancelled() for task in pending.values())
  115. finally:
  116. for entries in (gone, kept):
  117. for name, (key, value) in entries.items():
  118. target = getattr(main, name)
  119. if isinstance(target, set):
  120. target.discard(key)
  121. else:
  122. target.pop(key, None)
  123. if isinstance(value, asyncio.Task):
  124. value.cancel()
  125. async def test_forget_printer_clears_the_services():
  126. from backend.app.api.routes import camera
  127. from backend.app.services import (
  128. bambu_ftp,
  129. kprofile_drift,
  130. layer_timelapse,
  131. obico_detection,
  132. print_dispatch_context,
  133. usage_tracker,
  134. )
  135. from backend.app.services.printer_manager import printer_manager
  136. printer_manager._current_print_user[GONE] = {"user_id": 1, "username": "alice"}
  137. printer_manager._awaiting_plate_clear.add(GONE)
  138. printer_manager._last_trays[GONE] = {"ams": []}
  139. print_dispatch_context.mark_pending(GONE)
  140. kprofile_drift.note_slot_configured(GONE, 0, 0, -1)
  141. kprofile_drift._watches[(GONE, 0, 1)] = kprofile_drift._SlotWatch()
  142. layer_timelapse._active_sessions[GONE] = type("S", (), {"cleanup": lambda self: None})()
  143. usage_tracker._active_sessions[GONE] = object()
  144. service = obico_detection.obico_detection_service
  145. for attr in ("_states", "_state_keys", "_last_class", "_errors", "_action_fired"):
  146. getattr(service, attr)[GONE] = object()
  147. bambu_ftp._threemf_path_cache[(GONE, "cube")] = Path("/nonexistent/cube.3mf")
  148. camera._last_frames[GONE] = b"jpg"
  149. camera._last_frame_times[GONE] = 1.0
  150. camera._stream_start_times[GONE] = 1.0
  151. camera._active_external_streams[GONE] = 1
  152. camera._external_live_readers[GONE] = 1
  153. from backend.app.api.routes import printers
  154. printers._cover_cache[GONE] = {("Cube", "front"): b"png"}
  155. printers._cover_404_cache[GONE] = {}
  156. from backend.app.services.printer_file_cache import printer_file_cache
  157. printer_file_cache._keep((GONE, "/cache/a.3mf"), b"3mf")
  158. await main.forget_printer(GONE)
  159. assert GONE not in printer_manager._current_print_user
  160. assert GONE not in printer_manager._awaiting_plate_clear
  161. assert GONE not in printer_manager._last_trays
  162. assert not print_dispatch_context.adopt(GONE)
  163. assert (GONE, 0, 0) not in kprofile_drift._default_chosen
  164. assert (GONE, 0, 1) not in kprofile_drift._watches
  165. assert GONE not in layer_timelapse._active_sessions
  166. assert GONE not in usage_tracker._active_sessions
  167. for attr in ("_states", "_state_keys", "_last_class", "_errors", "_action_fired"):
  168. assert GONE not in getattr(service, attr), attr
  169. assert (GONE, "cube") not in bambu_ftp._threemf_path_cache
  170. assert GONE not in camera._last_frames
  171. assert GONE not in camera._last_frame_times
  172. assert GONE not in camera._stream_start_times
  173. assert GONE not in camera._active_external_streams
  174. assert GONE not in camera._external_live_readers
  175. assert GONE not in printers._cover_cache
  176. assert GONE not in printers._cover_404_cache
  177. assert not [key for key in printer_file_cache._entries if key[0] == GONE]
  178. async def test_the_plate_references_of_a_printer_go(tmp_path, monkeypatch):
  179. from backend.app.services import plate_detection
  180. monkeypatch.setattr(plate_detection, "_get_calibration_dir", lambda: tmp_path)
  181. for printer_id in (GONE, KEPT, 55):
  182. (tmp_path / f"printer_{printer_id}_ref_0.jpg").write_bytes(b"jpg")
  183. (tmp_path / f"printer_{printer_id}_metadata.json").write_text("{}")
  184. plate_detection.remove_printer_references(GONE)
  185. assert sorted(p.name for p in tmp_path.iterdir()) == [
  186. "printer_55_metadata.json",
  187. "printer_55_ref_0.jpg",
  188. f"printer_{KEPT}_metadata.json",
  189. f"printer_{KEPT}_ref_0.jpg",
  190. ]
  191. async def test_the_scheduler_and_live_activities_forget_it():
  192. from backend.app.services.notify_live_activities import notify_live_activities
  193. from backend.app.services.print_scheduler import scheduler
  194. scheduler._upload_backoff[GONE] = 1e12
  195. scheduler._upload_refusals[GONE] = 6
  196. scheduler._upload_refusal_notified.add(GONE)
  197. scheduler._wake_failures[GONE] = 1e12
  198. scheduler._dispatch_holds[GONE] = (1.0, "IDLE", None)
  199. scheduler._auto_dry_units[(GONE, 0)] = {}
  200. for attr in ("_snapshots", "_new_fallbacks", "_started_at", "_progress_resets"):
  201. getattr(notify_live_activities, attr)[GONE] = object()
  202. notify_live_activities._wake.clear()
  203. await main.forget_printer(GONE)
  204. assert GONE not in scheduler._upload_backoff
  205. assert GONE not in scheduler._upload_refusals
  206. assert GONE not in scheduler._upload_refusal_notified
  207. assert GONE not in scheduler._wake_failures
  208. assert GONE not in scheduler._dispatch_holds
  209. assert (GONE, 0) not in scheduler._auto_dry_units
  210. for attr in ("_snapshots", "_new_fallbacks", "_started_at", "_progress_resets"):
  211. assert GONE not in getattr(notify_live_activities, attr), attr
  212. # Its tiles are ended at once (the printer is gone), before a new one takes the id.
  213. assert notify_live_activities._wake.is_set()
  214. @pytest.mark.parametrize("ml_fails", [False, True])
  215. async def test_an_obico_check_in_flight_when_the_printer_goes_ends_quietly(ml_fails):
  216. """The check waits up to 30 s for the ML server. A KeyError there stopped the
  217. poll for every printer; an error written back stuck to the next printer given the id."""
  218. from unittest.mock import AsyncMock, MagicMock, patch
  219. from httpx import Request, Response
  220. from backend.app.services.obico_detection import ObicoDetectionService
  221. service = ObicoDetectionService()
  222. async def ml_call(url, **_kwargs):
  223. service.forget_printer(GONE) # deleted meanwhile
  224. if ml_fails:
  225. raise OSError("ML server gone")
  226. return Response(200, json={"detections": []}, request=Request("GET", url))
  227. client = MagicMock()
  228. client.get = AsyncMock(side_effect=ml_call)
  229. client.__aenter__ = AsyncMock(return_value=client)
  230. client.__aexit__ = AsyncMock(return_value=False)
  231. settings = {"sensitivity": "medium", "snapshot_base_url": "http://b", "ml_url": "http://o", "action": "notify"}
  232. with (
  233. patch("backend.app.services.obico_detection.httpx.AsyncClient", return_value=client),
  234. patch.object(service, "_capture_frame", new=AsyncMock(return_value=b"\xff\xd8jpg\xff\xd9")),
  235. ):
  236. await service._check_printer(GONE, MagicMock(state="RUNNING", task_name="job", subtask_name=""), settings)
  237. for attr in ("_states", "_state_keys", "_last_class", "_errors", "_action_fired"):
  238. assert GONE not in getattr(service, attr), attr
  239. assert service._last_error is None