From c9fde8eea80efe718958c0a0884b32fd122ab12a Mon Sep 17 00:00:00 2001 From: n07070 Date: Sat, 28 Nov 2026 23:57:30 +0100 Subject: [PATCH] Add REMOVE task for images once printed --- src/task.py | 4 ++-- src/web.py | 27 +++++++++++++++------------ src/worker.py | 18 ++++++++++++------ 3 files changed, 29 insertions(+), 20 deletions(-) diff --git a/src/task.py b/src/task.py index 5c0312c..207797e 100644 --- a/src/task.py +++ b/src/task.py @@ -109,9 +109,9 @@ class RemoveTask(PrintTask): This class is used to clear images and leftovers after a print """ - def __init__(self, task_id, image_path): + def __init__(self, image_path): super().__init__(TaskType.REMOVE) - self.task_id = self._generate_id() if not task_id else task_id + self.task_id = self._generate_id() self.image_path = image_path # There is no print data, diff --git a/src/web.py b/src/web.py index e32e728..1899a33 100644 --- a/src/web.py +++ b/src/web.py @@ -5,7 +5,7 @@ Manage all of the inputs from a web source import os from werkzeug.utils import secure_filename import uuid -from task import TextTask, ImageTask +from task import TextTask, ImageTask, RemoveTask class Web: @@ -24,11 +24,6 @@ class Web: self.app.logger.debug("Printing : " + str(texte) + " from " + str(sign)) try: - # We create two new tasks and add them directly to the queue - # TODO: this might need to be improved because - # !! 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. for i in range(many): sms = self.print_queue.enqueue( TextTask(content=texte, signature=sign, do_cut=do_cut) @@ -49,7 +44,7 @@ class Web: 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. + Get an image and a signature, prints the image and cuts if asked after that. """ image_uuid = str(uuid.uuid4()) @@ -62,14 +57,18 @@ class Web: if file_uploaded: self.app.logger.debug("File has been uploaded, printing...") + image_path = os.path.join( self.app.config["UPLOAD_FOLDER"], + secure_filename( + str( + file_uploaded.split("/").pop() + ) + ), + ) try: - for i in range(many): + for _ in range(many): img = self.print_queue.enqueue( ImageTask( - os.path.join( - self.app.config["UPLOAD_FOLDER"], - secure_filename(str(file_uploaded.split("/").pop())), - ), + image_path, signature=sign, process=process_image, task_id=str(uuid.uuid4()), @@ -85,6 +84,10 @@ class Web: except Exception as e: raise RuntimeError("Could not add IMG to queue " + str(e)) from e + self.app.logger.debug("Adding a remove task for image of UUID %s", image_uuid) + self.print_queue.enqueue( + RemoveTask(image_path=image_path) + ) return True # def login(self, username: str, password: str) -> bool: diff --git a/src/worker.py b/src/worker.py index ab03c32..e9c8580 100644 --- a/src/worker.py +++ b/src/worker.py @@ -6,8 +6,8 @@ This is the main printing thread. A worker thread consums Tasks from import threading import time import traceback -import random from uuid import uuid4 +import os class PrintWorker(threading.Thread): @@ -67,7 +67,6 @@ class PrintWorker(threading.Thread): while True: # If the printer is dead or asleep, it can't work. if not self.running: - self.app.logger.debug("\t[⚙ %s] Worker is asleep", self.id) time.sleep(0.4) continue @@ -138,13 +137,20 @@ class PrintWorker(threading.Thread): task.status = "processing" - print_data = task.get_print_data() - try: - self.printer.print_task(task, print_data) + if task.task_type.value == "remove": + try: + self.app.logger.debug("Removing file %s", task.image_path) + os.remove(task.image_path) + except Exception as e: + self.app.logger.error("Could not remove file %s because %s", task.image_path, str(e)) + raise RuntimeError from e + else: + print_data = task.get_print_data() + self.printer.print_task(task, print_data) except RuntimeError as e: self.state = "idle" - self.app.logger.error("Could not print : %s", str(e)) + self.app.logger.error("Could not process task %s because: %s", task.task_id, str(e)) self.print_queue.mark_completed(task.task_id, "failed", log=str(e)) self.app.logger.error(traceback.format_exc()) self.printer.free_printer()