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
from Products.CMFActivity.ActivityTool import (
Message, MESSAGE_NOT_EXECUTED, MESSAGE_EXECUTED, SkippedMessage)
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 Products.CMFActivity.Errors import ActivityFlushError
......@@ -508,11 +508,10 @@ class SQLBase(Queue):
# everything needed from MySQL to get a fresh view of ZODB objects.
transaction.commit()
transaction.begin()
tv = getTransactionalVariable()
tv['activity_runtime_environment'] = activity_runtime_environment
# Try to invoke
try:
method(*args)
with activity_runtime_environment:
method(*args)
# 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
# increased.
......@@ -533,8 +532,8 @@ class SQLBase(Queue):
if exc_info:
try:
# Register it again.
tv['activity_runtime_environment'] = activity_runtime_environment
cancel = message.on_error_callback(*exc_info)
with activity_runtime_environment:
cancel = message.on_error_callback(*exc_info)
del exc_info, message.exc_info
transaction.commit()
if cancel:
......
from Products.ERP5Type.TransactionalVariable import getTransactionalVariable
from threading import local
from AccessControl import ClassSecurityInfo
from Products.ERP5Type.Globals import InitializeClass
DEFAULT_MAX_RETRY = 3
_activity_runtime_environment = local()
def getActivityRuntimeEnvironment():
"""
Raises KeyError if called outside activity.
"""
return getTransactionalVariable()['activity_runtime_environment']
try:
return _activity_runtime_environment.value
except AttributeError:
raise KeyError
def _getActivityRuntimeEnvironment():
try:
return getActivityRuntimeEnvironment()
except KeyError:
return
return getattr(_activity_runtime_environment, 'value', None)
class BaseMessage:
......@@ -43,6 +44,14 @@ class ActivityRuntimeEnvironment(object):
def __init__(self, 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')
def getTag(self, default=None):
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