Multi-printers #32

Open
n07070 wants to merge 80 commits from multi-printers into master
pull from: multi-printers
3 changed files with 29 additions and 20 deletions
Showing only changes of commit c9fde8eea8 - Show all commits

No files matched your search

+2 -2
View File
@@ -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,
+15 -12
View File
@@ -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:
+12 -6
View File
@@ -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()