print_scheduler.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. """Print scheduler service - processes the print queue."""
  2. import asyncio
  3. import logging
  4. from datetime import datetime
  5. from sqlalchemy import func, select
  6. from sqlalchemy.ext.asyncio import AsyncSession
  7. from backend.app.core.config import settings
  8. from backend.app.core.database import async_session
  9. from backend.app.models.archive import PrintArchive
  10. from backend.app.models.library import LibraryFile
  11. from backend.app.models.print_queue import PrintQueueItem
  12. from backend.app.models.printer import Printer
  13. from backend.app.models.smart_plug import SmartPlug
  14. from backend.app.services.bambu_ftp import delete_file_async, get_ftp_retry_settings, upload_file_async, with_ftp_retry
  15. from backend.app.services.notification_service import notification_service
  16. from backend.app.services.printer_manager import printer_manager
  17. from backend.app.services.smart_plug_manager import smart_plug_manager
  18. from backend.app.utils.printer_models import normalize_printer_model
  19. logger = logging.getLogger(__name__)
  20. class PrintScheduler:
  21. """Background scheduler that processes the print queue."""
  22. def __init__(self):
  23. self._running = False
  24. self._check_interval = 30 # seconds
  25. self._power_on_wait_time = 180 # seconds to wait for printer after power on (3 min)
  26. self._power_on_check_interval = 10 # seconds between connection checks
  27. async def run(self):
  28. """Main loop - check queue every interval."""
  29. self._running = True
  30. logger.info("Print scheduler started")
  31. while self._running:
  32. try:
  33. await self.check_queue()
  34. except Exception as e:
  35. logger.error(f"Scheduler error: {e}")
  36. await asyncio.sleep(self._check_interval)
  37. def stop(self):
  38. """Stop the scheduler."""
  39. self._running = False
  40. logger.info("Print scheduler stopped")
  41. async def check_queue(self):
  42. """Check for prints ready to start."""
  43. async with async_session() as db:
  44. # Get all pending items, ordered by printer and position
  45. result = await db.execute(
  46. select(PrintQueueItem)
  47. .where(PrintQueueItem.status == "pending")
  48. .order_by(PrintQueueItem.printer_id, PrintQueueItem.position)
  49. )
  50. items = list(result.scalars().all())
  51. if not items:
  52. return
  53. # Track busy printers to avoid assigning multiple items to same printer
  54. busy_printers: set[int] = set()
  55. for item in items:
  56. # Check scheduled time first (scheduled_time is stored in UTC from ISO string)
  57. if item.scheduled_time and item.scheduled_time > datetime.utcnow():
  58. continue
  59. # Skip items that require manual start
  60. if item.manual_start:
  61. continue
  62. if item.printer_id:
  63. # Specific printer assignment (existing behavior)
  64. if item.printer_id in busy_printers:
  65. continue
  66. # Check if printer is idle
  67. printer_idle = self._is_printer_idle(item.printer_id)
  68. printer_connected = printer_manager.is_connected(item.printer_id)
  69. # If printer not connected, try to power on via smart plug
  70. if not printer_connected:
  71. plug = await self._get_smart_plug(db, item.printer_id)
  72. if plug and plug.auto_on and plug.enabled:
  73. logger.info(f"Printer {item.printer_id} offline, attempting to power on via smart plug")
  74. powered_on = await self._power_on_and_wait(plug, item.printer_id, db)
  75. if powered_on:
  76. printer_connected = True
  77. printer_idle = self._is_printer_idle(item.printer_id)
  78. else:
  79. logger.warning(f"Could not power on printer {item.printer_id} via smart plug")
  80. busy_printers.add(item.printer_id)
  81. continue
  82. else:
  83. # No plug or auto_on disabled
  84. busy_printers.add(item.printer_id)
  85. continue
  86. # Check if printer is idle (busy with another print)
  87. if not printer_idle:
  88. busy_printers.add(item.printer_id)
  89. continue
  90. # Check condition (previous print success)
  91. if item.require_previous_success:
  92. if not await self._check_previous_success(db, item):
  93. item.status = "skipped"
  94. item.error_message = "Previous print failed or was aborted"
  95. item.completed_at = datetime.now()
  96. await db.commit()
  97. logger.info(f"Skipped queue item {item.id} - previous print failed")
  98. # Send notification
  99. job_name = await self._get_job_name(db, item)
  100. printer = await self._get_printer(db, item.printer_id)
  101. await notification_service.on_queue_job_skipped(
  102. job_name=job_name,
  103. printer_id=item.printer_id,
  104. printer_name=printer.name if printer else "Unknown",
  105. reason="Previous print failed or was aborted",
  106. db=db,
  107. )
  108. continue
  109. # Start the print
  110. await self._start_print(db, item)
  111. busy_printers.add(item.printer_id)
  112. elif item.target_model:
  113. # Model-based assignment - find any idle printer of matching model
  114. # Parse required filament types if present
  115. required_types = None
  116. if item.required_filament_types:
  117. try:
  118. import json
  119. required_types = json.loads(item.required_filament_types)
  120. except json.JSONDecodeError:
  121. pass
  122. printer_id, waiting_reason = await self._find_idle_printer_for_model(
  123. db, item.target_model, busy_printers, required_types
  124. )
  125. # Update waiting_reason if changed and send notification when first waiting
  126. if item.waiting_reason != waiting_reason:
  127. was_waiting = item.waiting_reason is not None
  128. item.waiting_reason = waiting_reason
  129. await db.commit()
  130. # Send waiting notification only when transitioning to waiting state
  131. if waiting_reason and not was_waiting:
  132. job_name = await self._get_job_name(db, item)
  133. await notification_service.on_queue_job_waiting(
  134. job_name=job_name,
  135. target_model=item.target_model,
  136. waiting_reason=waiting_reason,
  137. db=db,
  138. )
  139. if printer_id:
  140. # Check condition (previous print success) before assigning
  141. if item.require_previous_success:
  142. if not await self._check_previous_success(db, item):
  143. item.status = "skipped"
  144. item.error_message = "Previous print failed or was aborted"
  145. item.completed_at = datetime.now()
  146. await db.commit()
  147. logger.info(f"Skipped queue item {item.id} - previous print failed")
  148. # Send notification
  149. job_name = await self._get_job_name(db, item)
  150. printer = await self._get_printer(db, printer_id)
  151. await notification_service.on_queue_job_skipped(
  152. job_name=job_name,
  153. printer_id=printer_id,
  154. printer_name=printer.name if printer else "Unknown",
  155. reason="Previous print failed or was aborted",
  156. db=db,
  157. )
  158. continue
  159. # Assign printer and start - clear waiting reason
  160. item.printer_id = printer_id
  161. item.waiting_reason = None
  162. logger.info(f"Model-based assignment: queue item {item.id} assigned to printer {printer_id}")
  163. # Send assignment notification
  164. job_name = await self._get_job_name(db, item)
  165. printer = await self._get_printer(db, printer_id)
  166. await notification_service.on_queue_job_assigned(
  167. job_name=job_name,
  168. printer_id=printer_id,
  169. printer_name=printer.name if printer else "Unknown",
  170. target_model=item.target_model,
  171. db=db,
  172. )
  173. await self._start_print(db, item)
  174. busy_printers.add(printer_id)
  175. async def _find_idle_printer_for_model(
  176. self,
  177. db: AsyncSession,
  178. model: str,
  179. exclude_ids: set[int],
  180. required_filament_types: list[str] | None = None,
  181. ) -> tuple[int | None, str | None]:
  182. """Find an idle, connected printer matching the model with compatible filaments.
  183. Args:
  184. db: Database session
  185. model: Printer model to match (e.g., "X1C", "P1S")
  186. exclude_ids: Printer IDs to exclude (already busy)
  187. required_filament_types: Optional list of filament types needed (e.g., ["PLA", "PETG"])
  188. If provided, only printers with all required types loaded will match.
  189. Returns:
  190. Tuple of (printer_id, waiting_reason):
  191. - (printer_id, None) if a matching printer was found
  192. - (None, reason) if no printer is available, with explanation
  193. """
  194. # Normalize model name and use case-insensitive matching
  195. normalized_model = normalize_printer_model(model) or model
  196. result = await db.execute(
  197. select(Printer)
  198. .where(func.lower(Printer.model) == normalized_model.lower())
  199. .where(Printer.is_active == True) # noqa: E712
  200. )
  201. printers = list(result.scalars().all())
  202. if not printers:
  203. return None, f"No active {normalized_model} printers configured"
  204. # Track reasons for skipping printers
  205. printers_busy = []
  206. printers_offline = []
  207. printers_missing_filament = []
  208. for printer in printers:
  209. if printer.id in exclude_ids:
  210. printers_busy.append(printer.name)
  211. continue
  212. is_connected = printer_manager.is_connected(printer.id)
  213. is_idle = self._is_printer_idle(printer.id) if is_connected else False
  214. if not is_connected:
  215. printers_offline.append(printer.name)
  216. continue
  217. if not is_idle:
  218. printers_busy.append(printer.name)
  219. continue
  220. # Validate filament compatibility if required types are specified
  221. if required_filament_types:
  222. missing = self._get_missing_filament_types(printer.id, required_filament_types)
  223. if missing:
  224. printers_missing_filament.append((printer.name, missing))
  225. logger.debug(f"Skipping printer {printer.id} ({printer.name}) - missing filaments: {missing}")
  226. continue
  227. # Found a matching printer - clear waiting reason
  228. return printer.id, None
  229. # Build waiting reason from what we found
  230. reasons = []
  231. if printers_missing_filament:
  232. # Filament mismatch is most actionable - show first
  233. names_and_missing = [f"{name} (needs {', '.join(missing)})" for name, missing in printers_missing_filament]
  234. reasons.append(f"Waiting for filament: {'; '.join(names_and_missing)}")
  235. if printers_busy:
  236. reasons.append(f"Busy: {', '.join(printers_busy)}")
  237. if printers_offline:
  238. reasons.append(f"Offline: {', '.join(printers_offline)}")
  239. return None, " | ".join(reasons) if reasons else f"No available {model} printers"
  240. def _get_missing_filament_types(self, printer_id: int, required_types: list[str]) -> list[str]:
  241. """Get the list of required filament types that are not loaded on the printer.
  242. Args:
  243. printer_id: The printer ID
  244. required_types: List of filament types needed (e.g., ["PLA", "PETG"])
  245. Returns:
  246. List of missing filament types (empty if all are loaded)
  247. """
  248. status = printer_manager.get_status(printer_id)
  249. if not status:
  250. return required_types # Can't determine, assume all missing
  251. # Collect all filament types loaded on this printer (AMS units + external spool)
  252. loaded_types: set[str] = set()
  253. # Check AMS units (stored in raw_data["ams"])
  254. ams_data = status.raw_data.get("ams", [])
  255. if ams_data:
  256. for ams_unit in ams_data:
  257. for tray in ams_unit.get("tray", []):
  258. tray_type = tray.get("tray_type")
  259. if tray_type:
  260. loaded_types.add(tray_type.upper())
  261. # Check external spool (virtual tray, stored in raw_data["vt_tray"])
  262. vt_tray = status.raw_data.get("vt_tray")
  263. if vt_tray:
  264. vt_type = vt_tray.get("tray_type")
  265. if vt_type:
  266. loaded_types.add(vt_type.upper())
  267. # Find which required types are missing (case-insensitive comparison)
  268. missing = []
  269. for req_type in required_types:
  270. if req_type.upper() not in loaded_types:
  271. missing.append(req_type)
  272. return missing
  273. def _is_printer_idle(self, printer_id: int) -> bool:
  274. """Check if a printer is connected and idle."""
  275. if not printer_manager.is_connected(printer_id):
  276. return False
  277. state = printer_manager.get_status(printer_id)
  278. if not state:
  279. return False
  280. # Printer is idle if state is IDLE, FINISH, FAILED, or unknown
  281. # FAILED means previous print failed, printer is ready for new print
  282. return state.state in ("IDLE", "FINISH", "FAILED", "unknown")
  283. async def _get_smart_plug(self, db: AsyncSession, printer_id: int) -> SmartPlug | None:
  284. """Get the smart plug associated with a printer."""
  285. result = await db.execute(select(SmartPlug).where(SmartPlug.printer_id == printer_id))
  286. return result.scalar_one_or_none()
  287. async def _power_on_and_wait(self, plug: SmartPlug, printer_id: int, db: AsyncSession) -> bool:
  288. """Turn on smart plug and wait for printer to connect.
  289. Returns True if printer connected successfully within timeout.
  290. """
  291. # Get the appropriate service for the plug type (Tasmota or Home Assistant)
  292. service = await smart_plug_manager.get_service_for_plug(plug, db)
  293. # Check current plug state
  294. status = await service.get_status(plug)
  295. if not status.get("reachable"):
  296. logger.warning(f"Smart plug '{plug.name}' is not reachable")
  297. return False
  298. # Turn on if not already on
  299. if status.get("state") != "ON":
  300. success = await service.turn_on(plug)
  301. if not success:
  302. logger.warning(f"Failed to turn on smart plug '{plug.name}'")
  303. return False
  304. logger.info(f"Powered on smart plug '{plug.name}' for printer {printer_id}")
  305. # Get printer from database for connection
  306. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  307. printer = result.scalar_one_or_none()
  308. if not printer:
  309. logger.error(f"Printer {printer_id} not found in database")
  310. return False
  311. # Wait for printer to boot (give it some time before trying to connect)
  312. logger.info(f"Waiting 30s for printer {printer_id} to boot...")
  313. await asyncio.sleep(30)
  314. # Try to connect to the printer periodically
  315. elapsed = 30 # Already waited 30s
  316. while elapsed < self._power_on_wait_time:
  317. # Try to connect
  318. logger.info(f"Attempting to connect to printer {printer_id}...")
  319. try:
  320. connected = await printer_manager.connect_printer(printer)
  321. if connected:
  322. logger.info(f"Printer {printer_id} connected after {elapsed}s")
  323. # Give it a moment to stabilize and get status
  324. await asyncio.sleep(5)
  325. return True
  326. except Exception as e:
  327. logger.debug(f"Connection attempt failed: {e}")
  328. await asyncio.sleep(self._power_on_check_interval)
  329. elapsed += self._power_on_check_interval
  330. logger.debug(f"Waiting for printer {printer_id} to connect... ({elapsed}s)")
  331. logger.warning(f"Printer {printer_id} did not connect within {self._power_on_wait_time}s after power on")
  332. return False
  333. async def _check_previous_success(self, db: AsyncSession, item: PrintQueueItem) -> bool:
  334. """Check if the previous print on this printer succeeded."""
  335. # Find the most recent completed queue item for this printer
  336. result = await db.execute(
  337. select(PrintQueueItem)
  338. .where(PrintQueueItem.printer_id == item.printer_id)
  339. .where(PrintQueueItem.id != item.id)
  340. .where(PrintQueueItem.status.in_(["completed", "failed", "skipped", "aborted"]))
  341. .order_by(PrintQueueItem.completed_at.desc())
  342. .limit(1)
  343. )
  344. prev_item = result.scalar_one_or_none()
  345. # If no previous item, assume success (first in queue)
  346. if not prev_item:
  347. return True
  348. return prev_item.status == "completed"
  349. async def _power_off_if_needed(self, db: AsyncSession, item: PrintQueueItem):
  350. """Power off printer if auto_off_after is enabled (waits for cooldown)."""
  351. if not item.auto_off_after:
  352. return
  353. plug = await self._get_smart_plug(db, item.printer_id)
  354. if plug and plug.enabled:
  355. logger.info(f"Auto-off: Waiting for printer {item.printer_id} to cool down before power off...")
  356. # Wait for cooldown (up to 10 minutes)
  357. await printer_manager.wait_for_cooldown(item.printer_id, target_temp=50.0, timeout=600)
  358. logger.info(f"Auto-off: Powering off printer {item.printer_id}")
  359. service = await smart_plug_manager.get_service_for_plug(plug, db)
  360. await service.turn_off(plug)
  361. async def _get_job_name(self, db: AsyncSession, item: PrintQueueItem) -> str:
  362. """Get a human-readable name for a queue item."""
  363. if item.archive_id:
  364. result = await db.execute(select(PrintArchive).where(PrintArchive.id == item.archive_id))
  365. archive = result.scalar_one_or_none()
  366. if archive:
  367. return archive.filename.replace(".gcode.3mf", "").replace(".3mf", "")
  368. if item.library_file_id:
  369. result = await db.execute(select(LibraryFile).where(LibraryFile.id == item.library_file_id))
  370. library_file = result.scalar_one_or_none()
  371. if library_file:
  372. return library_file.filename.replace(".gcode.3mf", "").replace(".3mf", "")
  373. return f"Job #{item.id}"
  374. async def _get_printer(self, db: AsyncSession, printer_id: int) -> Printer | None:
  375. """Get printer by ID."""
  376. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  377. return result.scalar_one_or_none()
  378. async def _start_print(self, db: AsyncSession, item: PrintQueueItem):
  379. """Upload file and start print for a queue item.
  380. Supports two sources:
  381. - archive_id: Print from an existing archive
  382. - library_file_id: Print from a library file (file manager)
  383. """
  384. logger.info(f"Starting queue item {item.id}")
  385. # Get printer first (needed for both paths)
  386. result = await db.execute(select(Printer).where(Printer.id == item.printer_id))
  387. printer = result.scalar_one_or_none()
  388. if not printer:
  389. item.status = "failed"
  390. item.error_message = "Printer not found"
  391. item.completed_at = datetime.utcnow()
  392. await db.commit()
  393. logger.error(f"Queue item {item.id}: Printer {item.printer_id} not found")
  394. await self._power_off_if_needed(db, item)
  395. return
  396. # Check printer is connected
  397. if not printer_manager.is_connected(item.printer_id):
  398. item.status = "failed"
  399. item.error_message = "Printer not connected"
  400. item.completed_at = datetime.utcnow()
  401. await db.commit()
  402. logger.error(f"Queue item {item.id}: Printer {item.printer_id} not connected")
  403. await self._power_off_if_needed(db, item)
  404. return
  405. # Determine source: archive or library file
  406. archive = None
  407. library_file = None
  408. file_path = None
  409. filename = None
  410. if item.archive_id:
  411. # Print from archive
  412. result = await db.execute(select(PrintArchive).where(PrintArchive.id == item.archive_id))
  413. archive = result.scalar_one_or_none()
  414. if not archive:
  415. item.status = "failed"
  416. item.error_message = "Archive not found"
  417. item.completed_at = datetime.utcnow()
  418. await db.commit()
  419. logger.error(f"Queue item {item.id}: Archive {item.archive_id} not found")
  420. await self._power_off_if_needed(db, item)
  421. return
  422. file_path = settings.base_dir / archive.file_path
  423. filename = archive.filename
  424. elif item.library_file_id:
  425. # Print from library file (file manager)
  426. result = await db.execute(select(LibraryFile).where(LibraryFile.id == item.library_file_id))
  427. library_file = result.scalar_one_or_none()
  428. if not library_file:
  429. item.status = "failed"
  430. item.error_message = "Library file not found"
  431. item.completed_at = datetime.utcnow()
  432. await db.commit()
  433. logger.error(f"Queue item {item.id}: Library file {item.library_file_id} not found")
  434. await self._power_off_if_needed(db, item)
  435. return
  436. # Library files store absolute paths
  437. from pathlib import Path
  438. lib_path = Path(library_file.file_path)
  439. file_path = lib_path if lib_path.is_absolute() else settings.base_dir / library_file.file_path
  440. filename = library_file.filename
  441. else:
  442. # Neither archive nor library file specified
  443. item.status = "failed"
  444. item.error_message = "No source file specified"
  445. item.completed_at = datetime.utcnow()
  446. await db.commit()
  447. logger.error(f"Queue item {item.id}: No archive_id or library_file_id specified")
  448. await self._power_off_if_needed(db, item)
  449. return
  450. # Check file exists on disk
  451. if not file_path.exists():
  452. item.status = "failed"
  453. item.error_message = "Source file not found on disk"
  454. item.completed_at = datetime.utcnow()
  455. await db.commit()
  456. logger.error(f"Queue item {item.id}: File not found: {file_path}")
  457. await self._power_off_if_needed(db, item)
  458. return
  459. # Upload file to printer via FTP
  460. # Use a clean filename to avoid issues with double extensions like .gcode.3mf
  461. base_name = filename
  462. if base_name.endswith(".gcode.3mf"):
  463. base_name = base_name[:-10] # Remove .gcode.3mf
  464. elif base_name.endswith(".3mf"):
  465. base_name = base_name[:-4] # Remove .3mf
  466. remote_filename = f"{base_name}.3mf"
  467. # Upload to root directory (not /cache/) - the start_print command references
  468. # files by name only (ftp://{filename}), so they must be in the root
  469. remote_path = f"/{remote_filename}"
  470. # Get FTP retry settings
  471. ftp_retry_enabled, ftp_retry_count, ftp_retry_delay, ftp_timeout = await get_ftp_retry_settings()
  472. # Delete existing file if present (avoids 553 error on overwrite)
  473. try:
  474. await delete_file_async(
  475. printer.ip_address,
  476. printer.access_code,
  477. remote_path,
  478. socket_timeout=ftp_timeout,
  479. printer_model=printer.model,
  480. )
  481. except Exception:
  482. pass # File may not exist, that's fine
  483. try:
  484. if ftp_retry_enabled:
  485. uploaded = await with_ftp_retry(
  486. upload_file_async,
  487. printer.ip_address,
  488. printer.access_code,
  489. file_path,
  490. remote_path,
  491. socket_timeout=ftp_timeout,
  492. printer_model=printer.model,
  493. max_retries=ftp_retry_count,
  494. retry_delay=ftp_retry_delay,
  495. operation_name=f"Upload print to {printer.name}",
  496. )
  497. else:
  498. uploaded = await upload_file_async(
  499. printer.ip_address,
  500. printer.access_code,
  501. file_path,
  502. remote_path,
  503. socket_timeout=ftp_timeout,
  504. printer_model=printer.model,
  505. )
  506. except Exception as e:
  507. uploaded = False
  508. logger.error(f"Queue item {item.id}: FTP error: {e}")
  509. if not uploaded:
  510. item.status = "failed"
  511. item.error_message = "Failed to upload file to printer"
  512. item.completed_at = datetime.utcnow()
  513. await db.commit()
  514. logger.error(f"Queue item {item.id}: FTP upload failed")
  515. # Send failure notification
  516. await notification_service.on_queue_job_failed(
  517. job_name=filename.replace(".gcode.3mf", "").replace(".3mf", ""),
  518. printer_id=printer.id,
  519. printer_name=printer.name,
  520. reason="Failed to upload file to printer",
  521. db=db,
  522. )
  523. await self._power_off_if_needed(db, item)
  524. return
  525. # Register as expected print so we don't create a duplicate archive
  526. # Only applicable for archive-based prints
  527. if archive:
  528. from backend.app.main import register_expected_print
  529. register_expected_print(item.printer_id, remote_filename, archive.id)
  530. # Parse AMS mapping if stored
  531. ams_mapping = None
  532. if item.ams_mapping:
  533. try:
  534. import json
  535. ams_mapping = json.loads(item.ams_mapping)
  536. except json.JSONDecodeError:
  537. logger.warning(f"Queue item {item.id}: Invalid AMS mapping JSON, ignoring")
  538. # Start the print with AMS mapping, plate_id and print options
  539. started = printer_manager.start_print(
  540. item.printer_id,
  541. remote_filename,
  542. plate_id=item.plate_id or 1,
  543. ams_mapping=ams_mapping,
  544. bed_levelling=item.bed_levelling,
  545. flow_cali=item.flow_cali,
  546. vibration_cali=item.vibration_cali,
  547. layer_inspect=item.layer_inspect,
  548. timelapse=item.timelapse,
  549. use_ams=item.use_ams,
  550. )
  551. if started:
  552. item.status = "printing"
  553. item.started_at = datetime.utcnow()
  554. await db.commit()
  555. logger.info(f"Queue item {item.id}: Print started - {filename}")
  556. # Get estimated time for notification
  557. estimated_time = None
  558. if archive and archive.print_time_seconds:
  559. estimated_time = archive.print_time_seconds
  560. elif library_file and library_file.print_time_seconds:
  561. estimated_time = library_file.print_time_seconds
  562. # Send job started notification
  563. await notification_service.on_queue_job_started(
  564. job_name=filename.replace(".gcode.3mf", "").replace(".3mf", ""),
  565. printer_id=printer.id,
  566. printer_name=printer.name,
  567. db=db,
  568. estimated_time=estimated_time,
  569. )
  570. # MQTT relay - publish queue job started
  571. try:
  572. from backend.app.services.mqtt_relay import mqtt_relay
  573. await mqtt_relay.on_queue_job_started(
  574. job_id=item.id,
  575. filename=filename,
  576. printer_id=printer.id,
  577. printer_name=printer.name,
  578. printer_serial=printer.serial_number,
  579. )
  580. except Exception:
  581. pass # Don't fail if MQTT fails
  582. else:
  583. item.status = "failed"
  584. item.error_message = "Failed to send print command"
  585. item.completed_at = datetime.utcnow()
  586. await db.commit()
  587. logger.error(f"Queue item {item.id}: Failed to start print")
  588. # Send failure notification
  589. await notification_service.on_queue_job_failed(
  590. job_name=filename.replace(".gcode.3mf", "").replace(".3mf", ""),
  591. printer_id=printer.id,
  592. printer_name=printer.name,
  593. reason="Failed to send print command",
  594. db=db,
  595. )
  596. await self._power_off_if_needed(db, item)
  597. # Global scheduler instance
  598. scheduler = PrintScheduler()