print_scheduler.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699
  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.tasmota import tasmota_service
  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. # Check current plug state
  292. status = await tasmota_service.get_status(plug)
  293. if not status.get("reachable"):
  294. logger.warning(f"Smart plug '{plug.name}' is not reachable")
  295. return False
  296. # Turn on if not already on
  297. if status.get("state") != "ON":
  298. success = await tasmota_service.turn_on(plug)
  299. if not success:
  300. logger.warning(f"Failed to turn on smart plug '{plug.name}'")
  301. return False
  302. logger.info(f"Powered on smart plug '{plug.name}' for printer {printer_id}")
  303. # Get printer from database for connection
  304. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  305. printer = result.scalar_one_or_none()
  306. if not printer:
  307. logger.error(f"Printer {printer_id} not found in database")
  308. return False
  309. # Wait for printer to boot (give it some time before trying to connect)
  310. logger.info(f"Waiting 30s for printer {printer_id} to boot...")
  311. await asyncio.sleep(30)
  312. # Try to connect to the printer periodically
  313. elapsed = 30 # Already waited 30s
  314. while elapsed < self._power_on_wait_time:
  315. # Try to connect
  316. logger.info(f"Attempting to connect to printer {printer_id}...")
  317. try:
  318. connected = await printer_manager.connect_printer(printer)
  319. if connected:
  320. logger.info(f"Printer {printer_id} connected after {elapsed}s")
  321. # Give it a moment to stabilize and get status
  322. await asyncio.sleep(5)
  323. return True
  324. except Exception as e:
  325. logger.debug(f"Connection attempt failed: {e}")
  326. await asyncio.sleep(self._power_on_check_interval)
  327. elapsed += self._power_on_check_interval
  328. logger.debug(f"Waiting for printer {printer_id} to connect... ({elapsed}s)")
  329. logger.warning(f"Printer {printer_id} did not connect within {self._power_on_wait_time}s after power on")
  330. return False
  331. async def _check_previous_success(self, db: AsyncSession, item: PrintQueueItem) -> bool:
  332. """Check if the previous print on this printer succeeded."""
  333. # Find the most recent completed queue item for this printer
  334. result = await db.execute(
  335. select(PrintQueueItem)
  336. .where(PrintQueueItem.printer_id == item.printer_id)
  337. .where(PrintQueueItem.id != item.id)
  338. .where(PrintQueueItem.status.in_(["completed", "failed", "skipped", "aborted"]))
  339. .order_by(PrintQueueItem.completed_at.desc())
  340. .limit(1)
  341. )
  342. prev_item = result.scalar_one_or_none()
  343. # If no previous item, assume success (first in queue)
  344. if not prev_item:
  345. return True
  346. return prev_item.status == "completed"
  347. async def _power_off_if_needed(self, db: AsyncSession, item: PrintQueueItem):
  348. """Power off printer if auto_off_after is enabled (waits for cooldown)."""
  349. if not item.auto_off_after:
  350. return
  351. plug = await self._get_smart_plug(db, item.printer_id)
  352. if plug and plug.enabled:
  353. logger.info(f"Auto-off: Waiting for printer {item.printer_id} to cool down before power off...")
  354. # Wait for cooldown (up to 10 minutes)
  355. await printer_manager.wait_for_cooldown(item.printer_id, target_temp=50.0, timeout=600)
  356. logger.info(f"Auto-off: Powering off printer {item.printer_id}")
  357. await tasmota_service.turn_off(plug)
  358. async def _get_job_name(self, db: AsyncSession, item: PrintQueueItem) -> str:
  359. """Get a human-readable name for a queue item."""
  360. if item.archive_id:
  361. result = await db.execute(select(PrintArchive).where(PrintArchive.id == item.archive_id))
  362. archive = result.scalar_one_or_none()
  363. if archive:
  364. return archive.filename.replace(".gcode.3mf", "").replace(".3mf", "")
  365. if item.library_file_id:
  366. result = await db.execute(select(LibraryFile).where(LibraryFile.id == item.library_file_id))
  367. library_file = result.scalar_one_or_none()
  368. if library_file:
  369. return library_file.filename.replace(".gcode.3mf", "").replace(".3mf", "")
  370. return f"Job #{item.id}"
  371. async def _get_printer(self, db: AsyncSession, printer_id: int) -> Printer | None:
  372. """Get printer by ID."""
  373. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  374. return result.scalar_one_or_none()
  375. async def _start_print(self, db: AsyncSession, item: PrintQueueItem):
  376. """Upload file and start print for a queue item.
  377. Supports two sources:
  378. - archive_id: Print from an existing archive
  379. - library_file_id: Print from a library file (file manager)
  380. """
  381. logger.info(f"Starting queue item {item.id}")
  382. # Get printer first (needed for both paths)
  383. result = await db.execute(select(Printer).where(Printer.id == item.printer_id))
  384. printer = result.scalar_one_or_none()
  385. if not printer:
  386. item.status = "failed"
  387. item.error_message = "Printer not found"
  388. item.completed_at = datetime.utcnow()
  389. await db.commit()
  390. logger.error(f"Queue item {item.id}: Printer {item.printer_id} not found")
  391. await self._power_off_if_needed(db, item)
  392. return
  393. # Check printer is connected
  394. if not printer_manager.is_connected(item.printer_id):
  395. item.status = "failed"
  396. item.error_message = "Printer not connected"
  397. item.completed_at = datetime.utcnow()
  398. await db.commit()
  399. logger.error(f"Queue item {item.id}: Printer {item.printer_id} not connected")
  400. await self._power_off_if_needed(db, item)
  401. return
  402. # Determine source: archive or library file
  403. archive = None
  404. library_file = None
  405. file_path = None
  406. filename = None
  407. if item.archive_id:
  408. # Print from archive
  409. result = await db.execute(select(PrintArchive).where(PrintArchive.id == item.archive_id))
  410. archive = result.scalar_one_or_none()
  411. if not archive:
  412. item.status = "failed"
  413. item.error_message = "Archive not found"
  414. item.completed_at = datetime.utcnow()
  415. await db.commit()
  416. logger.error(f"Queue item {item.id}: Archive {item.archive_id} not found")
  417. await self._power_off_if_needed(db, item)
  418. return
  419. file_path = settings.base_dir / archive.file_path
  420. filename = archive.filename
  421. elif item.library_file_id:
  422. # Print from library file (file manager)
  423. result = await db.execute(select(LibraryFile).where(LibraryFile.id == item.library_file_id))
  424. library_file = result.scalar_one_or_none()
  425. if not library_file:
  426. item.status = "failed"
  427. item.error_message = "Library file not found"
  428. item.completed_at = datetime.utcnow()
  429. await db.commit()
  430. logger.error(f"Queue item {item.id}: Library file {item.library_file_id} not found")
  431. await self._power_off_if_needed(db, item)
  432. return
  433. # Library files store absolute paths
  434. from pathlib import Path
  435. lib_path = Path(library_file.file_path)
  436. file_path = lib_path if lib_path.is_absolute() else settings.base_dir / library_file.file_path
  437. filename = library_file.filename
  438. else:
  439. # Neither archive nor library file specified
  440. item.status = "failed"
  441. item.error_message = "No source file specified"
  442. item.completed_at = datetime.utcnow()
  443. await db.commit()
  444. logger.error(f"Queue item {item.id}: No archive_id or library_file_id specified")
  445. await self._power_off_if_needed(db, item)
  446. return
  447. # Check file exists on disk
  448. if not file_path.exists():
  449. item.status = "failed"
  450. item.error_message = "Source file not found on disk"
  451. item.completed_at = datetime.utcnow()
  452. await db.commit()
  453. logger.error(f"Queue item {item.id}: File not found: {file_path}")
  454. await self._power_off_if_needed(db, item)
  455. return
  456. # Upload file to printer via FTP
  457. # Use a clean filename to avoid issues with double extensions like .gcode.3mf
  458. base_name = filename
  459. if base_name.endswith(".gcode.3mf"):
  460. base_name = base_name[:-10] # Remove .gcode.3mf
  461. elif base_name.endswith(".3mf"):
  462. base_name = base_name[:-4] # Remove .3mf
  463. remote_filename = f"{base_name}.3mf"
  464. # Upload to root directory (not /cache/) - the start_print command references
  465. # files by name only (ftp://{filename}), so they must be in the root
  466. remote_path = f"/{remote_filename}"
  467. # Get FTP retry settings
  468. ftp_retry_enabled, ftp_retry_count, ftp_retry_delay, ftp_timeout = await get_ftp_retry_settings()
  469. # Delete existing file if present (avoids 553 error on overwrite)
  470. try:
  471. await delete_file_async(
  472. printer.ip_address,
  473. printer.access_code,
  474. remote_path,
  475. socket_timeout=ftp_timeout,
  476. printer_model=printer.model,
  477. )
  478. except Exception:
  479. pass # File may not exist, that's fine
  480. try:
  481. if ftp_retry_enabled:
  482. uploaded = await with_ftp_retry(
  483. upload_file_async,
  484. printer.ip_address,
  485. printer.access_code,
  486. file_path,
  487. remote_path,
  488. socket_timeout=ftp_timeout,
  489. printer_model=printer.model,
  490. max_retries=ftp_retry_count,
  491. retry_delay=ftp_retry_delay,
  492. operation_name=f"Upload print to {printer.name}",
  493. )
  494. else:
  495. uploaded = await upload_file_async(
  496. printer.ip_address,
  497. printer.access_code,
  498. file_path,
  499. remote_path,
  500. socket_timeout=ftp_timeout,
  501. printer_model=printer.model,
  502. )
  503. except Exception as e:
  504. uploaded = False
  505. logger.error(f"Queue item {item.id}: FTP error: {e}")
  506. if not uploaded:
  507. item.status = "failed"
  508. item.error_message = "Failed to upload file to printer"
  509. item.completed_at = datetime.utcnow()
  510. await db.commit()
  511. logger.error(f"Queue item {item.id}: FTP upload failed")
  512. # Send failure notification
  513. await notification_service.on_queue_job_failed(
  514. job_name=filename.replace(".gcode.3mf", "").replace(".3mf", ""),
  515. printer_id=printer.id,
  516. printer_name=printer.name,
  517. reason="Failed to upload file to printer",
  518. db=db,
  519. )
  520. await self._power_off_if_needed(db, item)
  521. return
  522. # Register as expected print so we don't create a duplicate archive
  523. # Only applicable for archive-based prints
  524. if archive:
  525. from backend.app.main import register_expected_print
  526. register_expected_print(item.printer_id, remote_filename, archive.id)
  527. # Parse AMS mapping if stored
  528. ams_mapping = None
  529. if item.ams_mapping:
  530. try:
  531. import json
  532. ams_mapping = json.loads(item.ams_mapping)
  533. except json.JSONDecodeError:
  534. logger.warning(f"Queue item {item.id}: Invalid AMS mapping JSON, ignoring")
  535. # Start the print with AMS mapping, plate_id and print options
  536. started = printer_manager.start_print(
  537. item.printer_id,
  538. remote_filename,
  539. plate_id=item.plate_id or 1,
  540. ams_mapping=ams_mapping,
  541. bed_levelling=item.bed_levelling,
  542. flow_cali=item.flow_cali,
  543. vibration_cali=item.vibration_cali,
  544. layer_inspect=item.layer_inspect,
  545. timelapse=item.timelapse,
  546. use_ams=item.use_ams,
  547. )
  548. if started:
  549. item.status = "printing"
  550. item.started_at = datetime.utcnow()
  551. await db.commit()
  552. logger.info(f"Queue item {item.id}: Print started - {filename}")
  553. # Get estimated time for notification
  554. estimated_time = None
  555. if archive and archive.print_time_seconds:
  556. estimated_time = archive.print_time_seconds
  557. elif library_file and library_file.print_time_seconds:
  558. estimated_time = library_file.print_time_seconds
  559. # Send job started notification
  560. await notification_service.on_queue_job_started(
  561. job_name=filename.replace(".gcode.3mf", "").replace(".3mf", ""),
  562. printer_id=printer.id,
  563. printer_name=printer.name,
  564. db=db,
  565. estimated_time=estimated_time,
  566. )
  567. # MQTT relay - publish queue job started
  568. try:
  569. from backend.app.services.mqtt_relay import mqtt_relay
  570. await mqtt_relay.on_queue_job_started(
  571. job_id=item.id,
  572. filename=filename,
  573. printer_id=printer.id,
  574. printer_name=printer.name,
  575. printer_serial=printer.serial_number,
  576. )
  577. except Exception:
  578. pass # Don't fail if MQTT fails
  579. else:
  580. item.status = "failed"
  581. item.error_message = "Failed to send print command"
  582. item.completed_at = datetime.utcnow()
  583. await db.commit()
  584. logger.error(f"Queue item {item.id}: Failed to start print")
  585. # Send failure notification
  586. await notification_service.on_queue_job_failed(
  587. job_name=filename.replace(".gcode.3mf", "").replace(".3mf", ""),
  588. printer_id=printer.id,
  589. printer_name=printer.name,
  590. reason="Failed to send print command",
  591. db=db,
  592. )
  593. await self._power_off_if_needed(db, item)
  594. # Global scheduler instance
  595. scheduler = PrintScheduler()