Apply linting

This commit is contained in:
n07070
2026-05-21 02:57:27 +02:00
parent c50922790d
commit 2daafe28f2
8 changed files with 209 additions and 155 deletions

View File

@@ -4,7 +4,7 @@
import threading
import time
from task import TaskType
class PrintWorker(threading.Thread):
def __init__(self, app, print_queue, printer, socketio=None):
@@ -14,7 +14,7 @@ class PrintWorker(threading.Thread):
self.printer = printer
self.socketio = socketio # Optional
self.running = True
self.state = "idle" # idle, printing, dead, drinking-a-beer
self.state = "idle" # idle, printing, dead, drinking-a-beer
self.app.logger.debug("Ho great, I'm alive... I'm ready to work another day...")
@@ -22,23 +22,17 @@ class PrintWorker(threading.Thread):
"""Background thread that processes queue items"""
self.app.logger.info("Worker started working.")
while True:
if not self.running:
if not self.running or not self.printer.ready:
time.sleep(0.2)
continue
# TODO: This could be improved to simply no start
# the while loop as long as the printer is not ready.
# and maybe get out of it when the printer is not ready anymore ?
if not self.printer.ready:
self.app.logger.debug("Waiting for the printer to be ready...")
time.sleep(1)
continue
try:
task = self.print_queue.dequeue()
except Exception as e:
self.app.logger.error("Could not get a new task ! %s ", str(e))
raise RuntimeError("We could not get a new task because " + str(e)) from e
raise RuntimeError(
"We could not get a new task because " + str(e)
) from e
if task:
try:
@@ -51,7 +45,7 @@ class PrintWorker(threading.Thread):
print_data = task.get_print_data()
try:
self.printer.print_task(task.task_type, print_data)
except Exception as e:
except RuntimeError as e:
self.app.logger.error("Could not print : %s", str(e))
raise e
@@ -59,7 +53,7 @@ class PrintWorker(threading.Thread):
self.print_queue.mark_completed(task.task_id, "completed")
self._emit_status(task.task_id, "completed")
except Exception as e:
except RuntimeError as e:
task.status = "failed"
self.print_queue.mark_completed(task.task_id, "failed")
self._emit_status(task.task_id, "failed", error=str(e))
@@ -78,18 +72,19 @@ class PrintWorker(threading.Thread):
data = {
"task_id": task_id,
"status": status,
"position": None # Task no longer in queue
"position": None, # Task no longer in queue
}
if error:
data["error"] = error
self.socketio.emit('task_status', data, room=room)
self.socketio.emit("task_status", data, room=room)
def stop_worker(self):
"""
Give the worker a break
"""
self.app.logger.debug("Giving the worker a break")
self.state = "drinking-a-beer"
self.running = False
@@ -97,6 +92,7 @@ class PrintWorker(threading.Thread):
"""
Get the worker back to it
"""
self.app.logger.debug("Time to work !")
self.state = "idle"
self.running = True
@@ -107,5 +103,5 @@ class PrintWorker(threading.Thread):
return {
"is_running": self.running,
"queue_size": len(self.print_queue),
"state" : self.state
}
"state": self.state,
}