--- a/src/pyams_scheduler/task.py Thu Jan 10 17:14:14 2019 +0100
+++ b/src/pyams_scheduler/task.py Thu Jan 10 17:18:19 2019 +0100
@@ -12,47 +12,42 @@
__docformat__ = 'restructuredtext'
-
-# import standard library
import logging
-logger = logging.getLogger('PyAMS (scheduler)')
-
import traceback
-
from datetime import datetime, timedelta
from io import StringIO
-# import interfaces
-from pyams_scheduler.interfaces import IScheduler, ITask, ITaskInfo, ITaskHistory, ITaskHistoryContainer, \
- ITaskSchedulingMode, SCHEDULER_NAME, SCHEDULER_HANDLER_KEY, SCHEDULER_AUTH_KEY, AfterRunJobEvent, BeforeRunJobEvent
-from pyams_utils.interfaces import PYAMS_APPLICATION_DEFAULT_NAME, PYAMS_APPLICATION_SETTINGS_KEY
+import transaction
+from apscheduler.triggers.base import BaseTrigger
+from persistent import Persistent
+from pyramid.events import subscriber
+from pyramid.threadlocal import RequestContext
from pyramid_mailer.interfaces import IMailer
+from pyramid_mailer.message import Message
from transaction.interfaces import ITransactionManager
from zope.component.interfaces import ISite
+from zope.container.contained import Contained
+from zope.container.folder import Folder
+from zope.interface import alsoProvides, implementer, noLongerProvides
from zope.interface.interfaces import ComponentLookupError
from zope.intid.interfaces import IIntIds
-from zope.lifecycleevent.interfaces import IObjectRemovedEvent, IObjectAddedEvent, IObjectModifiedEvent
-
-# import packages
-import transaction
+from zope.lifecycleevent.interfaces import IObjectAddedEvent, IObjectModifiedEvent, IObjectRemovedEvent
+from zope.location import locate
+from zope.schema.fieldproperty import FieldProperty
-from apscheduler.triggers.base import BaseTrigger
-from persistent import Persistent
+from pyams_scheduler.interfaces import AfterRunJobEvent, BeforeRunJobEvent, IScheduler, ITask, ITaskHistory, \
+ ITaskHistoryContainer, ITaskInfo, ITaskSchedulingMode, SCHEDULER_AUTH_KEY, SCHEDULER_HANDLER_KEY, SCHEDULER_NAME
from pyams_utils.date import get_duration
-from pyams_utils.registry import query_utility, get_utility, set_local_registry, get_global_registry
+from pyams_utils.interfaces import PYAMS_APPLICATION_DEFAULT_NAME, PYAMS_APPLICATION_SETTINGS_KEY
+from pyams_utils.registry import get_utility, query_utility, set_local_registry
from pyams_utils.request import check_request
from pyams_utils.timezone import tztime
from pyams_utils.traversing import get_parent
from pyams_utils.zodb import ZODBConnection
-from pyams_zmq.socket import zmq_socket, zmq_response
-from pyramid.events import subscriber
-from pyramid.threadlocal import RequestContext
-from pyramid_mailer.message import Message
-from zope.container.contained import Contained
-from zope.container.folder import Folder
-from zope.interface import implementer, alsoProvides, noLongerProvides
-from zope.location import locate
-from zope.schema.fieldproperty import FieldProperty
+from pyams_zmq.socket import zmq_response, zmq_socket
+
+
+logger = logging.getLogger('PyAMS (scheduler)')
class ImmediateTaskTrigger(BaseTrigger):
@@ -110,6 +105,7 @@
_history_length = FieldProperty(ITask['history_length'])
settings_view_name = FieldProperty(ITask['settings_view_name'])
+ principal_id = None
_internal_id = None
@@ -247,18 +243,18 @@
with zodb_connection as root:
try:
registry = kwargs.get('registry')
- 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:
+ 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:
+ set_local_registry(sm)
+ request = check_request(registry=registry, principal_id=self.principal_id)
+ with RequestContext(request):
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:
@@ -323,7 +319,7 @@
def send_report(self, report, status, registry):
try:
mailer_name = self.__parent__.report_mailer
- except (TypeError, ComponentLookupError):
+ except (TypeError, AttributeError, ComponentLookupError):
return
if ((status == 'Empty') and not self.send_empty_reports) or \
((status == 'OK') and self.report_errors_only):