test_scheduler_filament_hold_3522.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. """check_queue holds a job its creator may not print, and starts it once allowed (#3522).
  2. Run on a real database with the dispatch drained, as the printer-scope hold
  3. (#1727) is in ``test_scheduler_check_queue_holds.py``: the job stays pending
  4. with a reason naming the filament, is never failed, and goes out on its own
  5. on the first pass after the group's rules stop blocking it.
  6. """
  7. import asyncio
  8. from unittest.mock import AsyncMock, patch
  9. import pytest
  10. from sqlalchemy import update
  11. import backend.app.services.print_scheduler as scheduler_module
  12. from backend.app.models.group import Group
  13. from backend.app.models.print_queue import PrintQueueItem
  14. from backend.app.models.printer import Printer
  15. from backend.app.models.settings import Settings
  16. from backend.app.models.user import User
  17. from backend.app.services.print_scheduler import PrintScheduler
  18. from backend.tests.unit.test_scheduler_busy_reasons_3018 import _scheduler, one_printer # noqa: F401
  19. from backend.tests.unit.test_scheduler_filament_scope_3522 import (
  20. FILE_NEEDS_PETG_MATTE,
  21. REQ_PETG_MATTE,
  22. REQ_WHITE_PETG,
  23. SPOOL_IS_PETG_MATTE,
  24. _external,
  25. _tray,
  26. )
  27. async def _job_by_a_creator_blocked_from_petg_matte(ctx, **fields) -> tuple[int, int]:
  28. """Queue a job created by a user whose group blocks PETG Matte; returns (item id, group id)."""
  29. async with ctx.session_maker() as db:
  30. db.add(Settings(key="auth_enabled", value="true"))
  31. group = Group(
  32. name="friends",
  33. permissions=["queue:create"],
  34. restrict_filaments=True,
  35. filament_rule_mode="block",
  36. filament_rules=[{"match": "filament_id", "value": "GFG03"}],
  37. )
  38. user = User(username="friend", password_hash="x", is_active=True, groups=[group])
  39. db.add_all([group, user])
  40. await db.commit()
  41. item_id = await ctx.add_item()
  42. async with ctx.session_maker() as db:
  43. await db.execute(
  44. update(PrintQueueItem).where(PrintQueueItem.id == item_id).values(created_by_id=user.id, **fields)
  45. )
  46. await db.commit()
  47. return item_id, group.id
  48. async def _load(ctx, item_id: int) -> PrintQueueItem:
  49. async with ctx.session_maker() as db:
  50. return await db.get(PrintQueueItem, item_id)
  51. async def _lift_the_rule(session_maker, group_id: int) -> None:
  52. async with session_maker() as db:
  53. await db.execute(update(Group).where(Group.id == group_id).values(filament_rules=[]))
  54. await db.commit()
  55. def _file_prints_petg_matte():
  56. return patch.object(PrintScheduler, "_get_filament_requirements", AsyncMock(return_value=[dict(REQ_PETG_MATTE)]))
  57. class TestAJobOnAFixedPrinter:
  58. @pytest.mark.asyncio
  59. async def test_is_held_then_starts_once_the_rule_is_lifted(self, one_printer): # noqa: F811
  60. item_id, group_id = await _job_by_a_creator_blocked_from_petg_matte(one_printer)
  61. # One scheduler for both passes, as in production: each pass must read
  62. # the group's rules afresh, not keep the last pass's answer.
  63. with _file_prints_petg_matte():
  64. async with _scheduler(one_printer, idle=True) as scheduler:
  65. await scheduler.check_queue()
  66. assert not scheduler._inflight
  67. item = await _load(one_printer, item_id)
  68. assert item.status == "pending"
  69. assert item.waiting_reason == FILE_NEEDS_PETG_MATTE
  70. assert not item.manual_start
  71. assert item.error_message is None
  72. await _lift_the_rule(one_printer.session_maker, group_id)
  73. await scheduler.check_queue()
  74. item = await _load(one_printer, item_id)
  75. assert item.status != "pending"
  76. assert item.status != "failed"
  77. class TestAJobForAnyPrinterOfAModel:
  78. @pytest.mark.asyncio
  79. async def test_is_held_unassigned_then_starts_once_the_rule_is_lifted(self, one_printer): # noqa: F811
  80. item_id, group_id = await _job_by_a_creator_blocked_from_petg_matte(
  81. one_printer, printer_id=None, target_model="X1C"
  82. )
  83. with (
  84. _file_prints_petg_matte(),
  85. patch(
  86. "backend.app.services.notification_service.notification_service.on_queue_job_assigned",
  87. AsyncMock(),
  88. ) as assigned,
  89. patch(
  90. "backend.app.services.notification_service.notification_service.on_queue_job_waiting",
  91. AsyncMock(),
  92. ),
  93. ):
  94. async with _scheduler(one_printer, idle=True) as scheduler:
  95. await scheduler.check_queue()
  96. assert not scheduler._inflight
  97. item = await _load(one_printer, item_id)
  98. assert (item.status, item.printer_id) == ("pending", None)
  99. assert item.waiting_reason == FILE_NEEDS_PETG_MATTE
  100. # Never assigned to a printer only to be given back on every pass.
  101. assigned.assert_not_awaited()
  102. await _lift_the_rule(one_printer.session_maker, group_id)
  103. await scheduler.check_queue()
  104. item = await _load(one_printer, item_id)
  105. assert item.status not in ("pending", "failed")
  106. class TestAnEditBetweenTheCheckAndTheClaim:
  107. """The pass judges the job as loaded; dispatch sends the row as claimed (#3522).
  108. An edit landing in between must not carry the job onto a blocked spool:
  109. it is re-judged once claimed, when edits are refused, and left to the
  110. next pass, which holds it.
  111. """
  112. @pytest.mark.asyncio
  113. @pytest.mark.parametrize(
  114. "edit",
  115. [
  116. # Without the AMS the blocked external spool would feed.
  117. {"use_ams": False, "ams_mapping": "[254]"},
  118. # A real tray turns the AMS back on (#2595): the blocked tray would feed.
  119. {"use_ams": False, "ams_mapping": "[0]"},
  120. {"ams_mapping": "[0]"},
  121. ],
  122. ids=["use_ams_off_onto_the_external_spool", "use_ams_off_onto_a_blocked_tray", "blocked_tray"],
  123. )
  124. async def test_is_not_dispatched_as_edited(self, one_printer, edit): # noqa: F811
  125. item_id, _ = await _job_by_a_creator_blocked_from_petg_matte(one_printer)
  126. # Tray 1 is allowed and is what the pass maps the slot to; tray 0 and
  127. # the external spool hold PETG Matte, which the group blocks.
  128. loaded = [_tray(0, "GFG03"), _tray(1, "GFG99"), _external(254, "GFG03")]
  129. with (
  130. patch.object(PrintScheduler, "_get_filament_requirements", AsyncMock(return_value=[dict(REQ_WHITE_PETG)])),
  131. patch.object(PrintScheduler, "_build_loaded_filaments", lambda self, status: [dict(f) for f in loaded]),
  132. ):
  133. async with _scheduler(one_printer, idle=True) as scheduler:
  134. real_claim = scheduler._claim_for_dispatch
  135. async def claim_after_an_edit(db, claimed_id, printer_id=None):
  136. async with one_printer.session_maker() as edit_db:
  137. await edit_db.execute(
  138. update(PrintQueueItem).where(PrintQueueItem.id == claimed_id).values(**edit)
  139. )
  140. await edit_db.commit()
  141. return await real_claim(db, claimed_id, printer_id)
  142. clear_claim = AsyncMock(wraps=scheduler._clear_dispatch_claim)
  143. with (
  144. patch.object(scheduler, "_claim_for_dispatch", claim_after_an_edit),
  145. patch.object(scheduler, "_clear_dispatch_claim", clear_claim),
  146. ):
  147. await scheduler.check_queue()
  148. if scheduler._inflight:
  149. await asyncio.gather(*(t for t, _ in scheduler._inflight.values()), return_exceptions=True)
  150. start_print = scheduler_module.printer_manager.start_print
  151. start_print.assert_not_called()
  152. # The claim is backed out, as on every exit from a dispatch.
  153. assert [c.args[1] for c in clear_claim.await_args_list] == [item_id]
  154. async with one_printer.session_maker() as db:
  155. assert (await db.get(PrintQueueItem, item_id)).status == "pending"
  156. # The harness's in-memory SQLite shares one connection between
  157. # sessions (StaticPool), so the pass closing its session can
  158. # roll back the clear the dispatch task just wrote. Done again
  159. # here so the next pass can claim the row, as it could in
  160. # production.
  161. await db.execute(update(PrintQueueItem).where(PrintQueueItem.id == item_id).values(dispatching_at=None))
  162. await db.commit()
  163. # A stored blocked tray is re-picked onto the allowed one (#3522),
  164. # which feeds from the AMS whatever use_ams says.
  165. async with _scheduler(one_printer, idle=True) as scheduler:
  166. await scheduler.check_queue()
  167. await asyncio.gather(*(t for t, _ in scheduler._inflight.values()), return_exceptions=True)
  168. start_print = scheduler_module.printer_manager.start_print
  169. assert start_print.call_args.kwargs["ams_mapping"] == [1]
  170. def _spools(loaded):
  171. """The printer's spools as ``_build_loaded_filaments`` reports them."""
  172. return patch.object(PrintScheduler, "_build_loaded_filaments", lambda self, status: [dict(f) for f in loaded])
  173. def _assignment_notifications():
  174. return (
  175. patch(
  176. "backend.app.services.notification_service.notification_service.on_queue_job_assigned",
  177. AsyncMock(),
  178. ),
  179. patch(
  180. "backend.app.services.notification_service.notification_service.on_queue_job_waiting",
  181. AsyncMock(),
  182. ),
  183. )
  184. class TestAModelJobWithoutTheAms:
  185. """The model branch judges the external spool before assigning a printer (#3522)."""
  186. @pytest.mark.asyncio
  187. async def test_on_a_dual_nozzle_printer_a_blocked_external_spool_holds_it_unassigned(self, one_printer): # noqa: F811
  188. # use_ams picks the extruder there, so every external spool counts.
  189. async with one_printer.session_maker() as db:
  190. await db.execute(update(Printer).where(Printer.id == one_printer.printer_id).values(model="H2D"))
  191. await db.commit()
  192. item_id, _ = await _job_by_a_creator_blocked_from_petg_matte(
  193. one_printer, printer_id=None, target_model="H2D", use_ams=False
  194. )
  195. assigned_patch, waiting_patch = _assignment_notifications()
  196. with (
  197. patch.object(PrintScheduler, "_get_filament_requirements", AsyncMock(return_value=[dict(REQ_WHITE_PETG)])),
  198. _spools([_tray(1, "GFG99"), _external(254, "GFG03")]),
  199. assigned_patch as assigned,
  200. waiting_patch as waiting,
  201. ):
  202. async with _scheduler(one_printer, idle=True) as scheduler:
  203. for _ in range(2):
  204. await scheduler.check_queue()
  205. assert not scheduler._inflight
  206. item = await _load(one_printer, item_id)
  207. assert (item.status, item.printer_id) == ("pending", None)
  208. assert item.waiting_reason == SPOOL_IS_PETG_MATTE
  209. # Not assigned only to be given back on every pass.
  210. assigned.assert_not_awaited()
  211. # Held before a printer is assigned, the job says so once, as any
  212. # waiting model job does. Held only after the assignment, it would
  213. # be given back without a word.
  214. waiting.assert_awaited_once()
  215. assert waiting.await_args.kwargs["waiting_reason"] == SPOOL_IS_PETG_MATTE
  216. @pytest.mark.asyncio
  217. async def test_on_a_single_nozzle_printer_the_ams_tray_it_is_sent_decides(self, one_printer): # noqa: F811
  218. # A real tray in the mapping turns the AMS back on (#2595), so the
  219. # blocked spool on the holder isn't fed and holds nothing.
  220. item_id, _ = await _job_by_a_creator_blocked_from_petg_matte(
  221. one_printer, printer_id=None, target_model="X1C", use_ams=False
  222. )
  223. assigned_patch, waiting_patch = _assignment_notifications()
  224. with (
  225. patch.object(PrintScheduler, "_get_filament_requirements", AsyncMock(return_value=[dict(REQ_WHITE_PETG)])),
  226. _spools([_tray(1, "GFG99"), _external(254, "GFG03")]),
  227. assigned_patch,
  228. waiting_patch,
  229. ):
  230. async with _scheduler(one_printer, idle=True) as scheduler:
  231. await scheduler.check_queue()
  232. await asyncio.gather(*(t for t, _ in scheduler._inflight.values()), return_exceptions=True)
  233. start_print = scheduler_module.printer_manager.start_print
  234. assert start_print.call_args.kwargs["ams_mapping"] == [1]
  235. class TestAnEditToUseAms:
  236. @pytest.mark.asyncio
  237. async def test_counts_as_an_edit_since_the_pass_loaded_the_job(self, one_printer): # noqa: F811
  238. # use_ams decides the feed the filament check judges (#3522), so a
  239. # pass must not act on a job whose use_ams changed under it.
  240. item_id = await one_printer.add_item()
  241. async with one_printer.session_maker() as db:
  242. item = await db.get(PrintQueueItem, item_id)
  243. assert not await scheduler_module._edited_since_loaded(db, item)
  244. async with one_printer.session_maker() as edit_db:
  245. await edit_db.execute(update(PrintQueueItem).where(PrintQueueItem.id == item_id).values(use_ams=False))
  246. await edit_db.commit()
  247. assert await scheduler_module._edited_since_loaded(db, item)
  248. class TestAJobHeldByALoadedSpool:
  249. @pytest.mark.asyncio
  250. async def test_starts_once_the_spool_is_swapped(self, one_printer): # noqa: F811
  251. # Sent without the AMS, the job prints from the holder; no AMS tray
  252. # matches, so nothing turns the AMS back on.
  253. item_id, _ = await _job_by_a_creator_blocked_from_petg_matte(one_printer, use_ams=False)
  254. loaded = [_tray(1, "GFA00", "PLA"), _external(254, "GFG03")]
  255. with (
  256. patch.object(PrintScheduler, "_get_filament_requirements", AsyncMock(return_value=[dict(REQ_WHITE_PETG)])),
  257. patch.object(PrintScheduler, "_build_loaded_filaments", lambda self, status: [dict(f) for f in loaded]),
  258. ):
  259. async with _scheduler(one_printer, idle=True) as scheduler:
  260. await scheduler.check_queue()
  261. assert not scheduler._inflight
  262. item = await _load(one_printer, item_id)
  263. assert (item.status, item.waiting_reason) == ("pending", SPOOL_IS_PETG_MATTE)
  264. loaded[1] = _external(254, "GFG99")
  265. await scheduler.check_queue()
  266. await asyncio.gather(*(t for t, _ in scheduler._inflight.values()), return_exceptions=True)
  267. start_print = scheduler_module.printer_manager.start_print
  268. assert start_print.call_args.kwargs["ams_mapping"] == [254]
  269. class TestAModelJobHeldOnceAssigned:
  270. @pytest.mark.asyncio
  271. async def test_is_released_back_to_the_pool_not_failed_and_never_announced(self, one_printer): # noqa: F811
  272. # A file with no readable filament list passes every check made while
  273. # choosing a printer; once assigned, the printer's blocked tray holds
  274. # it, and "any X1C" means any, so the printer is given back. That
  275. # repeats on every pass, so "assigned" is only announced once the job
  276. # has passed the checks that follow the assignment.
  277. item_id, _ = await _job_by_a_creator_blocked_from_petg_matte(one_printer, printer_id=None, target_model="X1C")
  278. assigned_patch, waiting_patch = _assignment_notifications()
  279. with (
  280. patch.object(PrintScheduler, "_get_filament_requirements", AsyncMock(return_value=None)),
  281. _spools([_tray(0, "GFG03"), _tray(1, "GFG99")]),
  282. assigned_patch as assigned,
  283. waiting_patch,
  284. ):
  285. async with _scheduler(one_printer, idle=True) as scheduler:
  286. for _ in range(2):
  287. await scheduler.check_queue()
  288. assert not scheduler._inflight
  289. assigned.assert_not_awaited()
  290. item = await _load(one_printer, item_id)
  291. assert (item.status, item.printer_id, item.error_message) == ("pending", None, None)
  292. assert item.waiting_reason == (
  293. "This file doesn't list its filaments, and the printer has a loaded spool "
  294. "the job creator's group can't print with: PETG (GFG03)"
  295. )
  296. class TestAJobWhoseFileIsGone:
  297. @pytest.mark.asyncio
  298. async def test_fails_as_missing_instead_of_waiting_as_unlisted(self, one_printer): # noqa: F811
  299. # A missing file lists no filaments either, but there is nothing to
  300. # judge or print: it takes the dispatch's missing-file failure.
  301. item_id, _ = await _job_by_a_creator_blocked_from_petg_matte(one_printer)
  302. (one_printer.base_dir / "archives" / "job-0.3mf").unlink()
  303. with _spools([_tray(0, "GFG03")]):
  304. async with _scheduler(one_printer, idle=True) as scheduler:
  305. await scheduler.check_queue()
  306. item = await _load(one_printer, item_id)
  307. assert item.status == "failed"
  308. assert "can't print with" not in (item.waiting_reason or "")