--- a/src/pyams_media/process.py Thu Feb 08 08:53:54 2018 +0100
+++ b/src/pyams_media/process.py Sun Feb 18 12:47:28 2018 +0100
@@ -31,10 +31,11 @@
# import packages
from pyams_utils.registry import set_local_registry, get_utility, get_global_registry
+from pyams_utils.request import check_request
from pyams_utils.zodb import ZODBConnection
from pyams_zmq.handler import ZMQMessageHandler
from pyams_zmq.process import ZMQProcess
-from pyramid.threadlocal import manager as threadlocal_manager
+from pyramid.threadlocal import RequestContext
class ConversionProcess(Process):
@@ -46,12 +47,6 @@
def run(self):
logger.debug("Starting conversion process...")
- # Lower process nice...
- os.nice(10)
- # Loading components registry
- registry = get_global_registry()
- threadlocal_manager.set({'request': None, 'registry': registry})
- logger.debug("Getting global registry: {0!r}".format(registry))
# Check settings
settings = self.settings
logger.debug("Checking conversion parameters: {0}".format(str(settings)))
@@ -60,54 +55,65 @@
if not (media_id and media_format):
logger.warning('Bad conversion request: {0}'.format(str(settings)))
return
+ # Lower process nice...
+ os.nice(10)
+ # Loading components registry
+ registry = get_global_registry()
+ logger.debug("Getting global registry: {0!r}".format(registry))
+ # Checking converter
converter = registry.queryUtility(IMediaConverter, media_format)
if converter is None:
logger.warning('Missing media converter: {0}'.format(media_format))
return
- # Open ZODB connection
- manager = None
- zodb_name = settings.get('zodb_name')
- logger.debug("Opening ZODB connection...")
- with ZODBConnection(name=zodb_name) as root:
- try:
- logger.debug("Getting connection root {0!r}".format(root))
- application_name = registry.settings.get(PYAMS_APPLICATION_SETTINGS_KEY, PYAMS_APPLICATION_DEFAULT_NAME)
- application = root.get(application_name)
- logger.debug("Loading application {0!r} named {1}".format(application, application_name))
- if application is not None:
- # set local registry
- sm = application.getSiteManager()
- set_local_registry(sm)
- logger.debug("Setting local registry {0!r}".format(sm))
- # find media
- intids = get_utility(IIntIds)
- media = intids.queryObject(media_id)
- if media is None:
- logger.warning("Can't find requested media {0}!".format(media_id))
- return
- # extract converter output
- logger.debug("Starting conversion process for {0!r} to {1}".format(media, media_format))
- manager = ITransactionManager(media)
- for width, conversion in converter.convert(media):
- conversion_result = conversion.get('output', '')
- logger.debug("Finished FFmpeg conversion process to {0}, {1} bytes output"
- .format(media_format, len(conversion_result)))
- # add conversion in a transaction attempts loop
- if len(conversion_result) > 0:
- for attempt in manager.attempts():
- with attempt as t:
- converted = IMediaConversions(media).add_conversion(conversion_result, media_format,
- converter.format, width)
- IAnnotations(converted)[MEDIA_CONVERSION_CMDLINE_KEY] = conversion.get('cmdline')
- if t.status == 'Committed':
- break
- else:
- logger.warning("Finished FFmpeg conversion process with **NO OUTPUT** !!!")
- logger.warning(conversion.get('errors'))
- finally:
- if manager is not None:
- manager.abort()
- threadlocal_manager.pop()
+ # Create new request
+ request = check_request()
+ request.registry = registry
+ logger.debug("Creating new request {0!r}".format(request))
+ with RequestContext(request):
+ # Open ZODB connection
+ zodb_name = settings.get('zodb_name')
+ logger.debug("Opening ZODB connection...")
+ with ZODBConnection(name=zodb_name) as root:
+ manager = None
+ try:
+ logger.debug("Getting connection root {0!r}".format(root))
+ application_name = registry.settings.get(PYAMS_APPLICATION_SETTINGS_KEY,
+ PYAMS_APPLICATION_DEFAULT_NAME)
+ application = root.get(application_name)
+ logger.debug("Loading application {0!r} named {1}".format(application, application_name))
+ if application is not None:
+ # set local registry
+ sm = application.getSiteManager()
+ set_local_registry(sm)
+ logger.debug("Setting local registry {0!r}".format(sm))
+ # find media
+ intids = get_utility(IIntIds)
+ media = intids.queryObject(media_id)
+ if media is None:
+ logger.warning("Can't find requested media {0}!".format(media_id))
+ return
+ # extract converter output
+ logger.debug("Starting conversion process for {0!r} to {1}".format(media, media_format))
+ manager = ITransactionManager(media)
+ for width, conversion in converter.convert(media):
+ conversion_result = conversion.get('output', '')
+ logger.debug("Finished FFmpeg conversion process to {0}, {1} bytes output"
+ .format(media_format, len(conversion_result)))
+ # add conversion in a transaction attempts loop
+ if len(conversion_result) > 0:
+ for attempt in manager.attempts():
+ with attempt as t:
+ converted = IMediaConversions(media).add_conversion(conversion_result, media_format,
+ converter.format, width)
+ IAnnotations(converted)[MEDIA_CONVERSION_CMDLINE_KEY] = conversion.get('cmdline')
+ if t.status == 'Committed':
+ break
+ else:
+ logger.warning("Finished FFmpeg conversion process with **NO OUTPUT** !!!")
+ logger.warning(conversion.get('errors'))
+ finally:
+ if manager is not None:
+ manager.abort()
class ConversionThread(Thread):