27 from pyams_utils.interfaces import PYAMS_APPLICATION_SETTINGS_KEY, PYAMS_APPLICATION_DEFAULT_NAME |
27 from pyams_utils.interfaces import PYAMS_APPLICATION_SETTINGS_KEY, PYAMS_APPLICATION_DEFAULT_NAME |
28 from pyramid.interfaces import IApplicationCreated |
28 from pyramid.interfaces import IApplicationCreated |
29 |
29 |
30 # import packages |
30 # import packages |
31 from pyams_media.process import MediaConversionProcess, MediaConversionMessageHandler |
31 from pyams_media.process import MediaConversionProcess, MediaConversionMessageHandler |
|
32 from pyams_utils.protocol.tcp import is_port_in_use |
32 from pyams_utils.registry import set_local_registry, get_global_registry |
33 from pyams_utils.registry import set_local_registry, get_global_registry |
33 from pyams_utils.zodb import get_connection_from_settings |
34 from pyams_utils.zodb import get_connection_from_settings |
34 from pyams_zmq.process import process_exit_func |
35 from pyams_zmq.process import process_exit_func |
35 from pyramid.events import subscriber |
36 from pyramid.events import subscriber |
36 from pyramid.settings import asbool |
37 from pyramid.settings import asbool |
54 @subscriber(IApplicationCreated) |
55 @subscriber(IApplicationCreated) |
55 def handle_new_application(event): |
56 def handle_new_application(event): |
56 """Start converter process when application created""" |
57 """Start converter process when application created""" |
57 |
58 |
58 # check for PyAMS command line script |
59 # check for PyAMS command line script |
59 cmsline = os.path.split(sys.argv[0])[-1] |
60 cmdline = os.path.split(sys.argv[0])[-1] |
60 if cmsline.startswith('pyams_'): |
61 if cmdline.startswith('pyams_'): |
61 return |
62 return |
62 |
63 |
63 registry = get_global_registry() |
64 registry = get_global_registry() |
64 settings = registry.settings |
65 settings = registry.settings |
65 start_handler = asbool(settings.get(CONVERTER_STARTER_KEY, False)) |
66 start_handler = asbool(settings.get(CONVERTER_STARTER_KEY, False)) |
66 if start_handler: |
67 if start_handler: |
|
68 # check if port is available |
|
69 handler_address = settings.get(CONVERTER_HANDLER_KEY, '127.0.0.1:5556') |
|
70 hostname, port = handler_address.split(':') |
|
71 if is_port_in_use(int(port), hostname): |
|
72 logger.info("Medias converter port already used, aborting...") |
|
73 return |
67 # get database connection |
74 # get database connection |
68 connection = get_connection_from_settings(settings) |
75 connection = get_connection_from_settings(settings) |
69 root = connection.root() |
76 root = connection.root() |
70 # get application |
77 # get application |
71 application_name = settings.get(PYAMS_APPLICATION_SETTINGS_KEY, PYAMS_APPLICATION_DEFAULT_NAME) |
78 application_name = settings.get(PYAMS_APPLICATION_SETTINGS_KEY, PYAMS_APPLICATION_DEFAULT_NAME) |
76 process = None |
83 process = None |
77 try: |
84 try: |
78 conversion_util = sm.get(CONVERTER_NAME) |
85 conversion_util = sm.get(CONVERTER_NAME) |
79 if conversion_util is not None: |
86 if conversion_util is not None: |
80 # create medias converter process |
87 # create medias converter process |
81 process = MediaConversionProcess(settings.get(CONVERTER_HANDLER_KEY, '127.0.0.1:5556'), |
88 process = MediaConversionProcess(handler_address, |
82 MediaConversionMessageHandler, |
89 MediaConversionMessageHandler, |
83 settings.get(CONVERTER_AUTH_KEY), |
90 settings.get(CONVERTER_AUTH_KEY), |
84 settings.get(CONVERTER_CLIENTS_KEY), |
91 settings.get(CONVERTER_CLIENTS_KEY), |
85 registry) |
92 registry) |
86 logger.info('Starting medias converter {0!r}...'.format(process)) |
93 logger.info('Starting medias converter {0!r}...'.format(process)) |