Use request context in task run methodi
authorThierry Florac <tflorac@ulthar.net>
Sun, 18 Feb 2018 12:52:08 +0100
changeset 51 2eea97d70f10
parent 50 c66a8bf09662
child 52 b8698a742bed
Use request context in task run methodi
src/pyams_scheduler/task.py
--- a/src/pyams_scheduler/task.py	Sun Feb 18 12:50:18 2018 +0100
+++ b/src/pyams_scheduler/task.py	Sun Feb 18 12:52:08 2018 +0100
@@ -9,6 +9,7 @@
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 # FOR A PARTICULAR PURPOSE.
 #
+from pyramid.threadlocal import RequestContext
 
 __docformat__ = 'restructuredtext'
 
@@ -39,7 +40,7 @@
 from apscheduler.triggers.base import BaseTrigger
 from persistent import Persistent
 from pyams_utils.date import get_duration
-from pyams_utils.registry import query_utility, get_utility, set_local_registry
+from pyams_utils.registry import query_utility, get_utility, set_local_registry, get_global_registry
 from pyams_utils.request import check_request
 from pyams_utils.timezone import tztime
 from pyams_utils.traversing import get_parent
@@ -246,39 +247,41 @@
         with zodb_connection as root:
             try:
                 registry = kwargs.get('registry')
-                request = check_request()
-                request.registry = registry
-                application_name = registry.settings.get(PYAMS_APPLICATION_SETTINGS_KEY,
-                                                         PYAMS_APPLICATION_DEFAULT_NAME)
-                sm = root.get(application_name).getSiteManager()
-                scheduler_util = sm.get(SCHEDULER_NAME)
-                task = scheduler_util.get(self.__name__)
-                if task is not None:
-                    if not (kwargs.get('run_immediate') or task.is_runnable(registry)):
-                        logger.debug("Skipping inactive task {0}".format(task.name))
-                        return
-                    set_local_registry(sm)
-                    tm = ITransactionManager(task)
-                    for attempt in tm.attempts():
-                        with attempt as t:
-                            start = datetime.utcnow()
-                            try:
-                                registry.notify(BeforeRunJobEvent(task))
-                                task.run(report)
-                                if report.getvalue():
-                                    status = 'OK'
-                                else:
-                                    status = 'Empty'
-                                report.write('\n\nTask duration: {0}'.format(get_duration(start, request=request)))
-                            except:
-                                status = 'Error'
-                                task._log_exception(report,
-                                                    "An error occurred during execution of task '{0}'".format(task.name))
-                            registry.notify(AfterRunJobEvent(task, status))
-                            task.store_report(report, status)
-                            task.send_report(report, status, registry)
-                        if t.status == 'Committed':
-                            break
+                request = check_request(registry=registry)
+                with RequestContext(request):
+                    application_name = registry.settings.get(PYAMS_APPLICATION_SETTINGS_KEY,
+                                                             PYAMS_APPLICATION_DEFAULT_NAME)
+                    sm = root.get(application_name).getSiteManager()
+                    scheduler_util = sm.get(SCHEDULER_NAME)
+                    task = scheduler_util.get(self.__name__)
+                    if task is not None:
+                        if not (kwargs.get('run_immediate') or task.is_runnable(registry)):
+                            logger.debug("Skipping inactive task {0}".format(task.name))
+                            return
+                        set_local_registry(sm)
+                        tm = ITransactionManager(task)
+                        for attempt in tm.attempts():
+                            with attempt as t:
+                                start = datetime.utcnow()
+                                try:
+                                    registry.notify(BeforeRunJobEvent(task))
+                                    task.run(report)
+                                    if report.getvalue():
+                                        status = 'OK'
+                                    else:
+                                        status = 'Empty'
+                                    report.write('\n\nTask duration: {0}'.format(get_duration(start,
+                                                                                              request=request)))
+                                except:
+                                    status = 'Error'
+                                    task._log_exception(report,
+                                                        "An error occurred during execution of "
+                                                        "task '{0}'".format(task.name))
+                                registry.notify(AfterRunJobEvent(task, status))
+                                task.store_report(report, status)
+                                task.send_report(report, status, registry)
+                            if t.status == 'Committed':
+                                break
             except:
                 self._log_exception(None, "Can't execute scheduled job {0}".format(self.name))
             tm = ITransactionManager(self, None)