This commit is contained in:
zman 2025-04-07 17:26:48 -04:00
parent c53baccb38
commit 2561febca9

24
app.py
View File

@ -10,6 +10,7 @@ from PIL import Image
import mimetypes import mimetypes
import logging import logging
from logging.handlers import RotatingFileHandler from logging.handlers import RotatingFileHandler
import time
app = Flask(__name__) app = Flask(__name__)
@ -79,10 +80,13 @@ def print_address_label(pdf_path):
cut=True cut=True
) )
app.logger.info("Waiting to acquire printer lock...") # Try to acquire the printer lock with a timeout
with printer_lock: # Ensure only one thread sends a job at a time max_retries = 3
app.logger.info("Sending to printer...") retry_delay = 2 # seconds
for attempt in range(max_retries):
if printer_lock.acquire(timeout=5): # 5 seconds timeout
try: try:
app.logger.info(f"Attempting to send print job (Attempt {attempt + 1})...")
send( send(
instructions=instructions, instructions=instructions,
printer_identifier=printer, printer_identifier=printer,
@ -90,8 +94,22 @@ def print_address_label(pdf_path):
blocking=True blocking=True
) )
app.logger.info("Print job sent successfully") app.logger.info("Print job sent successfully")
break
except Exception as e: except Exception as e:
app.logger.error(f"Error during printing: {str(e)}") app.logger.error(f"Error during printing: {str(e)}")
if attempt < max_retries - 1:
app.logger.info(f"Retrying after {retry_delay} seconds...")
time.sleep(retry_delay)
else:
app.logger.error("Max retries reached. Failed to send print job.")
else:
app.logger.error(f"Failed to acquire printer lock (Attempt {attempt + 1}). Printer is busy.")
if attempt < max_retries - 1:
app.logger.info(f"Retrying after {retry_delay} seconds...")
time.sleep(retry_delay)
else:
app.logger.error("Printer is still busy after retries.")
raise Exception("Printer is busy. Try again later.")
except Exception as e: except Exception as e:
app.logger.error(f"Error during label printing: {str(e)}") app.logger.error(f"Error during label printing: {str(e)}")