Commit af0e5127 authored by Aurel's avatar Aurel

add parameter group_id to SQLDict, it's used to segregate activity

with the same group_method_id


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@14961 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 074f62fd
...@@ -122,6 +122,11 @@ class ActiveObject(ExtensionClass.Base): ...@@ -122,6 +122,11 @@ class ActiveObject(ExtensionClass.Base):
if k not in kw: if k not in kw:
kw[k] = v kw[k] = v
if kw.get('group_id', '') is None:
raise ValueError, "Cannot defined a group_id with value None"
elif kw.get('group_method_id') is None and kw.get('group_id') is not None:
raise ValueError, "Cannot defined a group_id without group_method_id"
activity_tool = getToolByName(self, 'portal_activities', None) activity_tool = getToolByName(self, 'portal_activities', None)
if activity_tool is None: return self # Do nothing if no portal_activities if activity_tool is None: return self # Do nothing if no portal_activities
# activate returns an ActiveWrapper # activate returns an ActiveWrapper
......
...@@ -42,7 +42,7 @@ try: ...@@ -42,7 +42,7 @@ try:
except ImportError: except ImportError:
pass pass
from zLOG import LOG, TRACE, WARNING, ERROR from zLOG import LOG, TRACE, WARNING, ERROR, INFO
MAX_PRIORITY = 5 MAX_PRIORITY = 5
MAX_GROUPED_OBJECTS = 500 MAX_GROUPED_OBJECTS = 500
...@@ -70,7 +70,8 @@ class SQLDict(RAMDict): ...@@ -70,7 +70,8 @@ class SQLDict(RAMDict):
broadcast = m.activity_kw.get('broadcast', 0), broadcast = m.activity_kw.get('broadcast', 0),
message = self.dumpMessage(m), message = self.dumpMessage(m),
date = m.activity_kw.get('at_date', DateTime()), date = m.activity_kw.get('at_date', DateTime()),
group_method_id = m.activity_kw.get('group_method_id', ''), group_method_id = '\0'.join([m.activity_kw.get('group_method_id', ''),
m.activity_kw.get('group_id', '')]),
tag = m.activity_kw.get('tag', ''), tag = m.activity_kw.get('tag', ''),
order_validation_text = self.getOrderValidationText(m)) order_validation_text = self.getOrderValidationText(m))
# Also store uid of activity # Also store uid of activity
...@@ -89,7 +90,8 @@ class SQLDict(RAMDict): ...@@ -89,7 +90,8 @@ class SQLDict(RAMDict):
dumped_message_list = [self.dumpMessage(message) for message in registered_message_list] dumped_message_list = [self.dumpMessage(message) for message in registered_message_list]
datetime = DateTime() datetime = DateTime()
date_list = [message.activity_kw.get('at_date', datetime) for message in registered_message_list] date_list = [message.activity_kw.get('at_date', datetime) for message in registered_message_list]
group_method_id_list = [message.activity_kw.get('group_method_id', '') for message in registered_message_list] group_method_id_list = ['\0'.join([message.activity_kw.get('group_method_id', ''), message.activity_kw.get('group_id', '')])
for message in registered_message_list]
tag_list = [message.activity_kw.get('tag', '') for message in registered_message_list] tag_list = [message.activity_kw.get('tag', '') for message in registered_message_list]
order_validation_text_list = [self.getOrderValidationText(message) for message in registered_message_list] order_validation_text_list = [self.getOrderValidationText(message) for message in registered_message_list]
uid_list = activity_tool.getPortalObject().portal_ids.generateNewLengthIdList(id_group='portal_activity', id_count=len(registered_message_list)) uid_list = activity_tool.getPortalObject().portal_ids.generateNewLengthIdList(id_group='portal_activity', id_count=len(registered_message_list))
...@@ -172,10 +174,12 @@ class SQLDict(RAMDict): ...@@ -172,10 +174,12 @@ class SQLDict(RAMDict):
line = result[0] line = result[0]
path = line.path path = line.path
method_id = line.method_id method_id = line.method_id
group_method_id = line.group_method_id
order_validation_text = line.order_validation_text order_validation_text = line.order_validation_text
uid_list = activity_tool.SQLDict_readUidList(path=path, method_id=method_id, uid_list = activity_tool.SQLDict_readUidList(path=path, method_id=method_id,
processing_node=None, to_date=now_date, processing_node=None, to_date=now_date,
order_validation_text=order_validation_text) order_validation_text=order_validation_text,
group_method_id=group_method_id)
uid_list = [x.uid for x in uid_list] uid_list = [x.uid for x in uid_list]
uid_list_list = [uid_list] uid_list_list = [uid_list]
priority_list = [line.priority] priority_list = [line.priority]
...@@ -196,15 +200,14 @@ class SQLDict(RAMDict): ...@@ -196,15 +200,14 @@ class SQLDict(RAMDict):
if not self.validateMessage(activity_tool, m, uid_list, line.priority, processing_node): if not self.validateMessage(activity_tool, m, uid_list, line.priority, processing_node):
return 0 return 0
group_method_id = m.activity_kw.get('group_method_id') if group_method_id not in (None, '', '\0'):
if group_method_id is not None:
# Count the number of objects to prevent too many objects. # Count the number of objects to prevent too many objects.
if m.hasExpandMethod(): if m.hasExpandMethod():
count = len(m.getObjectList(activity_tool)) count = len(m.getObjectList(activity_tool))
else: else:
count = 1 count = 1
group_method = activity_tool.getPortalObject().restrictedTraverse(group_method_id) group_method = activity_tool.getPortalObject().restrictedTraverse(group_method_id.split('\0')[0])
if count < MAX_GROUPED_OBJECTS: if count < MAX_GROUPED_OBJECTS:
# Retrieve objects which have the same group method. # Retrieve objects which have the same group method.
...@@ -255,12 +258,15 @@ class SQLDict(RAMDict): ...@@ -255,12 +258,15 @@ class SQLDict(RAMDict):
# Release locks before starting a potentially long calculation # Release locks before starting a potentially long calculation
get_transaction().commit() get_transaction().commit()
# Try to invoke # Remove group_id parameter from group_method_id
if group_method_id is not None: if group_method_id is not None:
LOG('SQLDict', TRACE, group_method_id = group_method_id.split('\0')[0]
# Try to invoke
if group_method_id not in (None, ""):
LOG('SQLDict', INFO,
'invoking a group method %s with %d objects '\ 'invoking a group method %s with %d objects '\
' (%d objects in expanded form)' % ( ' (%d objects in expanded form)' % (
group_method_id, len(message_list), count)) group_method_id, len(message_list), count))
activity_tool.invokeGroup(group_method_id, message_list) activity_tool.invokeGroup(group_method_id, message_list)
else: else:
activity_tool.invoke(message_list[0]) activity_tool.invoke(message_list[0])
...@@ -304,7 +310,7 @@ class SQLDict(RAMDict): ...@@ -304,7 +310,7 @@ class SQLDict(RAMDict):
error=sys.exc_info()) error=sys.exc_info())
raise raise
return 0 return 0
try: try:
for i in xrange(len(message_list)): for i in xrange(len(message_list)):
m = message_list[i] m = message_list[i]
...@@ -501,7 +507,8 @@ class SQLDict(RAMDict): ...@@ -501,7 +507,8 @@ class SQLDict(RAMDict):
broadcast_list = [1] * (node_count - 1) broadcast_list = [1] * (node_count - 1)
message_list = [self.dumpMessage(message)] * (node_count - 1) message_list = [self.dumpMessage(message)] * (node_count - 1)
date_list = [message.activity_kw.get('at_date', now_date)] * (node_count - 1) date_list = [message.activity_kw.get('at_date', now_date)] * (node_count - 1)
group_method_id_list = [message.activity_kw.get('group_method_id', '')] * (node_count - 1) group_method_id_list = ['\0'.join([message.activity_kw.get('group_method_id', ''),
message.activity_kw.get('group_id', '')])] * (node_count - 1)
tag_list = [message.activity_kw.get('tag', '')] * (node_count - 1) tag_list = [message.activity_kw.get('tag', '')] * (node_count - 1)
order_validation_text_list = [message.order_validation_text] * (node_count - 1) order_validation_text_list = [message.order_validation_text] * (node_count - 1)
activity_tool.SQLDict_writeMessageList(uid_list=uid_list, activity_tool.SQLDict_writeMessageList(uid_list=uid_list,
......
...@@ -11,7 +11,8 @@ class_file: ...@@ -11,7 +11,8 @@ class_file:
method_id method_id
path path
to_date to_date
order_validation_text</params> order_validation_text
group_method_id</params>
SELECT uid FROM SELECT uid FROM
message message
WHERE WHERE
...@@ -21,3 +22,4 @@ WHERE ...@@ -21,3 +22,4 @@ WHERE
<dtml-if path> AND path = <dtml-sqlvar path type="string"> </dtml-if> <dtml-if path> AND path = <dtml-sqlvar path type="string"> </dtml-if>
<dtml-if to_date> AND date <= <dtml-sqlvar to_date type="datetime"> </dtml-if> <dtml-if to_date> AND date <= <dtml-sqlvar to_date type="datetime"> </dtml-if>
<dtml-if order_validation_text> AND order_validation_text = <dtml-sqlvar order_validation_text type="string"> </dtml-if> <dtml-if order_validation_text> AND order_validation_text = <dtml-sqlvar order_validation_text type="string"> </dtml-if>
<dtml-if group_method_id> AND group_method_id = <dtml-sqlvar group_method_id type="string"> </dtml-if>
\ No newline at end of file
...@@ -1794,6 +1794,80 @@ class TestCMFActivity(ERP5TypeTestCase): ...@@ -1794,6 +1794,80 @@ class TestCMFActivity(ERP5TypeTestCase):
abortTransactionSynchronously() abortTransactionSynchronously()
getattr(organisation, 'uid') getattr(organisation, 'uid')
def test_80_CallWithGroupIdParamater(self, quiet=0, run=run_all_test):
"""
Test that group_id parameter is used to separate execution of the same method
"""
if not run: return
if not quiet:
message = '\nTest Activity with group_id parameter'
ZopeTestCase._print(message)
LOG('Testing... ',0,message)
portal = self.getPortal()
organisation = portal.organisation._getOb(self.company_id)
# Defined a group method
def setFoobar(self, object_list, number=1):
for obj in object_list:
if getattr(obj,'foobar', None) is not None:
obj.foobar = obj.foobar + number
else:
obj.foobar = number
object_list[:] = []
from Products.ERP5Type.Document.Folder import Folder
Folder.setFoobar = setFoobar
def getFoobar(self):
return (getattr(self,'foobar',0))
Organisation.getFoobar = getFoobar
organisation.foobar = 0
self.assertEquals(0,organisation.getFoobar())
# Test group_method_id is working without group_id
for x in xrange(5):
organisation.activate(activity='SQLDict', group_method_id="organisation_module/setFoobar").reindexObject(number=1)
get_transaction().commit()
message_list = portal.portal_activities.getMessageList()
self.assertEquals(len(message_list),5)
portal.portal_activities.distribute()
portal.portal_activities.tic()
self.assertEquals(1, organisation.getFoobar())
# Test group_method_id is working with one group_id defined
for x in xrange(5):
organisation.activate(activity='SQLDict', group_method_id="organisation_module/setFoobar", group_id="1").reindexObject(number=1)
get_transaction().commit()
message_list = portal.portal_activities.getMessageList()
self.assertEquals(len(message_list),5)
portal.portal_activities.distribute()
portal.portal_activities.tic()
self.assertEquals(2, organisation.getFoobar())
# Test group_method_id is working with many group_id defined
for x in xrange(5):
organisation.activate(activity='SQLDict', group_method_id="organisation_module/setFoobar", group_id="1").reindexObject(number=1)
get_transaction().commit()
organisation.activate(activity='SQLDict', group_method_id="organisation_module/setFoobar", group_id="2").reindexObject(number=3)
get_transaction().commit()
organisation.activate(activity='SQLDict', group_method_id="organisation_module/setFoobar", group_id="1").reindexObject(number=1)
get_transaction().commit()
organisation.activate(activity='SQLDict', group_method_id="organisation_module/setFoobar", group_id="3").reindexObject(number=5)
get_transaction().commit()
message_list = portal.portal_activities.getMessageList()
self.assertEquals(len(message_list),20)
portal.portal_activities.distribute()
portal.portal_activities.tic()
self.assertEquals(11, organisation.getFoobar())
message_list = portal.portal_activities.getMessageList()
self.assertEquals(len(message_list), 0)
if __name__ == '__main__': if __name__ == '__main__':
framework() framework()
else: else:
......
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