|
|
@@ -2088,6 +2088,50 @@ async def on_print_complete(printer_id: int, data: dict):
|
|
|
if archive:
|
|
|
archive_id = archive.id
|
|
|
|
|
|
+ # Cleanup: delete uploaded file from printer SD card to prevent phantom prints (Issue #374)
|
|
|
+ # The print scheduler uploads files to the SD card root (/). Some printers (e.g. P1S)
|
|
|
+ # auto-start files found in root on power cycle, causing ghost prints.
|
|
|
+ # Must run before the archive_id early-return so it executes even when archiving is disabled.
|
|
|
+ try:
|
|
|
+ printer_info = printer_manager.get_printer(printer_id)
|
|
|
+ if printer_info and subtask_name:
|
|
|
+ from backend.app.services.bambu_ftp import delete_file_async
|
|
|
+
|
|
|
+ # Try both .3mf and .gcode extensions — the printer may have either
|
|
|
+ for ext in (".3mf", ".gcode"):
|
|
|
+ remote_path = f"/{subtask_name}{ext}"
|
|
|
+ # Retry up to 3 times — the printer may still lock the filesystem briefly after a print ends
|
|
|
+ for attempt in range(1, 4):
|
|
|
+ try:
|
|
|
+ delete_result = await delete_file_async(
|
|
|
+ printer_info.ip_address,
|
|
|
+ printer_info.access_code,
|
|
|
+ remote_path,
|
|
|
+ printer_model=printer_info.model,
|
|
|
+ )
|
|
|
+ if delete_result:
|
|
|
+ logger.info("Deleted %s from printer %s SD card", remote_path, printer_info.name)
|
|
|
+ break # Success or file doesn't exist — no need to retry
|
|
|
+ except Exception as e:
|
|
|
+ if attempt < 3:
|
|
|
+ logger.debug(
|
|
|
+ "SD card cleanup attempt %d/3 failed for %s: %s, retrying in 2s",
|
|
|
+ attempt,
|
|
|
+ remote_path,
|
|
|
+ e,
|
|
|
+ )
|
|
|
+ await asyncio.sleep(2)
|
|
|
+ else:
|
|
|
+ logger.debug(
|
|
|
+ "SD card cleanup failed after 3 attempts for %s: %s (non-critical)",
|
|
|
+ remote_path,
|
|
|
+ e,
|
|
|
+ )
|
|
|
+ except Exception as e:
|
|
|
+ logger.debug("SD card file cleanup failed for printer %s: %s (non-critical)", printer_id, e)
|
|
|
+
|
|
|
+ log_timing("SD card cleanup")
|
|
|
+
|
|
|
if not archive_id:
|
|
|
logger.warning("Could not find archive for print complete: filename=%s, subtask=%s", filename, subtask_name)
|
|
|
return
|
|
|
@@ -2778,31 +2822,6 @@ async def on_print_complete(printer_id: int, data: dict):
|
|
|
logging.getLogger(__name__).warning(f"Queue item update failed: {e}")
|
|
|
|
|
|
log_timing("Queue item update")
|
|
|
-
|
|
|
- # Cleanup: delete uploaded file from printer SD card to prevent phantom prints (Issue #374)
|
|
|
- # The print scheduler uploads .3mf files to the SD card root (/). Some printers (e.g. P1S)
|
|
|
- # auto-start files found in root on power cycle, causing ghost prints.
|
|
|
- try:
|
|
|
- printer_info = printer_manager.get_printer(printer_id)
|
|
|
- if printer_info and subtask_name:
|
|
|
- from backend.app.services.bambu_ftp import delete_file_async
|
|
|
-
|
|
|
- remote_path = f"/{subtask_name}.3mf"
|
|
|
- logger.info("Cleaning up uploaded file from printer SD card: %s", remote_path)
|
|
|
- delete_result = await delete_file_async(
|
|
|
- printer_info.ip_address,
|
|
|
- printer_info.access_code,
|
|
|
- remote_path,
|
|
|
- printer_model=printer_info.model,
|
|
|
- )
|
|
|
- if delete_result:
|
|
|
- logger.info("Deleted %s from printer %s SD card", remote_path, printer_info.name)
|
|
|
- else:
|
|
|
- logger.debug("File delete returned False for %s (may not exist)", remote_path)
|
|
|
- except Exception as e:
|
|
|
- logger.debug("SD card file cleanup failed for printer %s: %s (non-critical)", printer_id, e)
|
|
|
-
|
|
|
- log_timing("SD card cleanup")
|
|
|
logger.info("[CALLBACK] on_print_complete finished for printer %s, archive %s", printer_id, archive_id)
|
|
|
|
|
|
|