Apply linting to worker
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
# This is the main printing thread
|
||||
# As explained in the task file, this is where we command
|
||||
# printing to happen.
|
||||
"""
|
||||
This is the main printing thread. A worker thread consums Tasks from
|
||||
a PrintQueue, while trying to find available printers.
|
||||
"""
|
||||
|
||||
import threading
|
||||
import time
|
||||
@@ -8,13 +9,21 @@ from printers import Printers
|
||||
|
||||
|
||||
class PrintWorker(threading.Thread):
|
||||
def __init__(self, app, print_queue, socketio=None):
|
||||
"""
|
||||
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.
|
||||
|
||||
If a print fails, it's not retried, but will be added to a list of completed
|
||||
tasks.
|
||||
"""
|
||||
def __init__(self, app, print_queue):
|
||||
super().__init__(daemon=True)
|
||||
self.app = app
|
||||
self.print_queue = print_queue
|
||||
self.printer = None
|
||||
self._lock = threading.Lock()
|
||||
self.socketio = socketio # Optional
|
||||
self.running = True
|
||||
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...")
|
||||
@@ -30,8 +39,8 @@ class PrintWorker(threading.Thread):
|
||||
def run(self):
|
||||
"""Background thread that processes queue items"""
|
||||
self.app.logger.debug("Worker %s started working.", threading.get_ident())
|
||||
self.app.logger.debug("Current threads : %s" % threading.active_count())
|
||||
self.app.logger.debug("Threads actives : %s " % threading.enumerate())
|
||||
self.app.logger.debug("Current threads : %s" , threading.active_count())
|
||||
self.app.logger.debug("Threads actives : %s " , threading.enumerate())
|
||||
|
||||
while True:
|
||||
|
||||
@@ -40,7 +49,8 @@ class PrintWorker(threading.Thread):
|
||||
time.sleep(0.2)
|
||||
continue
|
||||
|
||||
# If we have no available printer, we look at the list printers we know about, and try to find one that is available.
|
||||
# If we have no available printer, we look at the list printers
|
||||
# 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:
|
||||
@@ -79,7 +89,6 @@ class PrintWorker(threading.Thread):
|
||||
self.app.logger.info("Got a new task")
|
||||
self.app.logger.debug("Got task %s", task.task_id)
|
||||
task.status = "processing"
|
||||
self._emit_status(task.task_id, "processing")
|
||||
|
||||
print_data = task.get_print_data()
|
||||
|
||||
@@ -92,9 +101,8 @@ class PrintWorker(threading.Thread):
|
||||
|
||||
task.status = "completed"
|
||||
self.print_queue.mark_completed(task.task_id, "completed")
|
||||
self._emit_status(task.task_id, "completed")
|
||||
self.app.logger.debug(
|
||||
"Finished printing task %s " % task.task_id
|
||||
"Finished printing task %s " , task.task_id
|
||||
)
|
||||
self.state = "idle"
|
||||
|
||||
@@ -102,9 +110,8 @@ class PrintWorker(threading.Thread):
|
||||
task.status = "failed"
|
||||
self.state = "idle"
|
||||
self.print_queue.mark_completed(task.task_id, "failed")
|
||||
self._emit_status(task.task_id, "failed", error=str(e))
|
||||
self.app.logger.error(
|
||||
"Could not print task %s because %s " % task.task_id, str(e)
|
||||
"Could not print task %s because %s " , task.task_id, str(e)
|
||||
)
|
||||
|
||||
else:
|
||||
@@ -112,23 +119,6 @@ class PrintWorker(threading.Thread):
|
||||
self.state = "idle"
|
||||
time.sleep(0.1)
|
||||
|
||||
def _emit_status(self, task_id, status, error=None):
|
||||
"""Emit status update via Socket.IO if available"""
|
||||
if not self.socketio:
|
||||
return
|
||||
|
||||
room = f"task_{task_id}"
|
||||
data = {
|
||||
"task_id": task_id,
|
||||
"status": status,
|
||||
"position": None, # Task no longer in queue
|
||||
}
|
||||
|
||||
if error:
|
||||
data["error"] = error
|
||||
|
||||
self.socketio.emit("task_status", data, room=room)
|
||||
|
||||
def stop_worker(self):
|
||||
"""
|
||||
Give the worker a break
|
||||
|
||||
Reference in New Issue
Block a user