test_scheduler_cleanup_library.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. import logging
  2. from contextlib import ExitStack
  3. from pathlib import Path
  4. from types import SimpleNamespace
  5. from unittest.mock import AsyncMock, MagicMock, patch
  6. import pytest
  7. from sqlalchemy import event, select
  8. from sqlalchemy.exc import OperationalError
  9. from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
  10. import backend.app.models # noqa: F401 - populate Base.metadata
  11. import backend.app.services.print_scheduler as scheduler_module
  12. from backend.app.core.database import Base
  13. from backend.app.models.archive import PrintArchive
  14. from backend.app.models.library import LibraryFile
  15. from backend.app.models.print_queue import PrintQueueItem, PrintQueueVariant
  16. from backend.app.models.printer import Printer
  17. from backend.app.services.print_scheduler import PrintScheduler
  18. from backend.app.utils.archive_paths import archive_photos_dir
  19. from backend.app.utils.library_paths import library_photos_dir
  20. from backend.tests._fixtures.background_tasks import discarding_spawn_patch
  21. @pytest.fixture
  22. async def queue_factory(tmp_path):
  23. engine = create_async_engine("sqlite+aiosqlite:///:memory:", echo=False)
  24. async with engine.begin() as conn:
  25. await conn.run_sync(Base.metadata.create_all)
  26. session_maker = async_sessionmaker(engine, expire_on_commit=False)
  27. case_counter = 0
  28. async def make_case(*, cleanup=True, is_external=False, thumbnail_path=None, siblings=(), photos=()):
  29. nonlocal case_counter
  30. case_counter += 1
  31. base_dir = tmp_path / f"case-{case_counter}"
  32. base_dir.mkdir()
  33. archive_dir = base_dir / "archive"
  34. source_path = base_dir / "library" / f"source-{case_counter}.3mf"
  35. source_path.parent.mkdir()
  36. source_path.write_bytes(b"library source")
  37. thumbnail_actual_path = None
  38. thumbnail_db_path = None
  39. if thumbnail_path == "relative":
  40. thumbnail_db_path = f"thumbs/preview-{case_counter}.png"
  41. thumbnail_actual_path = base_dir / thumbnail_db_path
  42. elif thumbnail_path == "absolute":
  43. thumbnail_actual_path = tmp_path / f"absolute-preview-{case_counter}.png"
  44. thumbnail_db_path = str(thumbnail_actual_path)
  45. elif thumbnail_path is not None:
  46. thumbnail_actual_path = Path(thumbnail_path)
  47. thumbnail_db_path = str(thumbnail_path)
  48. if thumbnail_actual_path:
  49. thumbnail_actual_path.parent.mkdir(parents=True, exist_ok=True)
  50. thumbnail_actual_path.write_bytes(b"thumbnail")
  51. async with session_maker() as db:
  52. printer = Printer(
  53. name=f"Printer {case_counter}",
  54. serial_number=f"SERIAL-{case_counter}",
  55. ip_address="127.0.0.1",
  56. access_code="access-code",
  57. model="X1C",
  58. )
  59. library_file = LibraryFile(
  60. filename=f"source-{case_counter}.3mf",
  61. file_path=str(source_path),
  62. file_type="3mf",
  63. file_size=source_path.stat().st_size,
  64. file_hash=None,
  65. thumbnail_path=thumbnail_db_path,
  66. file_metadata=None,
  67. is_external=is_external,
  68. photos=list(photos) or None,
  69. )
  70. db.add_all([printer, library_file])
  71. await db.flush()
  72. # Photos of the printed result (#3077). Written through the real
  73. # helper so the test cannot drift from the layout the code uses.
  74. photos_dir = None
  75. if photos:
  76. with patch.object(scheduler_module.settings, "archive_dir", archive_dir):
  77. photos_dir = library_photos_dir(library_file.id)
  78. photos_dir.mkdir(parents=True, exist_ok=True)
  79. for name in photos:
  80. (photos_dir / name).write_bytes(f"photo {name}".encode())
  81. item = PrintQueueItem(
  82. printer_id=printer.id,
  83. library_file_id=library_file.id,
  84. status="pending",
  85. cleanup_library_after_dispatch=cleanup,
  86. bed_levelling="on",
  87. flow_cali="off",
  88. vibration_cali=True,
  89. layer_inspect=False,
  90. timelapse=False,
  91. use_ams=True,
  92. nozzle_offset_cali="on",
  93. )
  94. db.add(item)
  95. await db.flush()
  96. # The other copies of a quantity>1 dispatch (#2819). Each entry is a
  97. # dict of overrides: `status`, `own_archive` for a copy that already
  98. # holds one, and `extra_variant` for a cross-model copy that keeps a
  99. # candidate this cleanup does not consume.
  100. sibling_ids = []
  101. other_file = None
  102. for spec in siblings:
  103. sibling = PrintQueueItem(
  104. printer_id=printer.id,
  105. library_file_id=None if spec.get("variants") else library_file.id,
  106. status=spec.get("status", "pending"),
  107. cleanup_library_after_dispatch=cleanup,
  108. )
  109. if spec.get("own_archive"):
  110. own = PrintArchive(
  111. printer_id=printer.id,
  112. filename="already-dispatched.3mf",
  113. file_path="archives/already-dispatched.3mf",
  114. file_size=1,
  115. status="printing",
  116. )
  117. db.add(own)
  118. await db.flush()
  119. sibling.archive_id = own.id
  120. db.add(sibling)
  121. await db.flush()
  122. if spec.get("variants"):
  123. db.add(
  124. PrintQueueVariant(
  125. queue_item_id=sibling.id,
  126. library_file_id=library_file.id,
  127. target_model="X1C",
  128. position=0,
  129. )
  130. )
  131. if spec.get("extra_variant"):
  132. if other_file is None:
  133. other_path = base_dir / "library" / f"other-{case_counter}.3mf"
  134. other_path.write_bytes(b"other source")
  135. other_file = LibraryFile(
  136. filename=f"other-{case_counter}.3mf",
  137. file_path=str(other_path),
  138. file_type="3mf",
  139. file_size=other_path.stat().st_size,
  140. )
  141. db.add(other_file)
  142. await db.flush()
  143. db.add(
  144. PrintQueueVariant(
  145. queue_item_id=sibling.id,
  146. library_file_id=other_file.id,
  147. target_model="P1S",
  148. position=1,
  149. )
  150. )
  151. sibling_ids.append(sibling.id)
  152. await db.commit()
  153. return SimpleNamespace(
  154. session_maker=session_maker,
  155. base_dir=base_dir,
  156. archive_dir=archive_dir,
  157. source_path=source_path,
  158. photos_dir=photos_dir,
  159. photo_names=list(photos),
  160. thumbnail_path=thumbnail_actual_path,
  161. printer_id=printer.id,
  162. library_file_id=library_file.id,
  163. queue_item_id=item.id,
  164. sibling_ids=sibling_ids,
  165. other_library_file_id=other_file.id if other_file is not None else None,
  166. archive_path=None,
  167. upload=AsyncMock(return_value=True),
  168. start_print=MagicMock(return_value=True),
  169. )
  170. try:
  171. yield make_case
  172. finally:
  173. await engine.dispose()
  174. async def _dispatch_library_item(
  175. ctx,
  176. *,
  177. archive_failure=False,
  178. unlink_side_effect=None,
  179. cleanup_commit_failure=False,
  180. photo_commit_failure=False,
  181. ):
  182. scheduler = PrintScheduler()
  183. async def archive_print(
  184. self,
  185. *,
  186. printer_id,
  187. source_file,
  188. original_filename,
  189. created_by_id=None,
  190. project_id=None,
  191. cost_center_id=None,
  192. plate_id=None,
  193. library_file_id=None,
  194. ):
  195. if archive_failure:
  196. raise RuntimeError("archive copy failed")
  197. archive_rel_path = Path("archives") / f"archive-{ctx.queue_item_id}.3mf"
  198. ctx.archive_path = ctx.base_dir / archive_rel_path
  199. ctx.archive_path.parent.mkdir(parents=True, exist_ok=True)
  200. ctx.archive_path.write_bytes(Path(source_file).read_bytes())
  201. archive = PrintArchive(
  202. printer_id=printer_id,
  203. filename=original_filename,
  204. file_path=str(archive_rel_path),
  205. file_size=ctx.archive_path.stat().st_size,
  206. content_hash=None,
  207. thumbnail_path=None,
  208. timelapse_path=None,
  209. print_time_seconds=120,
  210. status="completed",
  211. project_id=project_id,
  212. library_file_id=library_file_id,
  213. created_by_id=created_by_id,
  214. )
  215. self.db.add(archive)
  216. await self.db.flush()
  217. return archive
  218. patches = [
  219. patch.object(scheduler_module.settings, "base_dir", ctx.base_dir),
  220. patch.object(scheduler_module.settings, "archive_dir", ctx.archive_dir),
  221. patch("backend.app.services.archive.ArchiveService.archive_print", new=archive_print),
  222. patch("backend.app.services.print_scheduler.printer_manager.is_connected", MagicMock(return_value=True)),
  223. patch("backend.app.services.print_scheduler.printer_manager.get_status", MagicMock(return_value=None)),
  224. patch("backend.app.services.print_scheduler.printer_manager.start_print", ctx.start_print),
  225. patch("backend.app.services.print_scheduler.printer_manager.set_awaiting_plate_clear", MagicMock()),
  226. patch(
  227. "backend.app.services.print_scheduler.get_ftp_retry_settings", AsyncMock(return_value=(False, 0, 0, 1.0))
  228. ),
  229. patch("backend.app.services.print_scheduler.delete_file_async", AsyncMock(return_value=True)),
  230. patch("backend.app.services.print_scheduler.upload_file_async", ctx.upload),
  231. patch("backend.app.services.print_scheduler.cache_3mf_download", MagicMock()),
  232. discarding_spawn_patch(),
  233. patch("backend.app.services.notification_service.notification_service.on_queue_job_started", AsyncMock()),
  234. patch("backend.app.services.notification_service.notification_service.on_queue_job_failed", AsyncMock()),
  235. patch("backend.app.services.mqtt_relay.mqtt_relay.on_queue_job_started", AsyncMock()),
  236. patch.object(scheduler, "_propagate_owner_to_printer_manager", AsyncMock()),
  237. patch.object(scheduler, "_power_off_if_needed", AsyncMock()),
  238. ]
  239. if unlink_side_effect:
  240. patches.append(patch.object(type(ctx.source_path), "unlink", unlink_side_effect))
  241. with ExitStack() as stack:
  242. for patcher in patches:
  243. stack.enter_context(patcher)
  244. async with ctx.session_maker() as db:
  245. if cleanup_commit_failure:
  246. _arm_commit_failure_on_library_delete(db)
  247. if photo_commit_failure:
  248. _arm_commit_failure_on_photo_move(db, stack)
  249. item = await db.get(PrintQueueItem, ctx.queue_item_id)
  250. await scheduler._start_print(db, item)
  251. def _fail_inside_the_next_flush(db, statement):
  252. """Make the next flush on this session fail, once, the way SQLite does.
  253. Raising *instead of* calling `db.commit()` does not reproduce a failed
  254. commit and is not the dangerous case: the session stays ACTIVE, nothing is
  255. expired, and every loaded instance still reads out of `__dict__`. What a
  256. busy writer actually gives you is a statement error raised inside the
  257. flush — SQLite takes the write lock at the first DML statement, not at
  258. COMMIT, so "database is locked" surfaces there (#1853). SQLAlchemy rolls
  259. that back internally through `safe_reraise` before re-raising, which
  260. expires every loaded instance and leaves the session in pending-rollback
  261. state: the next ORM attribute read raises PendingRollbackError, *before*
  262. the handler's own rollback can run. That is the state a handler on this
  263. path has to survive, so it is the state these tests have to produce.
  264. """
  265. sync_session = db.sync_session
  266. fired = False
  267. def after_flush(session, flush_context):
  268. nonlocal fired
  269. if fired:
  270. return
  271. fired = True
  272. raise OperationalError(statement, {}, Exception("database is locked"))
  273. event.listen(sync_session, "after_flush", after_flush)
  274. def _arm_commit_failure_on_library_delete(db):
  275. """Make the one commit that removes the library row fail, once.
  276. Stands in for the "database is locked" cascades the commit's own comment
  277. cites (#1853). Armed by the delete rather than by a call count so it
  278. cannot drift onto a different commit.
  279. """
  280. original_delete = db.delete
  281. original_commit = db.commit
  282. armed = False
  283. async def delete(obj):
  284. nonlocal armed
  285. if isinstance(obj, LibraryFile):
  286. armed = True
  287. return await original_delete(obj)
  288. async def commit():
  289. nonlocal armed
  290. if armed:
  291. armed = False
  292. _fail_inside_the_next_flush(db, "DELETE FROM library_files WHERE library_files.id = ?")
  293. return await original_commit()
  294. db.delete = delete
  295. db.commit = commit
  296. def _arm_commit_failure_on_photo_move(db, stack):
  297. """Make the commit that records the carried photos fail, once.
  298. The second commit of this path (#3077): the archive and the delete are
  299. already committed, the pictures are already on disk under the archive,
  300. and only `archive.photos` is pending. Armed by the move itself so it
  301. cannot drift onto the delete's commit.
  302. """
  303. original_commit = db.commit
  304. original_move = scheduler_module.move_library_photos
  305. armed = False
  306. def move_library_photos(file_id, photos, destination):
  307. nonlocal armed
  308. carried = original_move(file_id, photos, destination)
  309. armed = bool(carried)
  310. return carried
  311. async def commit():
  312. nonlocal armed
  313. if armed:
  314. armed = False
  315. _fail_inside_the_next_flush(db, "UPDATE print_archives SET photos=? WHERE print_archives.id = ?")
  316. return await original_commit()
  317. stack.enter_context(patch.object(scheduler_module, "move_library_photos", move_library_photos))
  318. db.commit = commit
  319. async def _queue_snapshot(ctx):
  320. async with ctx.session_maker() as db:
  321. item = await db.get(PrintQueueItem, ctx.queue_item_id)
  322. library_file = await db.get(LibraryFile, ctx.library_file_id)
  323. archive = await db.get(PrintArchive, item.archive_id) if item.archive_id else None
  324. return item, library_file, archive
  325. @pytest.mark.asyncio
  326. async def test_cleanup_unlinks_library_file_and_removes_db_row(queue_factory):
  327. ctx = await queue_factory(cleanup=True)
  328. await _dispatch_library_item(ctx)
  329. item, library_file, archive = await _queue_snapshot(ctx)
  330. assert item.status == "printing"
  331. assert item.library_file_id is None
  332. assert item.archive_id == archive.id
  333. assert library_file is None
  334. assert not ctx.source_path.exists()
  335. @pytest.mark.asyncio
  336. async def test_external_library_file_skips_cleanup(queue_factory):
  337. ctx = await queue_factory(cleanup=True, is_external=True)
  338. await _dispatch_library_item(ctx)
  339. item, library_file, archive = await _queue_snapshot(ctx)
  340. assert item.status == "printing"
  341. assert item.library_file_id == ctx.library_file_id
  342. assert item.archive_id == archive.id
  343. assert library_file is not None
  344. assert ctx.source_path.exists()
  345. @pytest.mark.asyncio
  346. async def test_cleanup_moves_the_photos_into_the_archive(queue_factory):
  347. """Photos follow the consumed file into the archive that replaces it (#3077).
  348. The row is hard-deleted here, so leaving the photo directory alone
  349. orphaned it under an id nothing points at any more — and the pictures
  350. of a print that still has a record disappeared from the UI.
  351. """
  352. ctx = await queue_factory(cleanup=True, photos=["a1b2c3d4.jpg", "e5f6a7b8.png"])
  353. await _dispatch_library_item(ctx)
  354. _, library_file, archive = await _queue_snapshot(ctx)
  355. assert library_file is None
  356. assert not ctx.photos_dir.exists()
  357. assert archive.photos == ctx.photo_names
  358. with patch.object(scheduler_module.settings, "base_dir", ctx.base_dir):
  359. destination = archive_photos_dir(archive)
  360. for name in ctx.photo_names:
  361. assert (destination / name).read_bytes() == f"photo {name}".encode()
  362. @pytest.mark.asyncio
  363. async def test_external_library_file_keeps_its_photos(queue_factory):
  364. ctx = await queue_factory(cleanup=True, is_external=True, photos=["a1b2c3d4.jpg"])
  365. await _dispatch_library_item(ctx)
  366. _, library_file, archive = await _queue_snapshot(ctx)
  367. assert library_file is not None
  368. assert (ctx.photos_dir / "a1b2c3d4.jpg").is_file()
  369. assert archive.photos is None
  370. @pytest.mark.asyncio
  371. async def test_archive_creation_failure_keeps_the_photos(queue_factory):
  372. ctx = await queue_factory(cleanup=True, photos=["a1b2c3d4.jpg"])
  373. await _dispatch_library_item(ctx, archive_failure=True)
  374. _, library_file, archive = await _queue_snapshot(ctx)
  375. assert archive is None
  376. assert library_file is not None
  377. assert (ctx.photos_dir / "a1b2c3d4.jpg").is_file()
  378. @pytest.mark.asyncio
  379. async def test_cleanup_commit_failure_keeps_the_photos_with_the_library_file(queue_factory):
  380. """The photos move after the delete commits, not before it (#3077).
  381. The commit that removes the library row can fail; the except branch rolls
  382. it back and the file is in the library again. Photos moved ahead of that
  383. commit would be gone from under it — the row would name a directory that
  384. no longer exists, and the pictures would sit under an archive that was
  385. rolled back too.
  386. """
  387. ctx = await queue_factory(cleanup=True, photos=["a1b2c3d4.jpg"])
  388. await _dispatch_library_item(ctx, cleanup_commit_failure=True)
  389. item, library_file, archive = await _queue_snapshot(ctx)
  390. assert item.status == "failed"
  391. assert archive is None
  392. assert library_file is not None
  393. assert library_file.photos == ["a1b2c3d4.jpg"]
  394. assert (ctx.photos_dir / "a1b2c3d4.jpg").read_bytes() == b"photo a1b2c3d4.jpg"
  395. assert not (ctx.base_dir / "archives" / "photos").exists()
  396. @pytest.mark.asyncio
  397. async def test_photo_commit_failure_still_dispatches_the_print(queue_factory, caplog):
  398. """A failed photos commit must not take the dispatch down with it (#3077).
  399. The archive and the delete are committed by then, so the print goes ahead
  400. and the pictures sit unnamed under the archive.
  401. The commit fails inside the flush, which is where a locked SQLite fails —
  402. see `_fail_inside_the_next_flush`. That expires every loaded instance
  403. twice over: once by SQLAlchemy's internal rollback, before the handler
  404. runs at all, and again at the handler's own `rollback()`. So the handler
  405. may not read an ORM attribute on either side of that rollback. Before it,
  406. a read raises PendingRollbackError; after it, MissingGreenlet — the nozzle
  407. guard's `archive.nozzle_diameter` and the upload's `printer.name` are the
  408. ones that used to die.
  409. """
  410. ctx = await queue_factory(cleanup=True, photos=["a1b2c3d4.jpg"])
  411. with caplog.at_level(logging.WARNING, logger="backend.app.services.print_scheduler"):
  412. await _dispatch_library_item(ctx, photo_commit_failure=True)
  413. item, library_file, archive = await _queue_snapshot(ctx)
  414. # The handler absorbed it rather than the failure being skipped: it names
  415. # the queue item and the archive from ints it held before the commit.
  416. assert any(
  417. f"Queue item {ctx.queue_item_id}: failed to carry library photos into archive {item.archive_id}"
  418. in record.message
  419. for record in caplog.records
  420. )
  421. assert item.status == "printing"
  422. assert item.archive_id == archive.id
  423. assert library_file is None
  424. assert not archive.photos
  425. ctx.upload.assert_awaited()
  426. ctx.start_print.assert_called()
  427. with patch.object(scheduler_module.settings, "base_dir", ctx.base_dir):
  428. destination = archive_photos_dir(archive)
  429. assert (destination / "a1b2c3d4.jpg").read_bytes() == b"photo a1b2c3d4.jpg"
  430. @pytest.mark.asyncio
  431. async def test_archive_creation_failure_skips_cleanup_and_dispatch(queue_factory):
  432. ctx = await queue_factory(cleanup=True, thumbnail_path="relative")
  433. await _dispatch_library_item(ctx, archive_failure=True)
  434. item, library_file, archive = await _queue_snapshot(ctx)
  435. assert item.status == "failed"
  436. assert item.error_message == "Failed to create archive from library file"
  437. assert item.archive_id is None
  438. assert archive is None
  439. assert library_file is not None
  440. assert ctx.source_path.exists()
  441. assert ctx.thumbnail_path.exists()
  442. ctx.upload.assert_not_awaited()
  443. ctx.start_print.assert_not_called()
  444. @pytest.mark.parametrize("thumbnail_path", ["absolute", "relative"])
  445. @pytest.mark.asyncio
  446. async def test_cleanup_resolves_absolute_and_relative_thumbnail_paths(queue_factory, thumbnail_path):
  447. ctx = await queue_factory(cleanup=True, thumbnail_path=thumbnail_path)
  448. await _dispatch_library_item(ctx)
  449. item, library_file, archive = await _queue_snapshot(ctx)
  450. assert item.status == "printing"
  451. assert item.archive_id == archive.id
  452. assert library_file is None
  453. assert not ctx.source_path.exists()
  454. assert not ctx.thumbnail_path.exists()
  455. @pytest.mark.asyncio
  456. async def test_archive_copy_survives_library_cleanup(queue_factory):
  457. ctx = await queue_factory(cleanup=True)
  458. await _dispatch_library_item(ctx)
  459. assert not ctx.source_path.exists()
  460. assert ctx.archive_path.exists()
  461. assert ctx.archive_path.read_bytes() == b"library source"
  462. uploaded_path = ctx.upload.await_args.args[2]
  463. assert uploaded_path == ctx.archive_path
  464. async def _sibling_snapshot(ctx):
  465. async with ctx.session_maker() as db:
  466. return [await db.get(PrintQueueItem, sid) for sid in ctx.sibling_ids]
  467. async def _variant_files(ctx, sibling_id):
  468. async with ctx.session_maker() as db:
  469. rows = await db.execute(
  470. select(PrintQueueVariant.library_file_id).where(PrintQueueVariant.queue_item_id == sibling_id)
  471. )
  472. return sorted(rows.scalars().all())
  473. # ---------------------------------------------------------------------------
  474. # Sibling copies of the same library row (#2819)
  475. #
  476. # `quantity > 1` on the printer-card upload-and-print flow puts the cleanup flag
  477. # on every copy, and batch clones inherit `library_file_id`. Consuming the row
  478. # for the first copy used to leave the others pointing at it, which failed with
  479. # "Library file not found" on SQLite and deleted the rows outright on
  480. # PostgreSQL, where the FK cascade is enforced.
  481. #
  482. # These run on SQLite, so they cover the orphan half directly. The cascade half
  483. # was verified by hand against a real PostgreSQL 16, building this same fixture
  484. # on both backends and comparing every row: without the fix the copies were gone
  485. # after the delete -- including the finished ones a batch order counts its
  486. # progress from, and a copy already printing from its own archive. With it, the
  487. # two backends agree row for row. `print_archives.library_file_id` is SET NULL,
  488. # so it is cleared by the same delete, which is why looking the archive up by
  489. # the consumed library id -- the obvious alternative fix -- cannot work there.
  490. # ---------------------------------------------------------------------------
  491. @pytest.mark.asyncio
  492. async def test_pending_copies_are_repointed_at_the_archive(queue_factory):
  493. ctx = await queue_factory(cleanup=True, siblings=({}, {}))
  494. await _dispatch_library_item(ctx)
  495. item, library_file, archive = await _queue_snapshot(ctx)
  496. assert library_file is None
  497. for sibling in await _sibling_snapshot(ctx):
  498. # Still queued -- the point is that they can now run, not that they run now.
  499. assert sibling.status == "pending"
  500. assert sibling.archive_id == archive.id
  501. assert sibling.library_file_id is None
  502. # Their file is already consumed; leaving this armed would delete
  503. # whatever library row they were next given.
  504. assert sibling.cleanup_library_after_dispatch is False
  505. @pytest.mark.asyncio
  506. async def test_copy_that_already_has_its_own_archive_keeps_it(queue_factory):
  507. ctx = await queue_factory(cleanup=True, siblings=({"own_archive": True, "status": "printing"},))
  508. await _dispatch_library_item(ctx)
  509. _, _, archive = await _queue_snapshot(ctx)
  510. (sibling,) = await _sibling_snapshot(ctx)
  511. # It is mid-print from its own archive and does not need the library file.
  512. # Re-pointing it would swap the file under a job already running.
  513. assert sibling.archive_id != archive.id
  514. assert sibling.status == "printing"
  515. # Cleared all the same: on PostgreSQL a row still naming the file goes with
  516. # it, and this one is a job that is currently printing.
  517. assert sibling.library_file_id is None
  518. @pytest.mark.parametrize("status", ["completed", "failed", "cancelled", "aborted"])
  519. @pytest.mark.asyncio
  520. async def test_finished_copies_keep_their_outcome_and_survive_the_delete(queue_factory, status):
  521. ctx = await queue_factory(cleanup=True, siblings=({"status": status},))
  522. await _dispatch_library_item(ctx)
  523. (sibling,) = await _sibling_snapshot(ctx)
  524. # A finished row is a record of what happened, not a spare part -- it keeps
  525. # its outcome and is not handed the archive.
  526. assert sibling.status == status
  527. assert sibling.archive_id is None
  528. # But the reference has to go: it is the only thing tying the row to the
  529. # cascade that would otherwise delete it, and a batch order counts its
  530. # progress from rows exactly like this one.
  531. assert sibling.library_file_id is None
  532. @pytest.mark.asyncio
  533. async def test_skipped_copy_is_repointed_because_it_can_come_back(queue_factory):
  534. ctx = await queue_factory(cleanup=True, siblings=({"status": "skipped"},))
  535. await _dispatch_library_item(ctx)
  536. _, _, archive = await _queue_snapshot(ctx)
  537. (sibling,) = await _sibling_snapshot(ctx)
  538. # Clearing the printer's previous-success gate puts skipped items back to
  539. # pending, so this one is only waiting -- not finished.
  540. assert sibling.status == "skipped"
  541. assert sibling.archive_id == archive.id
  542. assert sibling.library_file_id is None
  543. @pytest.mark.asyncio
  544. async def test_copies_are_untouched_when_the_dispatch_does_not_consume_the_file(queue_factory):
  545. ctx = await queue_factory(cleanup=False, siblings=({},))
  546. await _dispatch_library_item(ctx)
  547. item, library_file, _ = await _queue_snapshot(ctx)
  548. assert library_file is not None
  549. (sibling,) = await _sibling_snapshot(ctx)
  550. assert sibling.library_file_id == ctx.library_file_id
  551. assert sibling.archive_id is None
  552. @pytest.mark.asyncio
  553. async def test_cross_model_copy_keeps_its_other_candidate_instead_of_the_archive(queue_factory):
  554. ctx = await queue_factory(cleanup=True, siblings=({"variants": True, "extra_variant": True},))
  555. await _dispatch_library_item(ctx)
  556. (sibling,) = await _sibling_snapshot(ctx)
  557. # It still has somewhere to go, and that candidate carries its own target
  558. # model -- pointing it at this archive would print a file the matcher never
  559. # chose.
  560. assert sibling.archive_id is None
  561. assert sibling.library_file_id is None
  562. assert await _variant_files(ctx, sibling.id) == [ctx.other_library_file_id]
  563. @pytest.mark.asyncio
  564. async def test_copy_whose_only_candidate_was_consumed_is_repointed(queue_factory):
  565. ctx = await queue_factory(cleanup=True, siblings=({"variants": True},))
  566. await _dispatch_library_item(ctx)
  567. _, _, archive = await _queue_snapshot(ctx)
  568. (sibling,) = await _sibling_snapshot(ctx)
  569. # Its one candidate is gone. Without the re-point the resolver would hold it
  570. # pending forever with nothing left to dispatch.
  571. assert sibling.archive_id == archive.id
  572. assert sibling.library_file_id is None
  573. assert await _variant_files(ctx, sibling.id) == []
  574. @pytest.mark.asyncio
  575. async def test_oserror_during_unlink_logs_orphan_path_and_does_not_crash_dispatch(queue_factory, caplog):
  576. ctx = await queue_factory(cleanup=True, thumbnail_path="relative")
  577. original_unlink = type(ctx.source_path).unlink
  578. def unlink_with_source_failure(path, *args, **kwargs):
  579. if Path(path) == ctx.source_path:
  580. raise OSError("permission denied")
  581. return original_unlink(path, *args, **kwargs)
  582. with caplog.at_level("WARNING", logger="backend.app.services.print_scheduler"):
  583. await _dispatch_library_item(ctx, unlink_side_effect=unlink_with_source_failure)
  584. item, library_file, archive = await _queue_snapshot(ctx)
  585. assert item.status == "printing"
  586. assert item.archive_id == archive.id
  587. assert item.library_file_id is None
  588. assert library_file is None
  589. assert ctx.source_path.exists()
  590. assert not ctx.thumbnail_path.exists()
  591. assert ctx.archive_path.exists()
  592. assert "TRANSIENT_LIBRARY_FILE_ORPHAN" in caplog.text
  593. assert str(ctx.source_path) in caplog.text
  594. assert "permission denied" in caplog.text