Check if medias converter port is available before starting process to avoid zombies
authorThierry Florac <thierry.florac@onf.fr>
Fri, 09 Nov 2018 12:11:44 +0100
changeset 115 514949952033
parent 114 70d8305004c3
child 116 de79cf463a44
Check if medias converter port is available before starting process to avoid zombies
src/pyams_media/include.py
--- a/src/pyams_media/include.py	Fri Nov 09 12:01:44 2018 +0100
+++ b/src/pyams_media/include.py	Fri Nov 09 12:11:44 2018 +0100
@@ -29,6 +29,7 @@
 
 # import packages
 from pyams_media.process import MediaConversionProcess, MediaConversionMessageHandler
+from pyams_utils.protocol.tcp import is_port_in_use
 from pyams_utils.registry import set_local_registry, get_global_registry
 from pyams_utils.zodb import get_connection_from_settings
 from pyams_zmq.process import process_exit_func
@@ -56,14 +57,20 @@
     """Start converter process when application created"""
 
     # check for PyAMS command line script
-    cmsline = os.path.split(sys.argv[0])[-1]
-    if cmsline.startswith('pyams_'):
+    cmdline = os.path.split(sys.argv[0])[-1]
+    if cmdline.startswith('pyams_'):
         return
 
     registry = get_global_registry()
     settings = registry.settings
     start_handler = asbool(settings.get(CONVERTER_STARTER_KEY, False))
     if start_handler:
+        # check if port is available
+        handler_address = settings.get(CONVERTER_HANDLER_KEY, '127.0.0.1:5556')
+        hostname, port = handler_address.split(':')
+        if is_port_in_use(int(port), hostname):
+            logger.info("Medias converter port already used, aborting...")
+            return
         # get database connection
         connection = get_connection_from_settings(settings)
         root = connection.root()
@@ -78,7 +85,7 @@
                 conversion_util = sm.get(CONVERTER_NAME)
                 if conversion_util is not None:
                     # create medias converter process
-                    process = MediaConversionProcess(settings.get(CONVERTER_HANDLER_KEY, '127.0.0.1:5556'),
+                    process = MediaConversionProcess(handler_address,
                                                      MediaConversionMessageHandler,
                                                      settings.get(CONVERTER_AUTH_KEY),
                                                      settings.get(CONVERTER_CLIENTS_KEY),