better manage a segmenter failure to start and kill the process

This commit is contained in:
Romain Bazile 2021-09-06 18:58:23 +02:00
parent bd85bc0d28
commit 1cf9874391
2 changed files with 28 additions and 9 deletions

View file

@ -66,6 +66,7 @@ run = True
def handler_stop_signals(signum, frame):
"""This handler simply stop the forever running loop in __main__"""
global run
logger.info(f"Received a signal asking to stop {signum}")
run = False
@ -131,9 +132,14 @@ if __name__ == "__main__":
# Starts the segmenter process
logger.info("Starting the segmenter control process (step 4/4)")
try:
segmenter_thread = planktoscope.segmenter.SegmenterProcess(
shutdown_event, "/home/pi/data"
)
except Exception as e:
logger.error("The segmenter control process could not be started")
else:
segmenter_thread = None
segmenter_thread.start()
# Starts the module process
@ -154,28 +160,34 @@ if __name__ == "__main__":
if not stepper_thread.is_alive():
logger.error("The stepper process died unexpectedly! Oh no!")
break
if imager_thread and not imager_thread.is_alive():
if not imager_thread or not imager_thread.is_alive():
logger.error("The imager process died unexpectedly! Oh no!")
break
if not segmenter_thread.is_alive():
if not segmenter_thread or not segmenter_thread.is_alive():
logger.error("The segmenter process died unexpectedly! Oh no!")
break
time.sleep(1)
display.display_text("Bye Bye!")
logger.info("Shutting down the shop")
shutdown_event.set()
time.sleep(1)
stepper_thread.join()
if imager_thread:
imager_thread.join()
if segmenter_thread:
segmenter_thread.join()
# Uncomment this for clean shutdown
# module_thread.join()
stepper_thread.close()
if imager_thread:
imager_thread.close()
if segmenter_thread:
segmenter_thread.close()
# Uncomment this for clean shutdown
# module_thread.close()
display.stop()
logger.info("Bye")

View file

@ -954,7 +954,14 @@ class SegmenterProcess(multiprocessing.Process):
handler = functools.partial(
planktoscope.segmenter.streamer.StreamingHandler, refresh_delay
)
try:
server = planktoscope.segmenter.streamer.StreamingServer(address, handler)
except Exception as e:
logger.exception(
f"An exception has occured when starting up the segmenter: {e}"
)
raise e
self.streaming_thread = threading.Thread(
target=server.serve_forever, daemon=True
)