imager: add proper handling of startup exceptions

This commit is contained in:
Romain Bazile 2021-05-06 02:32:42 +02:00
parent 1da571bfef
commit 3840b15d7d
2 changed files with 24 additions and 11 deletions

View file

@ -120,9 +120,14 @@ if __name__ == "__main__":
stepper_thread.start()
# Starts the imager control process
logger.info("Starting the imager control process (step 3/4)")
imager_thread = planktoscope.imager.ImagerProcess(shutdown_event)
imager_thread.start()
logger.info("Starting the imager control process (step 3/6)")
try:
imager_thread = planktoscope.imager.ImagerProcess(shutdown_event)
except:
logger.error("The imager control process could not be started")
imager_thread = None
else:
imager_thread.start()
# Starts the segmenter process
logger.info("Starting the segmenter control process (step 4/4)")
@ -148,7 +153,7 @@ if __name__ == "__main__":
if not stepper_thread.is_alive():
logger.error("The stepper process died unexpectedly! Oh no!")
break
if not imager_thread.is_alive():
if imager_thread and not imager_thread.is_alive():
logger.error("The imager process died unexpectedly! Oh no!")
break
if not segmenter_thread.is_alive():
@ -160,12 +165,14 @@ if __name__ == "__main__":
shutdown_event.set()
time.sleep(1)
stepper_thread.join()
imager_thread.join()
if imager_thread:
imager_thread.join()
segmenter_thread.join()
# Uncomment this for clean shutdown
# module_thread.join()
stepper_thread.close()
imager_thread.close()
if imager_thread:
imager_thread.close()
segmenter_thread.close()
# Uncomment this for clean shutdown
# module_thread.close()

View file

@ -93,7 +93,7 @@ class StreamingHandler(http.server.BaseHTTPRequestHandler):
try:
while True:
try:
with open("/dev/shm/mjpeg/cam.jpg", "rb") as jpeg:
with open("/dev/shm/mjpeg/cam.jpg", "rb") as jpeg: # nosec
frame = jpeg.read()
except FileNotFoundError as e:
logger.error(f"Camera has not been started yet")
@ -129,7 +129,6 @@ logger.info("planktoscope.imager is loaded")
class ImagerProcess(multiprocessing.Process):
"""This class contains the main definitions for the imager of the PlanktoScope"""
@logger.catch
def __init__(self, stop_event, iso=100, shutter_speed=1):
"""Initialize the Imager class
@ -179,7 +178,14 @@ class ImagerProcess(multiprocessing.Process):
logger.exception(
f"An exception has occured when starting up raspimjpeg: {e}"
)
exit(1)
try:
self.__camera.start(true)
except Exception as e:
logger.exception(
f"A second exception has occured when starting up raspimjpeg: {e}"
)
logger.error(f"This error can't be recovered from, terminating now")
raise e
if self.__camera.sensor_name == "IMX219": # Camera v2.1
self.__resolution = (3280, 2464)
@ -653,7 +659,7 @@ class ImagerProcess(multiprocessing.Process):
return
logger.debug(f"Copying the image from the temp file to {filename_path}")
shutil.copy("/dev/shm/mjpeg/image.jpg", filename_path)
shutil.copy("/dev/shm/mjpeg/image.jpg", filename_path) # nosec
# TODO Try to stop the camera streaming and display instead each captured image
# os.rename("/dev/shm/mjpeg/image.jpg", "/dev/shm/mjpeg/cam.jpg")
logger.debug("Syncing the disk")