Commit 248c3a2b authored by Vincent Pelletier's avatar Vincent Pelletier

ActivityRuntimeEnvironment: Become a context manager.

Avoids logic duplication on how to store the environment.
Avoids relying on ERP5 products (for whatever it's worth anyway).
parent 4985f1ae
...@@ -36,7 +36,7 @@ from ZODB.POSException import ConflictError ...@@ -36,7 +36,7 @@ from ZODB.POSException import ConflictError
from Products.CMFActivity.ActivityTool import ( from Products.CMFActivity.ActivityTool import (
Message, MESSAGE_NOT_EXECUTED, MESSAGE_EXECUTED, SkippedMessage) Message, MESSAGE_NOT_EXECUTED, MESSAGE_EXECUTED, SkippedMessage)
from Products.CMFActivity.ActivityRuntimeEnvironment import ( from Products.CMFActivity.ActivityRuntimeEnvironment import (
DEFAULT_MAX_RETRY, ActivityRuntimeEnvironment, getTransactionalVariable) DEFAULT_MAX_RETRY, ActivityRuntimeEnvironment)
from Queue import Queue, VALIDATION_ERROR_DELAY, VALID, INVALID_PATH from Queue import Queue, VALIDATION_ERROR_DELAY, VALID, INVALID_PATH
from Products.CMFActivity.Errors import ActivityFlushError from Products.CMFActivity.Errors import ActivityFlushError
...@@ -508,11 +508,10 @@ class SQLBase(Queue): ...@@ -508,11 +508,10 @@ class SQLBase(Queue):
# everything needed from MySQL to get a fresh view of ZODB objects. # everything needed from MySQL to get a fresh view of ZODB objects.
transaction.commit() transaction.commit()
transaction.begin() transaction.begin()
tv = getTransactionalVariable()
tv['activity_runtime_environment'] = activity_runtime_environment
# Try to invoke # Try to invoke
try: try:
method(*args) with activity_runtime_environment:
method(*args)
# Abort if at least 1 message failed. On next tic, only those that # Abort if at least 1 message failed. On next tic, only those that
# succeeded will be selected because their at_date won't have been # succeeded will be selected because their at_date won't have been
# increased. # increased.
...@@ -533,8 +532,8 @@ class SQLBase(Queue): ...@@ -533,8 +532,8 @@ class SQLBase(Queue):
if exc_info: if exc_info:
try: try:
# Register it again. # Register it again.
tv['activity_runtime_environment'] = activity_runtime_environment with activity_runtime_environment:
cancel = message.on_error_callback(*exc_info) cancel = message.on_error_callback(*exc_info)
del exc_info, message.exc_info del exc_info, message.exc_info
transaction.commit() transaction.commit()
if cancel: if cancel:
......
from Products.ERP5Type.TransactionalVariable import getTransactionalVariable from threading import local
from AccessControl import ClassSecurityInfo from AccessControl import ClassSecurityInfo
from Products.ERP5Type.Globals import InitializeClass from Products.ERP5Type.Globals import InitializeClass
DEFAULT_MAX_RETRY = 3 DEFAULT_MAX_RETRY = 3
_activity_runtime_environment = local()
def getActivityRuntimeEnvironment(): def getActivityRuntimeEnvironment():
""" """
Raises KeyError if called outside activity. Raises KeyError if called outside activity.
""" """
return getTransactionalVariable()['activity_runtime_environment'] try:
return _activity_runtime_environment.value
except AttributeError:
raise KeyError
def _getActivityRuntimeEnvironment(): def _getActivityRuntimeEnvironment():
try: return getattr(_activity_runtime_environment, 'value', None)
return getActivityRuntimeEnvironment()
except KeyError:
return
class BaseMessage: class BaseMessage:
...@@ -43,6 +44,14 @@ class ActivityRuntimeEnvironment(object): ...@@ -43,6 +44,14 @@ class ActivityRuntimeEnvironment(object):
def __init__(self, message): def __init__(self, message):
self._message = message self._message = message
def __enter__(self):
assert not hasattr(_activity_runtime_environment, 'value')
_activity_runtime_environment.value = self
def __exit__(self, exc_type, exc_val, exc_tb):
assert _activity_runtime_environment.value is self
del _activity_runtime_environment.value
security.declarePublic('getTag') security.declarePublic('getTag')
def getTag(self, default=None): def getTag(self, default=None):
return self._message.activity_kw.get('tag', default) return self._message.activity_kw.get('tag', default)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment