src/pyams_media/include.py
changeset 66 6f502c50ef09
parent 65 d38220c1116e
child 73 93ad8b37b0c5
equal deleted inserted replaced
65:d38220c1116e 66:6f502c50ef09
    19 logger = logging.getLogger('PyAMS (media)')
    19 logger = logging.getLogger('PyAMS (media)')
    20 
    20 
    21 import sys
    21 import sys
    22 
    22 
    23 # import interfaces
    23 # import interfaces
    24 from pyams_media.interfaces import CONVERTER_HANDLER_KEY, CONVERTER_NAME
    24 from pyams_media.interfaces import CONVERTER_HANDLER_KEY, CONVERTER_STARTER_KEY, CONVERTER_NAME
    25 from pyams_utils.interfaces import PYAMS_APPLICATION_SETTINGS_KEY, PYAMS_APPLICATION_DEFAULT_NAME
    25 from pyams_utils.interfaces import PYAMS_APPLICATION_SETTINGS_KEY, PYAMS_APPLICATION_DEFAULT_NAME
    26 from pyramid.interfaces import IApplicationCreated
    26 from pyramid.interfaces import IApplicationCreated
    27 
    27 
    28 # import packages
    28 # import packages
    29 from pyams_media.process import MediaConversionProcess, MediaConversionMessageHandler
    29 from pyams_media.process import MediaConversionProcess, MediaConversionMessageHandler
    30 from pyams_utils.registry import set_local_registry
    30 from pyams_utils.registry import set_local_registry
    31 from pyams_utils.zodb import get_connection_from_settings
    31 from pyams_utils.zodb import get_connection_from_settings
    32 from pyams_zmq.process import process_exit_func
    32 from pyams_zmq.process import process_exit_func
    33 from pyramid.events import subscriber
    33 from pyramid.events import subscriber
       
    34 from pyramid.settings import asbool
    34 from zope.component.globalregistry import getGlobalSiteManager
    35 from zope.component.globalregistry import getGlobalSiteManager
    35 
    36 
    36 
    37 
    37 def include_package(config):
    38 def include_package(config):
    38     """Pyramid include"""
    39     """Pyramid include"""
    57     if sys.argv[0].endswith('pyams_upgrade'):
    58     if sys.argv[0].endswith('pyams_upgrade'):
    58         return
    59         return
    59 
    60 
    60     registry = getGlobalSiteManager()
    61     registry = getGlobalSiteManager()
    61     settings = registry.settings
    62     settings = registry.settings
    62     start_handler = settings.get(CONVERTER_HANDLER_KEY, False)
    63     start_handler = asbool(settings.get(CONVERTER_STARTER_KEY, False))
    63     if start_handler:
    64     if start_handler:
    64         # get database connection
    65         # get database connection
    65         connection = get_connection_from_settings(settings)
    66         connection = get_connection_from_settings(settings)
    66         root = connection.root()
    67         root = connection.root()
    67         # get application
    68         # get application
    73             set_local_registry(sm)
    74             set_local_registry(sm)
    74             try:
    75             try:
    75                 conversion_util = sm.get(CONVERTER_NAME)
    76                 conversion_util = sm.get(CONVERTER_NAME)
    76                 if conversion_util is not None:
    77                 if conversion_util is not None:
    77                     # create medias converter process
    78                     # create medias converter process
    78                     process = MediaConversionProcess(start_handler, MediaConversionMessageHandler, registry)
    79                     process = MediaConversionProcess(settings.get(CONVERTER_HANDLER_KEY, '127.0.0.1:5556'),
    79                     logger.debug('Starting medias conversion process {0!r}...'.format(process))
    80                                                      MediaConversionMessageHandler, registry)
       
    81                     logger.info('Starting medias converter {0!r}...'.format(process))
    80                     process.start()
    82                     process.start()
    81                     if process.is_alive():
    83                     if process.is_alive():
    82                         atexit.register(process_exit_func, process=process)
    84                         atexit.register(process_exit_func, process=process)
    83                         logger.debug('Started medias conversion process {0!r} with PID {1}...'.format(process,
    85                         logger.info('Started medias converter with PID {0}.'.format(process.pid))
    84                                                                                                       process.pid))
       
    85             finally:
    86             finally:
    86                 if process and not process.is_alive():
    87                 if process and not process.is_alive():
    87                     process.terminate()
    88                     process.terminate()
    88                     process.join()
    89                     process.join()
    89                 set_local_registry(None)
    90                 set_local_registry(None)