print_queue.py 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461
  1. """API routes for print queue management."""
  2. import json
  3. import logging
  4. import zipfile
  5. from datetime import datetime, timezone
  6. from pathlib import Path
  7. import defusedxml.ElementTree as ET
  8. from fastapi import APIRouter, Depends, HTTPException, Query
  9. from sqlalchemy import and_, func, or_, select, update
  10. from sqlalchemy.ext.asyncio import AsyncSession
  11. from sqlalchemy.orm import selectinload
  12. from backend.app.core.auth import RequirePermissionIfAuthEnabled, require_ownership_permission
  13. from backend.app.core.config import settings
  14. from backend.app.core.database import get_db
  15. from backend.app.core.permissions import Permission
  16. from backend.app.models.archive import PrintArchive
  17. from backend.app.models.library import LibraryFile
  18. from backend.app.models.print_batch import PrintBatch
  19. from backend.app.models.print_queue import PrintQueueItem
  20. from backend.app.models.printer import Printer
  21. from backend.app.models.project import Project
  22. from backend.app.models.user import User
  23. from backend.app.schemas.print_queue import (
  24. PrintBatchCreate,
  25. PrintBatchResponse,
  26. PrintBatchUngroupResponse,
  27. PrintQueueBulkUpdate,
  28. PrintQueueBulkUpdateResponse,
  29. PrintQueueItemCreate,
  30. PrintQueueItemResponse,
  31. PrintQueueItemUpdate,
  32. PrintQueueReorder,
  33. )
  34. from backend.app.services.filament_deficit import compute_deficit_for_queue_item
  35. from backend.app.services.filament_requirements import overrides_for_plate
  36. from backend.app.services.notification_service import notification_service
  37. from backend.app.utils.printer_models import normalize_printer_model, normalize_printer_model_id
  38. from backend.app.utils.threemf_tools import (
  39. extract_bed_type_from_3mf,
  40. extract_filament_usage_from_3mf,
  41. extract_print_time_from_3mf,
  42. )
  43. logger = logging.getLogger(__name__)
  44. router = APIRouter(prefix="/queue", tags=["queue"])
  45. def _extract_filament_types_from_3mf(file_path: Path, plate_id: int | None = None) -> list[str]:
  46. """Extract unique filament types from a 3MF file.
  47. Args:
  48. file_path: Path to the 3MF file
  49. plate_id: Optional plate index to filter for (for multi-plate files)
  50. Returns:
  51. List of unique filament types (e.g., ["PLA", "PETG"])
  52. """
  53. types: set[str] = set()
  54. try:
  55. with zipfile.ZipFile(file_path, "r") as zf:
  56. if "Metadata/slice_info.config" not in zf.namelist():
  57. return []
  58. content = zf.read("Metadata/slice_info.config").decode()
  59. root = ET.fromstring(content)
  60. if plate_id is not None:
  61. # Find the plate element with matching index
  62. for plate_elem in root.findall(".//plate"):
  63. plate_index = None
  64. for meta in plate_elem.findall("metadata"):
  65. if meta.get("key") == "index":
  66. try:
  67. plate_index = int(meta.get("value", "0"))
  68. except ValueError:
  69. pass # Skip plate with unparseable index
  70. break
  71. if plate_index == plate_id:
  72. for filament_elem in plate_elem.findall("filament"):
  73. filament_type = filament_elem.get("type", "")
  74. used_g = filament_elem.get("used_g", "0")
  75. try:
  76. used_grams = float(used_g)
  77. except (ValueError, TypeError):
  78. used_grams = 0
  79. if used_grams > 0 and filament_type:
  80. types.add(filament_type)
  81. break
  82. else:
  83. # No plate_id specified - extract all filaments with used_g > 0
  84. for filament_elem in root.findall(".//filament"):
  85. filament_type = filament_elem.get("type", "")
  86. used_g = filament_elem.get("used_g", "0")
  87. try:
  88. used_grams = float(used_g)
  89. except (ValueError, TypeError):
  90. used_grams = 0
  91. if used_grams > 0 and filament_type:
  92. types.add(filament_type)
  93. except Exception as e:
  94. logger.warning("Failed to extract filament types from %s: %s", file_path, e)
  95. return sorted(types)
  96. # Local alias kept so existing call sites stay compact; the implementation lives
  97. # in utils/threemf_tools.py so the notification path (main.py) can reuse it
  98. # without importing from a routes module (#1785).
  99. _extract_print_time_from_3mf = extract_print_time_from_3mf
  100. async def _resolve_source_path(db: AsyncSession, item: PrintQueueItem) -> Path | None:
  101. """Resolve an existing queue item's source 3MF on disk, or None."""
  102. if item.archive_id:
  103. result = await db.execute(select(PrintArchive).where(PrintArchive.id == item.archive_id))
  104. archive = result.scalar_one_or_none()
  105. if archive:
  106. return settings.base_dir / archive.file_path
  107. elif item.library_file_id:
  108. result = await db.execute(LibraryFile.active().where(LibraryFile.id == item.library_file_id))
  109. library_file = result.scalar_one_or_none()
  110. if library_file:
  111. lib_path = Path(library_file.file_path)
  112. return lib_path if lib_path.is_absolute() else settings.base_dir / library_file.file_path
  113. return None
  114. def _enrich_response(item: PrintQueueItem) -> PrintQueueItemResponse:
  115. """Add nested archive/printer/library_file info to response."""
  116. # Parse ams_mapping from JSON string BEFORE model_validate
  117. ams_mapping_parsed = None
  118. if item.ams_mapping:
  119. try:
  120. ams_mapping_parsed = json.loads(item.ams_mapping)
  121. except json.JSONDecodeError:
  122. ams_mapping_parsed = None
  123. # Parse required_filament_types from JSON string
  124. required_filament_types_parsed = None
  125. if item.required_filament_types:
  126. try:
  127. required_filament_types_parsed = json.loads(item.required_filament_types)
  128. except json.JSONDecodeError:
  129. required_filament_types_parsed = None
  130. # Parse filament_overrides from JSON string
  131. filament_overrides_parsed = None
  132. if item.filament_overrides:
  133. try:
  134. filament_overrides_parsed = json.loads(item.filament_overrides)
  135. except json.JSONDecodeError:
  136. filament_overrides_parsed = None
  137. # Parse nozzle_mapping from JSON string (#1780 — H2C rack slicer-pick
  138. # preservation). Nullable opaque JSON blob stored verbatim from
  139. # BambuStudio's project_file; surface it parsed for the response model
  140. # and any future "edit print → nozzle" UI.
  141. nozzle_mapping_parsed = None
  142. if item.nozzle_mapping:
  143. try:
  144. nozzle_mapping_parsed = json.loads(item.nozzle_mapping)
  145. except json.JSONDecodeError:
  146. nozzle_mapping_parsed = None
  147. nozzles_info_parsed = None
  148. if item.nozzles_info:
  149. try:
  150. nozzles_info_parsed = json.loads(item.nozzles_info)
  151. except json.JSONDecodeError:
  152. nozzles_info_parsed = None
  153. # Create response with parsed ams_mapping
  154. item_dict = {
  155. "id": item.id,
  156. "printer_id": item.printer_id,
  157. "target_model": item.target_model,
  158. "target_location": item.target_location,
  159. "required_filament_types": required_filament_types_parsed,
  160. "filament_overrides": filament_overrides_parsed,
  161. "waiting_reason": item.waiting_reason,
  162. "archive_id": item.archive_id,
  163. "library_file_id": item.library_file_id,
  164. "position": item.position,
  165. "scheduled_time": item.scheduled_time,
  166. "require_previous_success": item.require_previous_success,
  167. "auto_off_after": item.auto_off_after,
  168. "manual_start": item.manual_start,
  169. "filament_short": bool(item.filament_short),
  170. "skip_filament_check": bool(item.skip_filament_check),
  171. "ams_mapping": ams_mapping_parsed,
  172. "plate_id": item.plate_id,
  173. "bed_levelling": item.bed_levelling,
  174. "flow_cali": item.flow_cali,
  175. "vibration_cali": item.vibration_cali,
  176. "layer_inspect": item.layer_inspect,
  177. "timelapse": item.timelapse,
  178. "use_ams": item.use_ams,
  179. "nozzle_offset_cali": item.nozzle_offset_cali,
  180. "preheat_override": item.preheat_override,
  181. "preheat_chamber_target_override": item.preheat_chamber_target_override,
  182. "status": item.status,
  183. "started_at": item.started_at,
  184. "completed_at": item.completed_at,
  185. "error_message": item.error_message,
  186. "created_at": item.created_at,
  187. # User tracking (Issue #206)
  188. "created_by_id": item.created_by_id,
  189. "created_by_username": item.created_by.username if item.created_by else None,
  190. # Batch grouping
  191. "batch_id": item.batch_id,
  192. "batch_name": item.batch.name if item.batch else None,
  193. # SJF scheduling
  194. "been_jumped": item.been_jumped,
  195. # Auto-print G-code injection
  196. "gcode_injection": item.gcode_injection,
  197. # H2C rack-swap nozzle pick (#1780)
  198. "nozzle_mapping": nozzle_mapping_parsed,
  199. "nozzles_info": nozzles_info_parsed,
  200. "cleanup_library_after_dispatch": item.cleanup_library_after_dispatch,
  201. }
  202. response = PrintQueueItemResponse(**item_dict)
  203. if item.archive:
  204. # Soft-deleted archive: files are gone from disk but the row stays
  205. # (its filament/cost contribution still flows into stats per #1343).
  206. # Suppress the archive-derived UI surface so the queue page doesn't
  207. # 404-storm the thumbnail / plates / plate-thumbnail endpoints — the
  208. # frontend's existing truthy gate on archive_thumbnail covers it
  209. # (#1348 follow-up). The archive_deleted flag lets the UI render a
  210. # "source deleted" badge on these rows.
  211. if item.archive.deleted_at is not None:
  212. response.archive_deleted = True
  213. else:
  214. response.archive_name = item.archive.print_name or item.archive.filename
  215. response.archive_thumbnail = item.archive.thumbnail_path
  216. response.print_time_seconds = item.archive.print_time_seconds
  217. response.filament_used_grams = item.archive.filament_used_grams
  218. response.filament_type = item.archive.filament_type
  219. response.filament_color = item.archive.filament_color
  220. response.layer_height = item.archive.layer_height
  221. response.nozzle_diameter = item.archive.nozzle_diameter
  222. response.sliced_for_model = item.archive.sliced_for_model
  223. response.bed_type = item.archive.bed_type
  224. if item.plate_id:
  225. archive_path = settings.base_dir / item.archive.file_path
  226. if archive_path.exists():
  227. plate_time = _extract_print_time_from_3mf(archive_path, item.plate_id)
  228. plate_weight = sum(
  229. f["used_g"] for f in extract_filament_usage_from_3mf(archive_path, item.plate_id)
  230. )
  231. plate_bed = extract_bed_type_from_3mf(archive_path, item.plate_id)
  232. if plate_time is not None:
  233. response.print_time_seconds = plate_time
  234. if plate_weight > 0:
  235. response.filament_used_grams = plate_weight
  236. if plate_bed:
  237. response.bed_type = plate_bed
  238. if item.library_file:
  239. response.library_file_name = (
  240. item.library_file.file_metadata.get("print_name") if item.library_file.file_metadata else None
  241. )
  242. if not response.library_file_name:
  243. response.library_file_name = item.library_file.filename
  244. response.library_file_thumbnail = item.library_file.thumbnail_path
  245. # Get metadata from library file if no archive
  246. if not item.archive and item.library_file.file_metadata:
  247. response.print_time_seconds = item.library_file.file_metadata.get("print_time_seconds")
  248. response.filament_used_grams = item.library_file.file_metadata.get("filament_used_grams")
  249. response.filament_type = item.library_file.file_metadata.get("filament_type")
  250. response.filament_color = item.library_file.file_metadata.get("filament_color")
  251. response.layer_height = item.library_file.file_metadata.get("layer_height")
  252. response.nozzle_diameter = item.library_file.file_metadata.get("nozzle_diameter")
  253. response.sliced_for_model = item.library_file.file_metadata.get("sliced_for_model")
  254. response.bed_type = item.library_file.file_metadata.get("bed_type")
  255. if item.plate_id:
  256. lib_path = Path(item.library_file.file_path)
  257. library_file_path = lib_path if lib_path.is_absolute() else settings.base_dir / item.library_file.file_path
  258. if library_file_path.exists():
  259. plate_time = _extract_print_time_from_3mf(library_file_path, item.plate_id)
  260. plate_weight = sum(
  261. f["used_g"] for f in extract_filament_usage_from_3mf(library_file_path, item.plate_id)
  262. )
  263. plate_bed = extract_bed_type_from_3mf(library_file_path, item.plate_id)
  264. if plate_time is not None:
  265. response.print_time_seconds = plate_time
  266. if plate_weight > 0:
  267. response.filament_used_grams = plate_weight
  268. if plate_bed:
  269. response.bed_type = plate_bed
  270. if item.printer:
  271. response.printer_name = item.printer.name
  272. return response
  273. @router.get("/", response_model=list[PrintQueueItemResponse])
  274. async def list_queue(
  275. printer_id: int | None = Query(None, description="Filter by printer (-1 for unassigned)"),
  276. status: str | None = Query(None, description="Filter by status"),
  277. target_model: str | None = Query(
  278. None, description="Filter by target model (also includes model-based items when combined with printer_id)"
  279. ),
  280. db: AsyncSession = Depends(get_db),
  281. auth_result: tuple[User | None, bool] = Depends(
  282. require_ownership_permission(
  283. Permission.QUEUE_READ_ALL,
  284. Permission.QUEUE_READ_OWN,
  285. )
  286. ),
  287. ):
  288. """List all queue items, optionally filtered by printer or status."""
  289. user, can_read_all = auth_result
  290. query = (
  291. select(PrintQueueItem)
  292. .options(
  293. selectinload(PrintQueueItem.archive),
  294. selectinload(PrintQueueItem.printer),
  295. selectinload(PrintQueueItem.library_file),
  296. selectinload(PrintQueueItem.created_by),
  297. selectinload(PrintQueueItem.batch),
  298. )
  299. .order_by(PrintQueueItem.printer_id.nulls_first(), PrintQueueItem.position)
  300. )
  301. if user is not None and not can_read_all:
  302. query = query.where(PrintQueueItem.created_by_id == user.id)
  303. if printer_id is not None:
  304. if printer_id == -1:
  305. # Special value: filter for unassigned items
  306. query = query.where(PrintQueueItem.printer_id.is_(None))
  307. else:
  308. # Resolve effective model: prefer explicit param, fall back to printer's DB model.
  309. # This ensures model-based "Any X" items are returned even when the frontend
  310. # doesn't send target_model (e.g. printer.model is NULL on the client side).
  311. effective_model = target_model
  312. if not effective_model:
  313. printer_row = (
  314. await db.execute(select(Printer.model).where(Printer.id == printer_id))
  315. ).scalar_one_or_none()
  316. effective_model = printer_row
  317. if effective_model:
  318. # Include both printer-specific items AND model-based (unassigned) items
  319. query = query.where(
  320. or_(
  321. PrintQueueItem.printer_id == printer_id,
  322. and_(
  323. PrintQueueItem.printer_id.is_(None),
  324. func.lower(PrintQueueItem.target_model) == effective_model.lower(),
  325. ),
  326. )
  327. )
  328. else:
  329. query = query.where(PrintQueueItem.printer_id == printer_id)
  330. elif target_model:
  331. query = query.where(func.lower(PrintQueueItem.target_model) == target_model.lower())
  332. if status:
  333. query = query.where(PrintQueueItem.status == status)
  334. result = await db.execute(query)
  335. items = result.scalars().all()
  336. return [_enrich_response(item) for item in items]
  337. @router.post("/", response_model=PrintQueueItemResponse)
  338. async def add_to_queue(
  339. data: PrintQueueItemCreate,
  340. db: AsyncSession = Depends(get_db),
  341. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.QUEUE_CREATE),
  342. ):
  343. """Add an item to the print queue."""
  344. # Normalize target_model (e.g., "Bambu Lab X1E" / "C13" -> "X1E")
  345. target_model_norm = None
  346. if data.target_model:
  347. target_model_norm = (
  348. normalize_printer_model(data.target_model)
  349. or normalize_printer_model_id(data.target_model)
  350. or data.target_model
  351. )
  352. # Validate that either archive_id or library_file_id is provided
  353. if not data.archive_id and not data.library_file_id:
  354. raise HTTPException(400, "Either archive_id or library_file_id must be provided")
  355. # Cannot specify both printer_id and target_model
  356. if data.printer_id and target_model_norm:
  357. raise HTTPException(400, "Cannot specify both printer_id and target_model")
  358. # Validate printer exists (if assigned)
  359. if data.printer_id is not None:
  360. result = await db.execute(select(Printer).where(Printer.id == data.printer_id))
  361. if not result.scalar_one_or_none():
  362. raise HTTPException(400, "Printer not found")
  363. # Validate target_model has active printers
  364. if target_model_norm:
  365. result = await db.execute(
  366. select(Printer).where(Printer.model == target_model_norm).where(Printer.is_active == True) # noqa: E712
  367. )
  368. if not result.scalars().first():
  369. raise HTTPException(400, f"No active printers for model: {target_model_norm}")
  370. # Validate archive exists (if provided) and get it for filament extraction
  371. archive = None
  372. if data.archive_id:
  373. result = await db.execute(select(PrintArchive).where(PrintArchive.id == data.archive_id))
  374. archive = result.scalar_one_or_none()
  375. if not archive:
  376. raise HTTPException(400, "Archive not found")
  377. # IDOR fix (maziggy/bambuddy-security #2): without this check, a
  378. # caller with QUEUE_CREATE could queue any user's archive even
  379. # without ARCHIVES_READ on it — Landon's PoC enumerated this on
  380. # admin's archives as operator1. Gate on ARCHIVES_READ_ALL OR
  381. # ownership of the archive. 404 (not 403) so we don't leak
  382. # "this id exists but you can't queue it" for enumeration.
  383. if (
  384. current_user is not None
  385. and not current_user.has_permission(Permission.ARCHIVES_READ_ALL.value)
  386. and archive.created_by_id != current_user.id
  387. ):
  388. raise HTTPException(404, "Archive not found")
  389. # Reprint perm gate (#1625): the legacy /archives/{id}/reprint endpoint
  390. # required ARCHIVES_REPRINT_OWN/ALL; the unified queue route must keep
  391. # that gate or an operator with QUEUE_CREATE could reprint via direct
  392. # API call even if explicitly denied reprint perm. Mirrors the
  393. # frontend `canModify('archives', 'reprint', ...)` helper:
  394. # REPRINT_ALL allows any archive, REPRINT_OWN allows own only,
  395. # ownerless archives require REPRINT_ALL (fail-closed).
  396. if current_user is not None:
  397. owns_archive = archive.created_by_id is not None and archive.created_by_id == current_user.id
  398. has_reprint = current_user.has_permission(Permission.ARCHIVES_REPRINT_ALL.value) or (
  399. owns_archive and current_user.has_permission(Permission.ARCHIVES_REPRINT_OWN.value)
  400. )
  401. if not has_reprint:
  402. raise HTTPException(
  403. status_code=403,
  404. detail="Permission archives:reprint_own or archives:reprint_all required",
  405. )
  406. # Validate library file exists (if provided) and get it for filament extraction
  407. library_file = None
  408. if data.library_file_id:
  409. result = await db.execute(LibraryFile.active().where(LibraryFile.id == data.library_file_id))
  410. library_file = result.scalar_one_or_none()
  411. if not library_file:
  412. raise HTTPException(400, "Library file not found")
  413. # Same shape: gate cross-user library-file queueing on LIBRARY_READ_ALL.
  414. if (
  415. current_user is not None
  416. and not current_user.has_permission(Permission.LIBRARY_READ_ALL.value)
  417. and library_file.created_by_id != current_user.id
  418. ):
  419. raise HTTPException(404, "Library file not found")
  420. # Bambu SD card is FAT32/exFAT — illegal filename chars would 553 at
  421. # FTP upload time (#1540). Reject at queue time so the user gets the
  422. # actionable error before waiting in queue.
  423. from backend.app.utils.filename import InvalidFilenameError, validate_print_filename
  424. try:
  425. validate_print_filename(library_file.filename)
  426. except InvalidFilenameError as e:
  427. raise HTTPException(400, str(e)) from e
  428. # Extract filament types for model-based assignment (used by scheduler for validation)
  429. required_filament_types = None
  430. file_path = None
  431. if target_model_norm:
  432. # Get file path from archive or library file
  433. if archive:
  434. file_path = settings.base_dir / archive.file_path
  435. elif library_file:
  436. lib_path = Path(library_file.file_path)
  437. file_path = lib_path if lib_path.is_absolute() else settings.base_dir / library_file.file_path
  438. if file_path and file_path.exists():
  439. filament_types = _extract_filament_types_from_3mf(file_path, data.plate_id)
  440. if filament_types:
  441. required_filament_types = json.dumps(filament_types)
  442. logger.info("Extracted filament types for model-based queue: %s", filament_types)
  443. # If filament overrides are provided, update required_filament_types to match override types
  444. filament_overrides_json = None
  445. if data.filament_overrides and target_model_norm:
  446. plate_overrides = overrides_for_plate(data.filament_overrides, file_path, data.plate_id)
  447. if plate_overrides:
  448. filament_overrides_json = json.dumps(plate_overrides)
  449. # Update required_filament_types from overrides so scheduler validates against overridden types
  450. override_types = sorted({o["type"] for o in plate_overrides if "type" in o})
  451. if override_types:
  452. # Merge with existing types (overrides may only cover some slots)
  453. existing_types = set(json.loads(required_filament_types)) if required_filament_types else set()
  454. # Replace types for overridden slots, keep others
  455. all_types = existing_types | set(override_types)
  456. required_filament_types = json.dumps(sorted(all_types))
  457. # Validate quantity
  458. quantity = max(1, data.quantity)
  459. # Validate batch_id if provided. Client passes batch_id when adding items
  460. # into a pre-created batch (multi-plate auto-batch or "Group as batch" flow).
  461. # 404 keeps the existing-id leak surface low.
  462. batch = None
  463. batch_id = None
  464. if data.batch_id is not None:
  465. result = await db.execute(select(PrintBatch).where(PrintBatch.id == data.batch_id))
  466. existing_batch = result.scalar_one_or_none()
  467. if not existing_batch:
  468. raise HTTPException(404, "Batch not found")
  469. if existing_batch.status != "active":
  470. raise HTTPException(400, "Cannot add items to a non-active batch")
  471. if (
  472. current_user is not None
  473. and existing_batch.created_by_id is not None
  474. and existing_batch.created_by_id != current_user.id
  475. and not current_user.has_permission(Permission.QUEUE_UPDATE_ALL.value)
  476. ):
  477. raise HTTPException(404, "Batch not found")
  478. batch = existing_batch
  479. batch_id = existing_batch.id
  480. # Create batch if quantity > 1 and no batch_id provided
  481. if batch_id is None and quantity > 1:
  482. # Derive batch name from source file
  483. batch_name_base = "Batch"
  484. if archive:
  485. batch_name_base = archive.print_name or archive.filename or "Batch"
  486. elif library_file:
  487. if library_file.file_metadata:
  488. batch_name_base = library_file.file_metadata.get("print_name") or library_file.filename
  489. else:
  490. batch_name_base = library_file.filename
  491. batch_name_base = batch_name_base.replace(".gcode.3mf", "").replace(".3mf", "")
  492. batch = PrintBatch(
  493. name=f"{batch_name_base} ×{quantity}",
  494. archive_id=data.archive_id,
  495. library_file_id=data.library_file_id,
  496. quantity=quantity,
  497. status="active",
  498. created_by_id=current_user.id if current_user else None,
  499. )
  500. db.add(batch)
  501. await db.flush() # Get batch.id before creating items
  502. batch_id = batch.id
  503. # Get queue scope for this printer (or for unassigned/model-based items).
  504. if data.printer_id is not None:
  505. queue_scope = (
  506. PrintQueueItem.printer_id == data.printer_id,
  507. PrintQueueItem.status == "pending",
  508. )
  509. else:
  510. # For unassigned/model-based items, scope across all unassigned.
  511. queue_scope = (
  512. PrintQueueItem.printer_id.is_(None),
  513. PrintQueueItem.status == "pending",
  514. )
  515. # Serialize concurrent queue inserts to the same scope (#1625-followup).
  516. # The race: two concurrent ASAP inserts both compute MAX(position) before
  517. # either commits; in an empty scope, both INSERT at position 1 (duplicate).
  518. # In a non-empty scope, Postgres's row-level locks on the UPDATE shift
  519. # serialize naturally, but the empty-scope path has no rows to lock.
  520. # A transaction-scoped advisory lock keyed on the printer_id closes that
  521. # window; the lock is released automatically at commit/rollback. Different
  522. # printers don't contend. SQLite serializes writes implicitly so this is a
  523. # no-op there.
  524. #
  525. # Dialect is checked against the actual session binding, NOT the
  526. # `is_sqlite()` helper, because the test fixture overrides `get_db` with a
  527. # SQLite engine while `settings.database_url` still points at Postgres
  528. # (the helper reads settings). Inspecting the connection directly is the
  529. # right shape for any code that mutates SQL based on the live dialect.
  530. from sqlalchemy import text
  531. bind = db.get_bind()
  532. if bind.dialect.name == "postgresql":
  533. scope_key = data.printer_id if data.printer_id is not None else 0
  534. # 1625 namespaces the lock so it can't collide with other advisory
  535. # locks elsewhere in the codebase.
  536. await db.execute(text("SELECT pg_advisory_xact_lock(1625, :k)"), {"k": scope_key})
  537. insert_position = max(1, data.insert_position or 1)
  538. if data.insert_at_top or data.insert_position is not None:
  539. result = await db.execute(select(func.max(PrintQueueItem.position)).where(*queue_scope))
  540. max_pos = result.scalar() or 0
  541. insert_position = min(insert_position, max_pos + 1)
  542. await db.execute(
  543. update(PrintQueueItem)
  544. .where(*queue_scope)
  545. .where(PrintQueueItem.position >= insert_position)
  546. .values(position=PrintQueueItem.position + quantity)
  547. )
  548. start_position = insert_position
  549. else:
  550. result = await db.execute(select(func.max(PrintQueueItem.position)).where(*queue_scope))
  551. max_pos = result.scalar() or 0
  552. start_position = max_pos + 1
  553. # Resolve print_time_seconds for SJF scheduling (cache on item at creation)
  554. cached_print_time = None
  555. if archive:
  556. cached_print_time = archive.print_time_seconds
  557. if data.plate_id:
  558. archive_path = settings.base_dir / archive.file_path
  559. if archive_path.exists():
  560. plate_time = _extract_print_time_from_3mf(archive_path, data.plate_id)
  561. if plate_time is not None:
  562. cached_print_time = plate_time
  563. elif library_file:
  564. if library_file.file_metadata:
  565. cached_print_time = library_file.file_metadata.get("print_time_seconds")
  566. if data.plate_id:
  567. lib_path = Path(library_file.file_path)
  568. library_file_path = lib_path if lib_path.is_absolute() else settings.base_dir / library_file.file_path
  569. if library_file_path.exists():
  570. plate_time = _extract_print_time_from_3mf(library_file_path, data.plate_id)
  571. if plate_time is not None:
  572. cached_print_time = plate_time
  573. # Validate project exists before insert so a bogus ID yields 404, not an FK-constraint 500
  574. if data.project_id is not None:
  575. project_result = await db.execute(select(Project).where(Project.id == data.project_id))
  576. if not project_result.scalar_one_or_none():
  577. raise HTTPException(status_code=404, detail="Project not found")
  578. ams_mapping_json = json.dumps(data.ams_mapping) if data.ams_mapping else None
  579. items = []
  580. for i in range(quantity):
  581. item = PrintQueueItem(
  582. printer_id=data.printer_id,
  583. target_model=target_model_norm,
  584. target_location=data.target_location,
  585. required_filament_types=required_filament_types,
  586. filament_overrides=filament_overrides_json,
  587. archive_id=data.archive_id,
  588. library_file_id=data.library_file_id,
  589. scheduled_time=data.scheduled_time,
  590. require_previous_success=data.require_previous_success,
  591. auto_off_after=data.auto_off_after,
  592. manual_start=data.manual_start,
  593. skip_filament_check=data.skip_filament_check,
  594. ams_mapping=ams_mapping_json,
  595. plate_id=data.plate_id,
  596. bed_levelling=data.bed_levelling,
  597. flow_cali=data.flow_cali,
  598. vibration_cali=data.vibration_cali,
  599. layer_inspect=data.layer_inspect,
  600. timelapse=data.timelapse,
  601. use_ams=data.use_ams,
  602. nozzle_offset_cali=data.nozzle_offset_cali,
  603. preheat_override=data.preheat_override,
  604. preheat_chamber_target_override=data.preheat_chamber_target_override,
  605. gcode_injection=data.gcode_injection,
  606. cleanup_library_after_dispatch=data.cleanup_library_after_dispatch,
  607. project_id=data.project_id,
  608. position=start_position + i,
  609. status="pending",
  610. created_by_id=current_user.id if current_user else None,
  611. batch_id=batch_id,
  612. print_time_seconds=cached_print_time,
  613. )
  614. db.add(item)
  615. items.append(item)
  616. await db.commit()
  617. # Refresh the first item for the response
  618. item = items[0]
  619. await db.refresh(item)
  620. await db.refresh(item, ["archive", "printer", "library_file", "created_by", "batch"])
  621. source_name = f"archive {data.archive_id}" if data.archive_id else f"library file {data.library_file_id}"
  622. target_desc = data.printer_id or (f"model {target_model_norm}" if target_model_norm else "unassigned")
  623. qty_desc = f" (×{quantity})" if quantity > 1 else ""
  624. logger.info("Added %s to queue for %s%s", source_name, target_desc, qty_desc)
  625. # MQTT relay - publish queue job added
  626. try:
  627. from backend.app.services.mqtt_relay import mqtt_relay
  628. await mqtt_relay.on_queue_job_added(
  629. job_id=item.id,
  630. filename=item.archive.filename if item.archive else "",
  631. printer_id=item.printer_id,
  632. printer_name=item.printer.name if item.printer else None,
  633. )
  634. except Exception:
  635. pass # Don't fail queue add if MQTT fails
  636. # Send notification for job added
  637. try:
  638. job_name = (
  639. item.archive.filename
  640. if item.archive
  641. else item.library_file.filename
  642. if item.library_file
  643. else f"Job #{item.id}"
  644. )
  645. job_name = job_name.replace(".gcode.3mf", "").replace(".3mf", "")
  646. if quantity > 1:
  647. job_name = f"{job_name} ×{quantity}"
  648. target = (
  649. item.printer.name if item.printer else (f"Any {item.target_model}" if target_model_norm else "Unassigned")
  650. )
  651. await notification_service.on_queue_job_added(
  652. job_name=job_name,
  653. target=target,
  654. db=db,
  655. printer_id=item.printer_id,
  656. printer_name=item.printer.name if item.printer else None,
  657. )
  658. except Exception:
  659. pass # Don't fail queue add if notification fails
  660. return _enrich_response(item)
  661. @router.patch("/bulk", response_model=PrintQueueBulkUpdateResponse)
  662. async def bulk_update_queue_items(
  663. data: PrintQueueBulkUpdate,
  664. db: AsyncSession = Depends(get_db),
  665. auth_result: tuple[User | None, bool] = Depends(
  666. require_ownership_permission(
  667. Permission.QUEUE_UPDATE_ALL,
  668. Permission.QUEUE_UPDATE_OWN,
  669. )
  670. ),
  671. ):
  672. """Bulk update multiple queue items with the same values.
  673. Only pending items can be updated. Non-pending items are skipped.
  674. Items not owned by the user are also skipped (unless user has *_all permission).
  675. """
  676. user, can_modify_all = auth_result
  677. if not data.item_ids:
  678. raise HTTPException(400, "No item IDs provided")
  679. # Get fields to update (exclude item_ids and unset fields)
  680. update_data = data.model_dump(exclude={"item_ids"}, exclude_unset=True)
  681. if not update_data:
  682. raise HTTPException(400, "No fields to update")
  683. # Validate printer_id if being changed
  684. if "printer_id" in update_data and update_data["printer_id"] is not None:
  685. result = await db.execute(select(Printer).where(Printer.id == update_data["printer_id"]))
  686. if not result.scalar_one_or_none():
  687. raise HTTPException(400, "Printer not found")
  688. # Fetch all items
  689. result = await db.execute(select(PrintQueueItem).where(PrintQueueItem.id.in_(data.item_ids)))
  690. items = result.scalars().all()
  691. updated_count = 0
  692. skipped_count = 0
  693. for item in items:
  694. if item.status != "pending":
  695. skipped_count += 1
  696. continue
  697. # Ownership check
  698. if not can_modify_all and item.created_by_id != user.id:
  699. skipped_count += 1
  700. continue
  701. for field, value in update_data.items():
  702. setattr(item, field, value)
  703. updated_count += 1
  704. await db.commit()
  705. logger.info("Bulk updated %s queue items, skipped %s", updated_count, skipped_count)
  706. return PrintQueueBulkUpdateResponse(
  707. updated_count=updated_count,
  708. skipped_count=skipped_count,
  709. message=f"Updated {updated_count} items"
  710. + (f", skipped {skipped_count} non-pending/not-owned" if skipped_count else ""),
  711. )
  712. # --- Batch endpoints ---
  713. @router.post("/batches", response_model=PrintBatchResponse)
  714. async def create_batch(
  715. data: PrintBatchCreate,
  716. db: AsyncSession = Depends(get_db),
  717. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.QUEUE_CREATE),
  718. ):
  719. """Create a batch.
  720. Two modes:
  721. * ``item_ids`` provided: assign the listed pending queue items to a new
  722. batch ("Group as batch" UI action).
  723. * ``item_ids`` omitted/empty: create an empty batch so the client can
  724. pass the returned ``id`` on subsequent ``POST /queue/`` calls. Used by
  725. the multi-plate auto-batch flow in PrintModal.
  726. """
  727. if not data.name or not data.name.strip():
  728. raise HTTPException(400, "Batch name is required")
  729. batch = PrintBatch(
  730. name=data.name.strip()[:255],
  731. archive_id=data.archive_id,
  732. library_file_id=data.library_file_id,
  733. quantity=len(data.item_ids) if data.item_ids else 1,
  734. status="active",
  735. created_by_id=current_user.id if current_user else None,
  736. )
  737. db.add(batch)
  738. await db.flush() # Need batch.id before assigning to items
  739. assigned = 0
  740. if data.item_ids:
  741. result = await db.execute(select(PrintQueueItem).where(PrintQueueItem.id.in_(data.item_ids)))
  742. items = result.scalars().all()
  743. for item in items:
  744. if item.status != "pending":
  745. continue
  746. if item.batch_id is not None:
  747. continue
  748. if (
  749. current_user is not None
  750. and item.created_by_id != current_user.id
  751. and not current_user.has_permission(Permission.QUEUE_UPDATE_ALL.value)
  752. ):
  753. continue
  754. item.batch_id = batch.id
  755. assigned += 1
  756. batch.quantity = max(assigned, 1)
  757. await db.commit()
  758. await db.refresh(batch)
  759. logger.info("Created batch %s '%s' with %s assigned items", batch.id, batch.name, assigned)
  760. return await _build_batch_response(db, batch)
  761. @router.post("/batches/{batch_id}/ungroup", response_model=PrintBatchUngroupResponse)
  762. async def ungroup_batch(
  763. batch_id: int,
  764. db: AsyncSession = Depends(get_db),
  765. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.QUEUE_UPDATE_OWN),
  766. ):
  767. """Disband a batch: clear batch_id from all members and delete the batch row.
  768. Items stay in the queue. Only ungroups items the caller owns (unless they
  769. hold QUEUE_UPDATE_ALL). A batch with all members ungrouped is deleted.
  770. """
  771. result = await db.execute(select(PrintBatch).where(PrintBatch.id == batch_id))
  772. batch = result.scalar_one_or_none()
  773. if not batch:
  774. raise HTTPException(404, "Batch not found")
  775. can_modify_all = current_user is None or current_user.has_permission(Permission.QUEUE_UPDATE_ALL.value)
  776. if not can_modify_all and batch.created_by_id != (current_user.id if current_user else None):
  777. raise HTTPException(404, "Batch not found")
  778. result = await db.execute(select(PrintQueueItem).where(PrintQueueItem.batch_id == batch_id))
  779. items = result.scalars().all()
  780. ungrouped = 0
  781. remaining = 0
  782. for item in items:
  783. if not can_modify_all and item.created_by_id != (current_user.id if current_user else None):
  784. remaining += 1
  785. continue
  786. item.batch_id = None
  787. ungrouped += 1
  788. # Delete the batch row only when all members were ungrouped — otherwise it
  789. # still owns the items the caller couldn't touch.
  790. if remaining == 0:
  791. await db.delete(batch)
  792. await db.commit()
  793. logger.info("Ungrouped batch %s (%s items)", batch_id, ungrouped)
  794. return PrintBatchUngroupResponse(
  795. ungrouped_count=ungrouped,
  796. message=f"Ungrouped {ungrouped} item(s)",
  797. )
  798. @router.get("/batches", response_model=list[PrintBatchResponse])
  799. async def list_batches(
  800. status: str | None = Query(None, description="Filter by status (active, completed, cancelled)"),
  801. db: AsyncSession = Depends(get_db),
  802. auth_result: tuple[User | None, bool] = Depends(
  803. require_ownership_permission(
  804. Permission.QUEUE_READ_ALL,
  805. Permission.QUEUE_READ_OWN,
  806. )
  807. ),
  808. ):
  809. """List all print batches with progress stats."""
  810. current_user, can_read_all = auth_result
  811. query = select(PrintBatch).order_by(PrintBatch.created_at.desc())
  812. if status:
  813. query = query.where(PrintBatch.status == status)
  814. if current_user is not None and not can_read_all:
  815. query = query.where(PrintBatch.created_by_id == current_user.id)
  816. result = await db.execute(query)
  817. batches = result.scalars().all()
  818. responses = []
  819. for batch in batches:
  820. responses.append(await _build_batch_response(db, batch))
  821. return responses
  822. @router.get("/batches/{batch_id}", response_model=PrintBatchResponse)
  823. async def get_batch(
  824. batch_id: int,
  825. db: AsyncSession = Depends(get_db),
  826. auth_result: tuple[User | None, bool] = Depends(
  827. require_ownership_permission(
  828. Permission.QUEUE_READ_ALL,
  829. Permission.QUEUE_READ_OWN,
  830. )
  831. ),
  832. ):
  833. """Get a print batch with progress stats."""
  834. current_user, can_read_all = auth_result
  835. result = await db.execute(select(PrintBatch).where(PrintBatch.id == batch_id))
  836. batch = result.scalar_one_or_none()
  837. if not batch:
  838. raise HTTPException(404, "Batch not found")
  839. if (
  840. current_user is not None
  841. and not can_read_all
  842. and (batch.created_by_id is None or batch.created_by_id != current_user.id)
  843. ):
  844. raise HTTPException(404, "Batch not found")
  845. return await _build_batch_response(db, batch)
  846. @router.delete("/batches/{batch_id}")
  847. async def cancel_batch(
  848. batch_id: int,
  849. db: AsyncSession = Depends(get_db),
  850. current_user: User | None = RequirePermissionIfAuthEnabled(Permission.QUEUE_DELETE_ALL),
  851. ):
  852. """Cancel all pending items in a batch and mark batch as cancelled."""
  853. result = await db.execute(select(PrintBatch).where(PrintBatch.id == batch_id))
  854. batch = result.scalar_one_or_none()
  855. if not batch:
  856. raise HTTPException(404, "Batch not found")
  857. # Cancel all pending queue items in this batch
  858. result = await db.execute(
  859. select(PrintQueueItem).where(and_(PrintQueueItem.batch_id == batch_id, PrintQueueItem.status == "pending"))
  860. )
  861. pending_items = result.scalars().all()
  862. cancelled_count = 0
  863. for item in pending_items:
  864. item.status = "cancelled"
  865. cancelled_count += 1
  866. batch.status = "cancelled"
  867. await db.commit()
  868. return {"message": f"Batch cancelled, {cancelled_count} pending items cancelled"}
  869. async def _build_batch_response(db: AsyncSession, batch: PrintBatch) -> PrintBatchResponse:
  870. """Build a batch response with derived counts from queue items."""
  871. # Count queue items by status
  872. result = await db.execute(
  873. select(PrintQueueItem.status, func.count(PrintQueueItem.id))
  874. .where(PrintQueueItem.batch_id == batch.id)
  875. .group_by(PrintQueueItem.status)
  876. )
  877. status_counts = {row[0]: row[1] for row in result.fetchall()}
  878. # Load created_by for username
  879. created_by_username = None
  880. if batch.created_by_id:
  881. result = await db.execute(select(User).where(User.id == batch.created_by_id))
  882. user = result.scalar_one_or_none()
  883. if user:
  884. created_by_username = user.username
  885. return PrintBatchResponse(
  886. id=batch.id,
  887. name=batch.name,
  888. archive_id=batch.archive_id,
  889. library_file_id=batch.library_file_id,
  890. quantity=batch.quantity,
  891. status=batch.status,
  892. created_at=batch.created_at,
  893. created_by_id=batch.created_by_id,
  894. created_by_username=created_by_username,
  895. pending_count=status_counts.get("pending", 0),
  896. printing_count=status_counts.get("printing", 0),
  897. completed_count=status_counts.get("completed", 0),
  898. failed_count=status_counts.get("failed", 0),
  899. cancelled_count=status_counts.get("cancelled", 0),
  900. )
  901. @router.get("/{item_id}", response_model=PrintQueueItemResponse)
  902. async def get_queue_item(
  903. item_id: int,
  904. db: AsyncSession = Depends(get_db),
  905. auth_result: tuple[User | None, bool] = Depends(
  906. require_ownership_permission(
  907. Permission.QUEUE_READ_ALL,
  908. Permission.QUEUE_READ_OWN,
  909. )
  910. ),
  911. ):
  912. """Get a specific queue item."""
  913. current_user, can_read_all = auth_result
  914. result = await db.execute(
  915. select(PrintQueueItem)
  916. .options(
  917. selectinload(PrintQueueItem.archive),
  918. selectinload(PrintQueueItem.printer),
  919. selectinload(PrintQueueItem.library_file),
  920. selectinload(PrintQueueItem.created_by),
  921. selectinload(PrintQueueItem.batch),
  922. )
  923. .where(PrintQueueItem.id == item_id)
  924. )
  925. item = result.scalar_one_or_none()
  926. if not item:
  927. raise HTTPException(404, "Queue item not found")
  928. if (
  929. current_user is not None
  930. and not can_read_all
  931. and (item.created_by_id is None or item.created_by_id != current_user.id)
  932. ):
  933. raise HTTPException(404, "Queue item not found")
  934. return _enrich_response(item)
  935. @router.patch("/{item_id}", response_model=PrintQueueItemResponse)
  936. async def update_queue_item(
  937. item_id: int,
  938. data: PrintQueueItemUpdate,
  939. db: AsyncSession = Depends(get_db),
  940. auth_result: tuple[User | None, bool] = Depends(
  941. require_ownership_permission(
  942. Permission.QUEUE_UPDATE_ALL,
  943. Permission.QUEUE_UPDATE_OWN,
  944. )
  945. ),
  946. ):
  947. """Update a queue item."""
  948. user, can_modify_all = auth_result
  949. result = await db.execute(select(PrintQueueItem).where(PrintQueueItem.id == item_id))
  950. item = result.scalar_one_or_none()
  951. if not item:
  952. raise HTTPException(404, "Queue item not found")
  953. # Ownership check
  954. if not can_modify_all:
  955. if item.created_by_id != user.id:
  956. raise HTTPException(403, "You can only update your own queue items")
  957. if item.status != "pending":
  958. raise HTTPException(400, "Can only update pending items")
  959. update_data = data.model_dump(exclude_unset=True)
  960. # Normalize target_model if being updated
  961. if "target_model" in update_data and update_data["target_model"]:
  962. update_data["target_model"] = (
  963. normalize_printer_model(update_data["target_model"])
  964. or normalize_printer_model_id(update_data["target_model"])
  965. or update_data["target_model"]
  966. )
  967. # Cannot specify both printer_id and target_model
  968. new_printer_id = update_data.get("printer_id", item.printer_id)
  969. new_target_model = update_data.get("target_model", item.target_model)
  970. if new_printer_id and new_target_model:
  971. raise HTTPException(400, "Cannot specify both printer_id and target_model")
  972. # Validate new printer_id if being changed (and not None)
  973. if "printer_id" in update_data and update_data["printer_id"] is not None:
  974. result = await db.execute(select(Printer).where(Printer.id == update_data["printer_id"]))
  975. if not result.scalar_one_or_none():
  976. raise HTTPException(400, "Printer not found")
  977. # Validate target_model has active printers
  978. if "target_model" in update_data and update_data["target_model"]:
  979. result = await db.execute(
  980. select(Printer).where(Printer.model == update_data["target_model"]).where(Printer.is_active == True) # noqa: E712
  981. )
  982. if not result.scalars().first():
  983. raise HTTPException(400, f"No active printers for model: {update_data['target_model']}")
  984. # Serialize ams_mapping to JSON for TEXT column storage
  985. if "ams_mapping" in update_data:
  986. update_data["ams_mapping"] = json.dumps(update_data["ams_mapping"]) if update_data["ams_mapping"] else None
  987. # Serialize filament_overrides to JSON for TEXT column storage, keeping only
  988. # the slots this item's plate actually prints (#2551 — same shared-override
  989. # list the create path narrows).
  990. if "filament_overrides" in update_data:
  991. overrides = update_data["filament_overrides"]
  992. if overrides:
  993. overrides = overrides_for_plate(
  994. overrides,
  995. await _resolve_source_path(db, item),
  996. update_data.get("plate_id", item.plate_id),
  997. )
  998. update_data["filament_overrides"] = json.dumps(overrides) if overrides else None
  999. # Serialize H2C rack-swap nozzle pick (#1780) to JSON for TEXT column
  1000. # storage; same Text-as-opaque-blob convention as ams_mapping above.
  1001. if "nozzle_mapping" in update_data:
  1002. update_data["nozzle_mapping"] = (
  1003. json.dumps(update_data["nozzle_mapping"]) if update_data["nozzle_mapping"] else None
  1004. )
  1005. for field, value in update_data.items():
  1006. setattr(item, field, value)
  1007. await db.commit()
  1008. await db.refresh(item, ["archive", "printer", "library_file", "created_by", "batch"])
  1009. logger.info("Updated queue item %s", item_id)
  1010. return _enrich_response(item)
  1011. @router.delete("/{item_id}")
  1012. async def delete_queue_item(
  1013. item_id: int,
  1014. db: AsyncSession = Depends(get_db),
  1015. auth_result: tuple[User | None, bool] = Depends(
  1016. require_ownership_permission(
  1017. Permission.QUEUE_DELETE_ALL,
  1018. Permission.QUEUE_DELETE_OWN,
  1019. )
  1020. ),
  1021. ):
  1022. """Remove an item from the queue."""
  1023. user, can_modify_all = auth_result
  1024. result = await db.execute(select(PrintQueueItem).where(PrintQueueItem.id == item_id))
  1025. item = result.scalar_one_or_none()
  1026. if not item:
  1027. raise HTTPException(404, "Queue item not found")
  1028. # Ownership check
  1029. if not can_modify_all:
  1030. if item.created_by_id != user.id:
  1031. raise HTTPException(403, "You can only delete your own queue items")
  1032. if item.status == "printing":
  1033. raise HTTPException(400, "Cannot delete item that is currently printing")
  1034. await db.delete(item)
  1035. await db.commit()
  1036. logger.info("Deleted queue item %s", item_id)
  1037. return {"message": "Queue item deleted"}
  1038. @router.post("/reorder")
  1039. async def reorder_queue(
  1040. data: PrintQueueReorder,
  1041. db: AsyncSession = Depends(get_db),
  1042. _: User | None = RequirePermissionIfAuthEnabled(Permission.QUEUE_UPDATE_ALL),
  1043. ):
  1044. """Bulk update positions for queue items."""
  1045. for reorder_item in data.items:
  1046. result = await db.execute(select(PrintQueueItem).where(PrintQueueItem.id == reorder_item.id))
  1047. item = result.scalar_one_or_none()
  1048. if item and item.status == "pending":
  1049. item.position = reorder_item.position
  1050. await db.commit()
  1051. logger.info("Reordered %s queue items", len(data.items))
  1052. return {"message": f"Reordered {len(data.items)} items"}
  1053. @router.post("/printer/{printer_id}/resume")
  1054. async def resume_queue_after_failure(
  1055. printer_id: int,
  1056. db: AsyncSession = Depends(get_db),
  1057. _: User | None = RequirePermissionIfAuthEnabled(Permission.QUEUE_UPDATE_ALL),
  1058. ):
  1059. """Clear the previous-success gate for a printer and restore skipped items.
  1060. Single atomic op (#1818):
  1061. * Sets ``gate_acknowledged=True`` on every ``failed`` / ``aborted`` queue
  1062. item for this printer that's still in the scheduler's lookback window,
  1063. so the next ``_check_previous_success`` call ignores them.
  1064. * Restores ``skipped`` items whose ``error_message`` matches the
  1065. scheduler's exact "Previous print failed or was aborted" gate string
  1066. back to ``pending`` (clears ``error_message`` + ``completed_at``).
  1067. Returns counts so the UI can render a precise toast. No-op endpoint
  1068. (zero counts) when called against a printer with no gate to clear.
  1069. """
  1070. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  1071. printer = result.scalar_one_or_none()
  1072. if not printer:
  1073. raise HTTPException(404, "Printer not found")
  1074. ack_result = await db.execute(
  1075. select(PrintQueueItem)
  1076. .where(PrintQueueItem.printer_id == printer_id)
  1077. .where(PrintQueueItem.status.in_(["failed", "aborted"]))
  1078. .where(PrintQueueItem.gate_acknowledged == False) # noqa: E712
  1079. )
  1080. to_ack = ack_result.scalars().all()
  1081. for failed_item in to_ack:
  1082. failed_item.gate_acknowledged = True
  1083. restore_result = await db.execute(
  1084. select(PrintQueueItem)
  1085. .where(PrintQueueItem.printer_id == printer_id)
  1086. .where(PrintQueueItem.status == "skipped")
  1087. .where(PrintQueueItem.error_message == "Previous print failed or was aborted")
  1088. )
  1089. to_restore = restore_result.scalars().all()
  1090. for skipped_item in to_restore:
  1091. skipped_item.status = "pending"
  1092. skipped_item.error_message = None
  1093. skipped_item.completed_at = None
  1094. await db.commit()
  1095. logger.info(
  1096. "Resume after failure on printer %s: acknowledged %d failure(s), restored %d skipped item(s)",
  1097. printer_id,
  1098. len(to_ack),
  1099. len(to_restore),
  1100. )
  1101. return {"acknowledged": len(to_ack), "restored": len(to_restore)}
  1102. @router.post("/{item_id}/cancel")
  1103. async def cancel_queue_item(
  1104. item_id: int,
  1105. db: AsyncSession = Depends(get_db),
  1106. auth_result: tuple[User | None, bool] = Depends(
  1107. require_ownership_permission(
  1108. Permission.QUEUE_UPDATE_ALL,
  1109. Permission.QUEUE_UPDATE_OWN,
  1110. )
  1111. ),
  1112. ):
  1113. """Cancel a pending queue item."""
  1114. user, can_modify_all = auth_result
  1115. result = await db.execute(select(PrintQueueItem).where(PrintQueueItem.id == item_id))
  1116. item = result.scalar_one_or_none()
  1117. if not item:
  1118. raise HTTPException(404, "Queue item not found")
  1119. # Ownership check
  1120. if not can_modify_all:
  1121. if item.created_by_id != user.id:
  1122. raise HTTPException(403, "You can only cancel your own queue items")
  1123. if item.status not in ("pending",):
  1124. raise HTTPException(400, f"Cannot cancel item with status '{item.status}'")
  1125. item.status = "cancelled"
  1126. item.completed_at = datetime.now(timezone.utc)
  1127. await db.commit()
  1128. logger.info("Cancelled queue item %s", item_id)
  1129. return {"message": "Queue item cancelled"}
  1130. @router.post("/{item_id}/stop")
  1131. async def stop_queue_item(
  1132. item_id: int,
  1133. db: AsyncSession = Depends(get_db),
  1134. auth_result: tuple[User | None, bool] = Depends(
  1135. require_ownership_permission(
  1136. Permission.QUEUE_UPDATE_ALL,
  1137. Permission.QUEUE_UPDATE_OWN,
  1138. )
  1139. ),
  1140. ):
  1141. """Stop an actively printing queue item.
  1142. Ownership-scoped (#1625-followup): callers with QUEUE_UPDATE_OWN can stop
  1143. their own items; callers with QUEUE_UPDATE_ALL can stop any item. Mirrors
  1144. the /cancel shape. Pre-fix this required QUEUE_UPDATE_ALL — Operators
  1145. holding only _OWN saw the Stop button in the queue UI but got 403 on click.
  1146. """
  1147. from backend.app.services.printer_manager import printer_manager
  1148. user, can_modify_all = auth_result
  1149. result = await db.execute(select(PrintQueueItem).where(PrintQueueItem.id == item_id))
  1150. item = result.scalar_one_or_none()
  1151. if not item:
  1152. raise HTTPException(404, "Queue item not found")
  1153. # Ownership check — mirrors /cancel. Ownerless items (created_by_id IS NULL)
  1154. # require _ALL: stop is destructive and an _OWN holder can't claim "they
  1155. # own it" the way /start does (#1670).
  1156. if not can_modify_all and user is not None:
  1157. if item.created_by_id is None or item.created_by_id != user.id:
  1158. raise HTTPException(403, "You can only stop your own queue items")
  1159. if item.status != "printing":
  1160. raise HTTPException(400, f"Can only stop items that are printing, current status: '{item.status}'")
  1161. # Capture values we need for background task
  1162. printer_id = item.printer_id
  1163. auto_off_after = item.auto_off_after
  1164. # Try to send stop command to printer
  1165. stop_sent = False
  1166. try:
  1167. stop_sent = printer_manager.stop_print(printer_id)
  1168. if not stop_sent:
  1169. logger.warning("stop_print returned False for printer %s - printer may not be connected", printer_id)
  1170. except Exception as e:
  1171. logger.error("Error sending stop command for queue item %s: %s", item_id, e)
  1172. # Mark this printer as user-stopped BEFORE the first await so that if the
  1173. # MQTT on_print_complete callback fires during the db.commit() yield the flag
  1174. # is already set and the "failed" status will be correctly overridden to
  1175. # "cancelled" (preventing a spurious "print failed" notification).
  1176. try:
  1177. from backend.app.main import mark_printer_stopped_by_user
  1178. mark_printer_stopped_by_user(printer_id)
  1179. except Exception as _mark_err:
  1180. logger.warning("Failed to mark printer %s as user-stopped: %s", printer_id, _mark_err)
  1181. # Update queue item status regardless - if printer is off, print is already stopped
  1182. item.status = "cancelled"
  1183. item.completed_at = datetime.now(timezone.utc)
  1184. item.error_message = "Stopped by user" if stop_sent else "Stopped by user (printer was offline)"
  1185. await db.commit()
  1186. logger.info("Stopped printing queue item %s (stop command sent: %s)", item_id, stop_sent)
  1187. # Schedule power-off if the queue item opted in. Delegates to the smart-plug
  1188. # manager so the off honours each plug's configured strategy (time delay or
  1189. # temperature threshold), is cancelled if the printer starts printing again,
  1190. # and never cuts power on a loaded print (#1890). Previously an inline block
  1191. # hardcoded a 50°C / 600s cooldown wait and powered off on the timeout
  1192. # regardless of print state.
  1193. if auto_off_after:
  1194. from backend.app.services.smart_plug_manager import smart_plug_manager
  1195. try:
  1196. await smart_plug_manager.schedule_off_after_queue_job(printer_id, db)
  1197. except Exception as e:
  1198. logger.warning("Auto-off: Failed to schedule power-off for printer %s: %s", printer_id, e)
  1199. return {"message": "Print stopped" if stop_sent else "Queue item cancelled (printer was offline)"}
  1200. @router.post("/{item_id}/start")
  1201. async def start_queue_item(
  1202. item_id: int,
  1203. skip_filament_check: bool = Query(default=False),
  1204. db: AsyncSession = Depends(get_db),
  1205. auth_result: tuple[User | None, bool] = Depends(
  1206. require_ownership_permission(
  1207. Permission.QUEUE_UPDATE_ALL,
  1208. Permission.QUEUE_UPDATE_OWN,
  1209. )
  1210. ),
  1211. ):
  1212. """Manually start a staged (manual_start) queue item.
  1213. Ownership-scoped (#1625-followup): callers with QUEUE_UPDATE_OWN can
  1214. start their own items + claim ownership of NULL-owner items (VP-uploaded
  1215. items arrive unattributed per #1670). Callers with QUEUE_UPDATE_ALL can
  1216. start any item. Pre-fix this required QUEUE_UPDATE_OWN with no ownership
  1217. check, so _OWN holders could start anyone's queue items via direct API.
  1218. Clears the manual_start flag so the scheduler picks it up. When
  1219. ``skip_filament_check`` is false (the default) the live filament
  1220. deficit (#1496) is checked first — if the assigned spool can't satisfy
  1221. a slot's required grams, the route returns ``409`` with the deficit
  1222. payload so the caller can show a confirm dialog and retry with
  1223. ``skip_filament_check=true``.
  1224. """
  1225. user, can_modify_all = auth_result
  1226. result = await db.execute(
  1227. select(PrintQueueItem)
  1228. .options(
  1229. selectinload(PrintQueueItem.archive),
  1230. selectinload(PrintQueueItem.printer),
  1231. selectinload(PrintQueueItem.library_file),
  1232. selectinload(PrintQueueItem.batch),
  1233. )
  1234. .where(PrintQueueItem.id == item_id)
  1235. )
  1236. item = result.scalar_one_or_none()
  1237. if not item:
  1238. raise HTTPException(404, "Queue item not found")
  1239. # Ownership check — softer than /cancel because /start is the entry point
  1240. # for #1670's VP-import flow: an unowned item is claimable by the first
  1241. # _OWN holder who clicks ▶, and the route below credits them as owner.
  1242. # An item with a DIFFERENT owner → 403.
  1243. if not can_modify_all and user is not None:
  1244. if item.created_by_id is not None and item.created_by_id != user.id:
  1245. raise HTTPException(403, "You can only start your own queue items")
  1246. if item.status != "pending":
  1247. raise HTTPException(400, f"Can only start pending items, current status: '{item.status}'")
  1248. # Live deficit check — re-evaluated against current spool state, so a
  1249. # spool swap between scheduler flagging and the user clicking ▶ clears
  1250. # the block automatically.
  1251. if not skip_filament_check:
  1252. deficit = await compute_deficit_for_queue_item(db, item)
  1253. if deficit:
  1254. raise HTTPException(
  1255. status_code=409,
  1256. detail={
  1257. "code": "insufficient_filament",
  1258. "deficit": [d.to_dict() for d in deficit],
  1259. },
  1260. )
  1261. # Print Anyway / no deficit: clear the flags and let the scheduler dispatch.
  1262. item.manual_start = False
  1263. item.filament_short = False
  1264. # Persist the user's "Print Anyway" decision so the scheduler does not
  1265. # immediately re-flag this item on the next tick (#1698-followup). The
  1266. # pre-fix behaviour bounced between "user said anyway" and
  1267. # "scheduler re-blocked on same deficit" forever.
  1268. if skip_filament_check:
  1269. item.skip_filament_check = True
  1270. # Credit the clicker as the item's owner when no prior owner is set —
  1271. # VP-uploaded queue items arrive over FTP unattributed, so without this
  1272. # the print log's User column stays blank even when auth is on
  1273. # (#1670). An item that already has a creator (UI-added queue items)
  1274. # keeps that attribution; the dispatcher is not promoted over the
  1275. # original uploader.
  1276. if user is not None and item.created_by_id is None:
  1277. item.created_by_id = user.id
  1278. await db.commit()
  1279. await db.refresh(item, ["archive", "printer", "library_file", "created_by", "batch"])
  1280. logger.info(
  1281. "Manually started queue item %s (cleared manual_start; skip_filament_check=%s)",
  1282. item_id,
  1283. skip_filament_check,
  1284. )
  1285. return _enrich_response(item)