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
|
This is the main printing thread. A worker thread consums Tasks from
|
||||||
# printing to happen.
|
a PrintQueue, while trying to find available printers.
|
||||||
|
"""
|
||||||
|
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
@@ -8,13 +9,21 @@ from printers import Printers
|
|||||||
|
|
||||||
|
|
||||||
class PrintWorker(threading.Thread):
|
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)
|
super().__init__(daemon=True)
|
||||||
self.app = app
|
self.app = app
|
||||||
self.print_queue = print_queue
|
self.print_queue = print_queue
|
||||||
self.printer = None
|
self.printer = None
|
||||||
self._lock = threading.Lock()
|
self._lock = threading.Lock()
|
||||||
self.socketio = socketio # Optional
|
|
||||||
self.running = True
|
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...")
|
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):
|
def run(self):
|
||||||
"""Background thread that processes queue items"""
|
"""Background thread that processes queue items"""
|
||||||
self.app.logger.debug("Worker %s started working.", threading.get_ident())
|
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("Current threads : %s" , threading.active_count())
|
||||||
self.app.logger.debug("Threads actives : %s " % threading.enumerate())
|
self.app.logger.debug("Threads actives : %s " , threading.enumerate())
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
|
|
||||||
@@ -40,7 +49,8 @@ class PrintWorker(threading.Thread):
|
|||||||
time.sleep(0.2)
|
time.sleep(0.2)
|
||||||
continue
|
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 find a printer, we acquire it
|
||||||
# When we are finished with a printer, we release it to the world.
|
# When we are finished with a printer, we release it to the world.
|
||||||
while not self.printer or not self.printer.ready:
|
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.info("Got a new task")
|
||||||
self.app.logger.debug("Got task %s", task.task_id)
|
self.app.logger.debug("Got task %s", task.task_id)
|
||||||
task.status = "processing"
|
task.status = "processing"
|
||||||
self._emit_status(task.task_id, "processing")
|
|
||||||
|
|
||||||
print_data = task.get_print_data()
|
print_data = task.get_print_data()
|
||||||
|
|
||||||
@@ -92,9 +101,8 @@ class PrintWorker(threading.Thread):
|
|||||||
|
|
||||||
task.status = "completed"
|
task.status = "completed"
|
||||||
self.print_queue.mark_completed(task.task_id, "completed")
|
self.print_queue.mark_completed(task.task_id, "completed")
|
||||||
self._emit_status(task.task_id, "completed")
|
|
||||||
self.app.logger.debug(
|
self.app.logger.debug(
|
||||||
"Finished printing task %s " % task.task_id
|
"Finished printing task %s " , task.task_id
|
||||||
)
|
)
|
||||||
self.state = "idle"
|
self.state = "idle"
|
||||||
|
|
||||||
@@ -102,9 +110,8 @@ class PrintWorker(threading.Thread):
|
|||||||
task.status = "failed"
|
task.status = "failed"
|
||||||
self.state = "idle"
|
self.state = "idle"
|
||||||
self.print_queue.mark_completed(task.task_id, "failed")
|
self.print_queue.mark_completed(task.task_id, "failed")
|
||||||
self._emit_status(task.task_id, "failed", error=str(e))
|
|
||||||
self.app.logger.error(
|
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:
|
else:
|
||||||
@@ -112,23 +119,6 @@ class PrintWorker(threading.Thread):
|
|||||||
self.state = "idle"
|
self.state = "idle"
|
||||||
time.sleep(0.1)
|
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):
|
def stop_worker(self):
|
||||||
"""
|
"""
|
||||||
Give the worker a break
|
Give the worker a break
|
||||||
|
|||||||
Reference in New Issue
Block a user