From 836d6b0be9ad18345f17b6aa34dda49cd0bdbd68 Mon Sep 17 00:00:00 2001 From: n07070 Date: Fri, 13 Nov 2026 19:16:56 +0100 Subject: [PATCH] Update configuration options in the web methods --- src/web.py | 42 ++++++++++++++++++++++-------------------- src/worker.py | 24 ++++++++++++++++-------- 2 files changed, 38 insertions(+), 28 deletions(-) diff --git a/src/web.py b/src/web.py index fb23d30..77c8ada 100644 --- a/src/web.py +++ b/src/web.py @@ -16,7 +16,7 @@ class Web: self.print_queue = print_queue self.app = app - def print_sms(self, texte, sign: str, do_cut: bool) -> bool: + def print_sms(self, texte, sign: str, do_cut: bool, many: int = 1) -> bool: """ Get text and a signature, prints the text and cuts after that. """ @@ -28,19 +28,20 @@ class Web: # !! there is no garantee !! that both the SMS task and the Cut task # are added back to back, another task could be # inserted between the two. - sms = self.print_queue.enqueue(TextTask(content=texte, signature=sign)) - self.app.logger.info("Added a SMS task at position %s", sms) + for i in range(many): + sms = self.print_queue.enqueue(TextTask(content=texte, signature=sign)) + self.app.logger.info("Added a SMS task at position %s", sms) + if do_cut: + cut = self.print_queue.enqueue(CutTask()) + self.app.logger.info("Added a CUT task at position %s", cut) - if do_cut: - cut = self.print_queue.enqueue(CutTask()) - self.app.logger.info("Added a CUT task at position %s", cut) except Exception as e: self.app.logger.error(e) raise RuntimeError("Could not add SMS to queue, " + str(e)) from e return True - def print_image(self, image, sign: str, do_cut: bool, process_image: bool) -> bool: + def print_image(self, image, sign: str, do_cut: bool, process_image: bool, many: int = 1) -> bool: """ Get an image and a signature, prints the image and cuts after that. """ @@ -53,21 +54,22 @@ class Web: if file_uploaded: self.app.logger.debug("File has been uploaded, printing...") try: - img = self.print_queue.enqueue( - ImageTask( - os.path.join( - self.app.config["UPLOAD_FOLDER"], - secure_filename(image.filename), - ), - signature=sign, - process=process_image, + for i in range(many): + img = self.print_queue.enqueue( + ImageTask( + os.path.join( + self.app.config["UPLOAD_FOLDER"], + secure_filename(image.filename), + ), + signature=sign, + process=process_image, + ) ) - ) - self.app.logger.info("Added a IMG task at position %s", img) + self.app.logger.info("Added a IMG task at position %s", img) - if do_cut: - cut = self.print_queue.enqueue(CutTask()) - self.app.logger.info("Added a CUT task at position %s", cut) + if do_cut: + cut = self.print_queue.enqueue(CutTask()) + self.app.logger.info("Added a CUT task at position %s", cut) except Exception as e: raise RuntimeError("Could not add IMG to queue" + str(e)) from e diff --git a/src/worker.py b/src/worker.py index 04dbc8e..323f6bf 100644 --- a/src/worker.py +++ b/src/worker.py @@ -6,7 +6,6 @@ This is the main printing thread. A worker thread consums Tasks from import threading import time import traceback -from printers import Printers class PrintWorker(threading.Thread): @@ -21,7 +20,7 @@ class PrintWorker(threading.Thread): tasks. TODO : Change that """ - def __init__(self, app, print_queue, printers_pool): + def __init__(self, app, print_queue, printers_pool, printer_index=0): super().__init__(daemon=True) self.app = app self.print_queue = print_queue @@ -33,7 +32,9 @@ class PrintWorker(threading.Thread): # All workers share the same pool of printers. self.printers = printers_pool self.printers_obj = self.printers.printers - self._printer_index = 0 + # If we create several workers, we can assign an index + # so that we lookup the nth printer for the nth worker + self._printer_index = printer_index self.app.logger.debug("Worker initialized.") self.app.logger.debug("Printers connected:") @@ -44,11 +45,13 @@ class PrintWorker(threading.Thread): self.app.logger.debug("Ho great, I'm alive... I'm ready to work another day...") def _acquire_available_printer(self): - result = self.printers.acquire_available_printer(self._printer_index) + result = self.printers.acquire_available_printer(start_index=self._printer_index) if result is None: return None index, printer = result + # In case we need to lookup another printer, we don't + # look at the same printer in the printer list self._printer_index = (index + 1) % len(self.printers_obj) return printer @@ -78,14 +81,17 @@ class PrintWorker(threading.Thread): ) self.printer = self._acquire_available_printer() self.app.logger.debug( - "The worker got a %s printer and it's %s", - self.printer.printer_type, - "Ready" if self.printer.ready else "Not ready", + + "The worker got a %s printer and it's %s", + self.printer.printer_type, + "Ready" if self.printer.ready else "Not ready", ) if self.printer is None: time.sleep(1) + + self.printer.use_printer() # If the printer is not ready, we wait for it to become ready. @@ -128,7 +134,6 @@ class PrintWorker(threading.Thread): self.app.logger.error("Could not print : %s", str(e)) self.app.logger.error(traceback.format_exc()) self.printer.free_printer() - raise e task.status = "completed" self.print_queue.mark_completed(task.task_id, "completed") @@ -156,7 +161,10 @@ class PrintWorker(threading.Thread): """ self.app.logger.debug("Giving the worker a break") self.state = "drinking-a-beer" + # Free the printer during the worker break + # so it can be used by someone else self.printer.free_printer() + self.printer = None self.running = False def start_worker(self):