Change the way a Printer is used by the worker

This commit is contained in:
n07070
2026-10-11 21:42:29 +02:00
parent 31213eeeea
commit fd0507ff4f
4 changed files with 59 additions and 9 deletions

View File

@@ -5,6 +5,7 @@ This is the main printing thread. A worker thread consums Tasks from
import threading
import time
import traceback
from printers import Printers
@@ -13,7 +14,8 @@ class PrintWorker(threading.Thread):
A thread used to consume Tasks added to a Print Queue.
On initialisation, the worker will try to find Printers,
and on each print, choose an available Printer from a list of Printers.
and on each print, if the choosen Printer becomes unavailable, it
will choose an available Printer from a list of Printers.
If a print fails, it's not retried, but will be added to a list of completed
tasks.
@@ -30,8 +32,9 @@ class PrintWorker(threading.Thread):
try:
self.printers = Printers(self.app)
self.printers_obj = self.printers.printers
self.printers = iter(self.printers.printers)
self.printers_obj = self.printers.printers # Get the number of printers connected to the system
self.printers_list = iter(self.printers.printers)
except RuntimeError as e:
self.app.logger.warning("Could not get any Printers")
raise e
@@ -53,11 +56,31 @@ class PrintWorker(threading.Thread):
# we know about, and try to find one that is available.
# When we find a printer, we acquire it
# When we are finished with a printer, we release it to the world.
while not self.printer or not self.printer.ready:
# If we do not have a printer, we try to find one
while not self.printer:
time.sleep(1)
try:
self.app.logger.debug("Changing printers")
self.printer = next(self.printers)
self.app.logger.debug("Trying to find a available printer for the worker.")
# From the list of printer, we get one.
# If no printer in the list is avaible, we try again from the start of the loop, that we reset.
while self.printer.used or not self.printer:
self.printer = next(self.printers_list)
try:
self.printer = next(self.printer)
except StopIteration as e:
self.app.error("We do not have any available printers. Resetting our list of printers and trying again. " + str(e))
self.printers_list = iter(self.printers.printers)
time.sleep(1)
# TODO : Here, in case there is really no printer available, we will stay in a loop forever. Maybe after a number of tries, we should die.
# Here, we must have found a Printer() that is
# Printer.used = False, which means it's available.
# Therefore, we mark it used, so no other worker uses it.
self.printer.use_printer()
self.app.logger.debug(
"The worker got a %s printer and it's %s",
self.printer.printer_type,
@@ -67,6 +90,11 @@ class PrintWorker(threading.Thread):
self.app.logger.error("No printer detected" + str(e))
self.printer = None
# If the printer is not ready, we wait for it to become ready.
while not self.printer.ready:
self.app.debug("Waiting for our printer to become ready...")
time.sleep(1)
if self.state != "idle":
self.app.logger("We are not idle, waiting...")
time.sleep(1)
@@ -80,6 +108,7 @@ class PrintWorker(threading.Thread):
except Exception as e:
self.app.logger.error("Could not get a new task ! %s ", str(e))
self.state = "idle"
self.printer.free_printer()
raise RuntimeError(
"We could not get a new task because " + str(e)
) from e
@@ -97,6 +126,8 @@ class PrintWorker(threading.Thread):
except RuntimeError as e:
self.state = "idle"
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"
@@ -125,6 +156,7 @@ class PrintWorker(threading.Thread):
"""
self.app.logger.debug("Giving the worker a break")
self.state = "drinking-a-beer"
self.printer.free_printer()
self.running = False
def start_worker(self):