Commit b54a8418 authored by Julien Muchembled's avatar Julien Muchembled

Some cleanup in SQLDict and SQLQueue

Pass list of Message objects instead of a list of (uid, message, priority).
Store the fetched line on the Message object to retrieve the priority.

In the future, if we allow executed activity to access its related Message
object, it could also get the SQL line.

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@32875 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent d797bbce
This diff is collapsed.
...@@ -162,10 +162,7 @@ class SQLQueue(RAMQueue, SQLBase): ...@@ -162,10 +162,7 @@ class SQLQueue(RAMQueue, SQLBase):
unclean state. unclean state.
Returned values: Returned values:
list of 3-tuple: list of messages
- message uid
- message
- priority
""" """
def getReservedMessageList(limit): def getReservedMessageList(limit):
line_list = self.getReservedMessageList(activity_tool=activity_tool, line_list = self.getReservedMessageList(activity_tool=activity_tool,
...@@ -179,21 +176,18 @@ class SQLQueue(RAMQueue, SQLBase): ...@@ -179,21 +176,18 @@ class SQLQueue(RAMQueue, SQLBase):
self.makeMessageListAvailable(activity_tool=activity_tool, uid_list=uid_list) self.makeMessageListAvailable(activity_tool=activity_tool, uid_list=uid_list)
now_date = self.getNow(activity_tool) now_date = self.getNow(activity_tool)
message_list = [] message_list = []
def append(line, message):
uid = line.uid
message_list.append((uid, message, line.priority))
try: try:
result = getReservedMessageList(limit=MESSAGE_BUNDLE_SIZE) result = getReservedMessageList(limit=MESSAGE_BUNDLE_SIZE)
for line in result: for line in result:
m = self.loadMessage(line.message, uid=line.uid) m = self.loadMessage(line.message, uid=line.uid, line=line)
append(line, m) message_list.append(m)
if len(message_list): if len(message_list):
activity_tool.SQLQueue_processMessage(uid=[x[0] for x in message_list]) activity_tool.SQLQueue_processMessage(uid=[m.uid for x in message_list])
return message_list return message_list
except: except:
LOG('SQLQueue', WARNING, 'Exception while reserving messages.', error=sys.exc_info()) LOG('SQLQueue', WARNING, 'Exception while reserving messages.', error=sys.exc_info())
if len(message_list): if len(message_list):
to_free_uid_list = [x[0] for x in message_list] to_free_uid_list = [m.uid for m in message_list]
try: try:
makeMessageListAvailable(to_free_uid_list) makeMessageListAvailable(to_free_uid_list)
except: except:
...@@ -205,7 +199,7 @@ class SQLQueue(RAMQueue, SQLBase): ...@@ -205,7 +199,7 @@ class SQLQueue(RAMQueue, SQLBase):
LOG('SQLQueue', TRACE, '(no message was reserved)') LOG('SQLQueue', TRACE, '(no message was reserved)')
return [] return []
def finalizeMessageExecution(self, activity_tool, message_uid_priority_list): def finalizeMessageExecution(self, activity_tool, message_list):
def makeMessageListAvailable(uid_list): def makeMessageListAvailable(uid_list):
self.makeMessageListAvailable(activity_tool=activity_tool, uid_list=uid_list) self.makeMessageListAvailable(activity_tool=activity_tool, uid_list=uid_list)
deletable_uid_list = [] deletable_uid_list = []
...@@ -213,10 +207,12 @@ class SQLQueue(RAMQueue, SQLBase): ...@@ -213,10 +207,12 @@ class SQLQueue(RAMQueue, SQLBase):
final_error_uid_list = [] final_error_uid_list = []
notify_user_list = [] notify_user_list = []
non_executable_message_list = [] non_executable_message_list = []
for uid, m, priority in message_uid_priority_list: for m in message_list:
uid = m.uid
if m.getExecutionState() == MESSAGE_EXECUTED: if m.getExecutionState() == MESSAGE_EXECUTED:
deletable_uid_list.append(uid) deletable_uid_list.append(uid)
elif m.getExecutionState() == MESSAGE_NOT_EXECUTED: elif m.getExecutionState() == MESSAGE_NOT_EXECUTED:
priority = m.line.priority
# BACK: Only exceptions can be classes in Python 2.6. # BACK: Only exceptions can be classes in Python 2.6.
# Once we drop support for Python 2.4, # Once we drop support for Python 2.4,
# please, remove the "type(m.exc_type) is type(ConflictError)" check # please, remove the "type(m.exc_type) is type(ConflictError)" check
...@@ -288,11 +284,11 @@ class SQLQueue(RAMQueue, SQLBase): ...@@ -288,11 +284,11 @@ class SQLQueue(RAMQueue, SQLBase):
def dequeueMessage(self, activity_tool, processing_node): def dequeueMessage(self, activity_tool, processing_node):
def makeMessageListAvailable(uid_list): def makeMessageListAvailable(uid_list):
self.makeMessageListAvailable(activity_tool=activity_tool, uid_list=uid_list) self.makeMessageListAvailable(activity_tool=activity_tool, uid_list=uid_list)
message_uid_priority_list = \ message_list = \
self.getProcessableMessageList(activity_tool, processing_node) self.getProcessableMessageList(activity_tool, processing_node)
if len(message_uid_priority_list): if message_list:
processing_stop_time = time() + 30 # Stop processing after more than 10 seconds were spent processing_stop_time = time() + 30 # Stop processing after more than 10 seconds were spent
processed_message_uid_list = [] processed_count = 0
# Commit right before executing messages. # Commit right before executing messages.
# As MySQL transaction does not start exactly at the same time as ZODB # As MySQL transaction does not start exactly at the same time as ZODB
# transactions but a bit later, messages available might be called # transactions but a bit later, messages available might be called
...@@ -301,17 +297,17 @@ class SQLQueue(RAMQueue, SQLBase): ...@@ -301,17 +297,17 @@ class SQLQueue(RAMQueue, SQLBase):
# So all connectors must be committed now that we have selected # So all connectors must be committed now that we have selected
# everything needed from MySQL to get a fresh view of ZODB objects. # everything needed from MySQL to get a fresh view of ZODB objects.
get_transaction().commit() get_transaction().commit()
for value in message_uid_priority_list: for m in message_list:
processed_count += 1
clearActivityRuntimeEnvironment() clearActivityRuntimeEnvironment()
updateActivityRuntimeValue({'processing_node': processing_node, updateActivityRuntimeValue({'processing_node': processing_node,
'activity_kw': value[1].activity_kw, 'activity_kw': m.activity_kw,
'priority': value[2], 'priority': m.line.priority,
'uid': value[0]}) 'uid': m.uid})
processed_message_uid_list.append(value)
# Try to invoke # Try to invoke
try: try:
activity_tool.invoke(value[1]) activity_tool.invoke(m)
if value[1].getExecutionState() != MESSAGE_NOT_EXECUTED: if m.getExecutionState() != MESSAGE_NOT_EXECUTED:
# Commit so that if a message raises it doesn't causes previous # Commit so that if a message raises it doesn't causes previous
# successfull messages to be rolled back. This commit might fail, # successfull messages to be rolled back. This commit might fail,
# so it is protected the same way as activity execution by the # so it is protected the same way as activity execution by the
...@@ -321,7 +317,8 @@ class SQLQueue(RAMQueue, SQLBase): ...@@ -321,7 +317,8 @@ class SQLQueue(RAMQueue, SQLBase):
# This message failed, revert. # This message failed, revert.
abortTransactionSynchronously() abortTransactionSynchronously()
except: except:
LOG('SQLQueue', WARNING, 'Exception raised when invoking message (uid, path, method_id) %r' % ((value[0], value[1].object_path, value[1].method_id), ), error=sys.exc_info()) value = m.uid, m.object_path, m.method_id
LOG('SQLQueue', WARNING, 'Exception raised when invoking message (uid, path, method_id) %r' % (value, ), error=sys.exc_info())
try: try:
abortTransactionSynchronously() abortTransactionSynchronously()
except: except:
...@@ -331,11 +328,11 @@ class SQLQueue(RAMQueue, SQLBase): ...@@ -331,11 +328,11 @@ class SQLQueue(RAMQueue, SQLBase):
# We must make sure that the message is not set as executed. # We must make sure that the message is not set as executed.
# It is possible that the message is executed but the commit # It is possible that the message is executed but the commit
# of the transaction fails # of the transaction fails
value[1].setExecutionState(MESSAGE_NOT_EXECUTED, context=activity_tool) m.setExecutionState(MESSAGE_NOT_EXECUTED, context=activity_tool)
# XXX Is it still useful to free message now that this node is able # XXX Is it still useful to free message now that this node is able
# to reselect it ? # to reselect it ?
try: try:
makeMessageListAvailable([value[0]]) makeMessageListAvailable([m.uid])
except: except:
LOG('SQLQueue', ERROR, 'Failed to free message: %r' % (value, ), error=sys.exc_info()) LOG('SQLQueue', ERROR, 'Failed to free message: %r' % (value, ), error=sys.exc_info())
else: else:
...@@ -344,18 +341,18 @@ class SQLQueue(RAMQueue, SQLBase): ...@@ -344,18 +341,18 @@ class SQLQueue(RAMQueue, SQLBase):
LOG('SQLQueue', TRACE, 'Stop processing message batch because processing delay exceeded') LOG('SQLQueue', TRACE, 'Stop processing message batch because processing delay exceeded')
break break
# Release all unprocessed messages # Release all unprocessed messages
processed_uid_set = ImmutableSet([x[0] for x in processed_message_uid_list]) to_free_uid_list = [m.uid for m in message_list[processed_count:]]
to_free_uid_list = [x[0] for x in message_uid_priority_list if x[0] not in processed_uid_set] if to_free_uid_list:
if len(to_free_uid_list):
try: try:
makeMessageListAvailable(to_free_uid_list) makeMessageListAvailable(to_free_uid_list)
except: except:
LOG('SQLQueue', ERROR, 'Failed to free remaining messages: %r' % (to_free_uid_list, ), error=sys.exc_info()) LOG('SQLQueue', ERROR, 'Failed to free remaining messages: %r' % (to_free_uid_list, ), error=sys.exc_info())
else: else:
LOG('SQLQueue', TRACE, 'Freed messages %r' % (to_free_uid_list, )) LOG('SQLQueue', TRACE, 'Freed messages %r' % (to_free_uid_list, ))
self.finalizeMessageExecution(activity_tool, processed_message_uid_list) self.finalizeMessageExecution(activity_tool,
message_list[:processed_count])
get_transaction().commit() get_transaction().commit()
return not len(message_uid_priority_list) return not message_list
def hasActivity(self, activity_tool, object, method_id=None, only_valid=None, active_process_uid=None): def hasActivity(self, activity_tool, object, method_id=None, only_valid=None, active_process_uid=None):
...@@ -411,7 +408,7 @@ class SQLQueue(RAMQueue, SQLBase): ...@@ -411,7 +408,7 @@ class SQLQueue(RAMQueue, SQLBase):
for line in result: for line in result:
path = line.path path = line.path
method_id = line.method_id method_id = line.method_id
m = self.loadMessage(line.message, uid = line.uid) m = self.loadMessage(line.message, uid=line.uid, line=line)
if invoke: if invoke:
# First Validate (only if message is marked as new) # First Validate (only if message is marked as new)
if line.processing_node == -1: if line.processing_node == -1:
...@@ -467,7 +464,7 @@ class SQLQueue(RAMQueue, SQLBase): ...@@ -467,7 +464,7 @@ class SQLQueue(RAMQueue, SQLBase):
if dumpMessageList is not None: if dumpMessageList is not None:
result = dumpMessageList() result = dumpMessageList()
for line in result: for line in result:
m = self.loadMessage(line.message, uid = line.uid) m = self.loadMessage(line.message, uid=line.uid, line=line)
message_list.append(m) message_list.append(m)
return message_list return message_list
......
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