Skip to content

Commit c974331

Browse files
committed
Added a function to the DirWatcher and Analyser to check if the thread is safe to stop
1 parent 8dec4b2 commit c974331

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

src/murfey/client/analyser.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ def _analyse(self):
370370
)
371371
self.post_transfer(transferred_file)
372372
self.queue.task_done()
373+
logger.debug("Analyer thread has stopped analysing incoming files")
373374
self.notify(final=True)
374375

375376
def _xml_file(self, data_file: Path) -> Path:
@@ -403,6 +404,12 @@ def request_stop(self):
403404
self._stopping = True
404405
self._halt_thread = True
405406

407+
def is_safe_to_stop(self):
408+
"""
409+
Checks that the analyser thread is safe to stop
410+
"""
411+
return self._stopping and self._halt_thread and not self.queue.qsize()
412+
406413
def stop(self):
407414
logger.debug("Analyser thread stop requested")
408415
self._stopping = True
@@ -412,5 +419,8 @@ def stop(self):
412419
self.queue.put(None)
413420
self.thread.join()
414421
except Exception as e:
415-
logger.error(f"Exception encountered while stopping analyser: {e}")
422+
logger.error(
423+
f"Exception encountered while stopping Analyser: {e}",
424+
exc_info=True,
425+
)
416426
logger.debug("Analyser thread stop completed")

src/murfey/client/watchdir.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,27 @@ def request_stop(self):
6767
self._stopping = True
6868
self._halt_thread = True
6969

70+
def is_safe_to_stop(self):
71+
"""
72+
Checks that the directory watcher thread is safe to stop
73+
"""
74+
return self._stopping and self._halt_thread and not self.queue.qsize()
75+
7076
def stop(self):
71-
log.debug("DirWatcher thread stop requested")
7277
self._stopping = True
7378
if self.thread.is_alive():
7479
self.queue.join()
7580

7681
self._halt_thread = True
77-
if self.thread.is_alive():
78-
self.queue.put(None)
79-
self.thread.join()
82+
try:
83+
if self.thread.is_alive():
84+
self.queue.put(None)
85+
self.thread.join()
86+
except Exception as e:
87+
log.error(
88+
f"Exception encountered while stopping DirWatcher: {e}",
89+
exc_info=True,
90+
)
8091
log.debug("DirWatcher thread stop completed")
8192

8293
def _process(self):
@@ -94,6 +105,7 @@ def _process(self):
94105
modification_time=modification_time, transfer_all=self._transfer_all
95106
)
96107
time.sleep(15)
108+
log.debug(f"DirWatcher {self} has stopped scanning")
97109
self.notify(final=True)
98110

99111
def scan(self, modification_time: float | None = None, transfer_all: bool = False):

0 commit comments

Comments
 (0)