ActivityTool.py 57.8 KB
Newer Older
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1 2 3
##############################################################################
#
# Copyright (c) 2002 Nexedi SARL and Contributors. All Rights Reserved.
Jean-Paul Smets's avatar
Jean-Paul Smets committed
4
#                    Jean-Paul Smets-Solanes <jp@nexedi.com>
Jean-Paul Smets's avatar
Jean-Paul Smets committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################

29 30 31 32
import socket
import urllib
import threading
import sys
Vincent Pelletier's avatar
Vincent Pelletier committed
33
from types import StringType
34 35
import re

36
from Products.CMFCore import permissions as CMFCorePermissions
Jean-Paul Smets's avatar
Jean-Paul Smets committed
37
from Products.ERP5Type.Core.Folder import Folder
38
from Products.CMFActivity.ActiveResult import ActiveResult
39
from Products.CMFActivity.ActiveObject import DEFAULT_ACTIVITY
40
from Products.PythonScripts.Utility import allow_class
41
from AccessControl import ClassSecurityInfo, Permissions
Jérome Perrin's avatar
Jérome Perrin committed
42 43 44 45
from AccessControl.SecurityManagement import newSecurityManager
from AccessControl.SecurityManagement import noSecurityManager
from AccessControl.SecurityManagement import setSecurityManager
from AccessControl.SecurityManagement import getSecurityManager
46
from Products.CMFCore.utils import UniqueObject, _getAuthenticatedUser, getToolByName
47
from Products.ERP5Type.Globals import InitializeClass, DTMLFile
Jean-Paul Smets's avatar
Jean-Paul Smets committed
48
from Acquisition import aq_base
49
from Acquisition import aq_inner
50
from ActivityBuffer import ActivityBuffer
51
from ActivityRuntimeEnvironment import BaseMessage
52
from zExceptions import ExceptionFormatter
53
from BTrees.OIBTree import OIBTree
54 55 56 57 58 59 60 61 62 63 64 65 66

try:
  from Products import iHotfix
  localizer_lock = iHotfix._the_lock
  localizer_contexts = iHotfix.contexts
  LocalizerContext = iHotfix.Context
except ImportError:
  # Localizer 1.2 includes iHotFix patches
  import Products.Localizer.patches
  localizer_lock = Products.Localizer.patches._requests_lock
  localizer_contexts = Products.Localizer.patches._requests
  LocalizerContext = lambda request: request

67

68
from ZODB.POSException import ConflictError
69
from Products.MailHost.MailHost import MailHostError
Jean-Paul Smets's avatar
Jean-Paul Smets committed
70

71
from zLOG import LOG, INFO, WARNING, ERROR
72
from warnings import warn
73
from time import time
74 75

try:
76
  from Products.TimerService import getTimerService
77
except ImportError:
78 79
  def getTimerService(self):
    pass
Jean-Paul Smets's avatar
Jean-Paul Smets committed
80

81 82 83 84 85
try:
  from traceback import format_list, extract_stack
except ImportError:
  format_list = extract_stack = None

86
# minimal IP:Port regexp
87
NODE_RE = re.compile('^\d+\.\d+\.\d+\.\d+:\d+$')
88

Jean-Paul Smets's avatar
Jean-Paul Smets committed
89 90 91 92
# Using a RAM property (not a property of an instance) allows
# to prevent from storing a state in the ZODB (and allows to restart...)
active_threads = 0
max_active_threads = 1 # 2 will cause more bug to appear (he he)
Vincent Pelletier's avatar
Vincent Pelletier committed
93
is_initialized = False
94 95
tic_lock = threading.Lock() # A RAM based lock to prevent too many concurrent tic() calls
timerservice_lock = threading.Lock() # A RAM based lock to prevent TimerService spamming when busy
96
is_running_lock = threading.Lock()
97 98 99
currentNode = None
ROLE_IDLE = 0
ROLE_PROCESSING = 1
Jean-Paul Smets's avatar
Jean-Paul Smets committed
100 101 102 103

# Activity Registration
activity_dict = {}

104 105 106 107 108
# Logging channel definitions
import logging
# Main logging channel
activity_logger = logging.getLogger('CMFActivity')
# Some logging subchannels
109
activity_tracking_logger = logging.getLogger('Tracking')
110
activity_timing_logger = logging.getLogger('CMFActivity.TimingLog')
111 112 113 114 115 116 117 118 119 120

# Direct logging to "[instancehome]/log/CMFActivity.log", if this directory exists.
# Otherwise, it will end up in root logging facility (ie, event.log).
from App.config import getConfiguration
import os
instancehome = getConfiguration().instancehome
if instancehome is not None:
  log_directory = os.path.join(instancehome, 'log')
  if os.path.isdir(log_directory):
    from Signals import Signals
121 122
    from ZConfig.components.logger.loghandler import FileHandler
    log_file_handler = FileHandler(os.path.join(log_directory, 'CMFActivity.log'))
123 124 125 126 127 128 129 130
    # Default zope log format string borrowed from
    # ZConfig/components/logger/factory.xml, but without the extra "------"
    # line separating entries.
    log_file_handler.setFormatter(logging.Formatter("%(asctime)s %(levelname)s %(name)s %(message)s", "%Y-%m-%dT%H:%M:%S"))
    Signals.registerZopeSignals([log_file_handler])
    activity_logger.addHandler(log_file_handler)
    activity_logger.propagate = 0

131 132 133 134 135 136 137 138
def activity_timing_method(method, args, kw):
  begin = time()
  try:
    return method(*args, **kw)
  finally:
    end = time()
    activity_timing_logger.info('%.02fs: %r(*%r, **%r)' % (end - begin, method, args, kw))

139 140 141 142 143 144 145
# Here go ActivityBuffer instances
# Structure:
#  global_activity_buffer[activity_tool_path][thread_id] = ActivityBuffer
global_activity_buffer = {}
from thread import get_ident, allocate_lock
global_activity_buffer_lock = allocate_lock()

Jean-Paul Smets's avatar
Jean-Paul Smets committed
146 147 148
def registerActivity(activity):
  # Must be rewritten to register
  # class and create instance for each activity
149
  #LOG('Init Activity', 0, str(activity.__name__))
Jean-Paul Smets's avatar
Jean-Paul Smets committed
150 151 152
  activity_instance = activity()
  activity_dict[activity.__name__] = activity_instance

153 154 155 156
MESSAGE_NOT_EXECUTED = 0
MESSAGE_EXECUTED = 1
MESSAGE_NOT_EXECUTABLE = 2

157 158

class Message(BaseMessage):
159
  """Activity Message Class.
160

161 162
  Message instances are stored in an activity queue, inside the Activity Tool.
  """
163

164
  active_process = None
165 166
  active_process_uid = None

167 168
  def __init__(self, obj, active_process, activity_kw, method_id, args, kw):
    if isinstance(obj, str):
169
      self.object_path = tuple(obj.split('/'))
170
      activity_creation_trace = False
Jean-Paul Smets's avatar
Jean-Paul Smets committed
171
    else:
172
      self.object_path = obj.getPhysicalPath()
173
      activity_creation_trace = obj.getPortalObject().portal_activities.activity_creation_trace
174
    if active_process is not None:
175
      self.active_process = active_process.getPhysicalPath()
176
      self.active_process_uid = active_process.getUid()
177 178 179
    if activity_kw.get('serialization_tag', False) is None:
      # Remove serialization_tag if it's None.
      del activity_kw['serialization_tag']
Jean-Paul Smets's avatar
Jean-Paul Smets committed
180 181 182 183
    self.activity_kw = activity_kw
    self.method_id = method_id
    self.args = args
    self.kw = kw
184
    self.is_executed = MESSAGE_NOT_EXECUTED
Vincent Pelletier's avatar
Vincent Pelletier committed
185 186 187
    self.exc_type = None
    self.exc_value = None
    self.traceback = None
188
    if activity_creation_trace and format_list is not None:
189 190 191 192
      # Save current traceback, to make it possible to tell where a message
      # was generated.
      # Strip last stack entry, since it will always be the same.
      self.call_traceback = ''.join(format_list(extract_stack()[:-1]))
193 194
    else:
      self.call_traceback = None
195
    self.processing = None
196
    self.user_name = str(_getAuthenticatedUser(self))
197
    # Store REQUEST Info
198
    self.request_info = {}
199 200
    request = getattr(obj, 'REQUEST', None)
    if request is not None:
201 202 203 204 205 206 207 208 209
      if 'SERVER_URL' in request.other:
        self.request_info['SERVER_URL'] = request.other['SERVER_URL']
      if 'VirtualRootPhysicalPath' in request.other:
        self.request_info['VirtualRootPhysicalPath'] = \
          request.other['VirtualRootPhysicalPath']
      if 'HTTP_ACCEPT_LANGUAGE' in request.environ:
        self.request_info['HTTP_ACCEPT_LANGUAGE'] = \
          request.environ['HTTP_ACCEPT_LANGUAGE']
      self.request_info['_script'] = list(request._script)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
210

211
  def getObject(self, activity_tool):
212
    """return the object referenced in this message."""
213
    return activity_tool.unrestrictedTraverse(self.object_path)
214

215
  def getObjectList(self, activity_tool):
216
    """return the list of object that can be expanded from this message."""
217
    object_list = []
218
    try:
219
      object_list.append(self.getObject(activity_tool))
220
    except KeyError:
221 222 223 224 225 226
      pass
    else:
      if self.hasExpandMethod():
        expand_method_id = self.activity_kw['expand_method_id']
        # FIXME: how to pass parameters?
        object_list = getattr(object_list[0], expand_method_id)()
227
    return object_list
228

229
  def hasExpandMethod(self):
230 231 232 233 234
    """return true if the message has an expand method.
    An expand method is used to expand the list of objects and to turn a
    big recursive transaction affecting many objects into multiple
    transactions affecting only one object at a time (this can prevent
    duplicated method calls)."""
235
    return self.activity_kw.has_key('expand_method_id')
236

237
  def changeUser(self, user_name, activity_tool):
238
    """restore the security context for the calling user."""
239 240
    uf = activity_tool.getPortalObject().acl_users
    user = uf.getUserById(user_name)
241
    # if the user is not found, try to get it from a parent acl_users
242 243 244 245
    # XXX this is still far from perfect, because we need to store all
    # informations about the user (like original user folder, roles) to
    # replay the activity with exactly the same security context as if
    # it had been executed without activity.
246 247 248
    if user is None:
      uf = activity_tool.getPortalObject().aq_parent.acl_users
      user = uf.getUserById(user_name)
249 250 251
    if user is not None:
      user = user.__of__(uf)
      newSecurityManager(None, user)
252
    else :
253
      LOG("CMFActivity", WARNING,
254
          "Unable to find user %r in the portal" % user_name)
255
      noSecurityManager()
256 257 258 259 260
    return user

  def activateResult(self, activity_tool, result, object):
    if self.active_process is not None:
      active_process = activity_tool.unrestrictedTraverse(self.active_process)
261
      if isinstance(result, ActiveResult):
262 263
        result.edit(object_path=object)
        result.edit(method_id=self.method_id)
264 265
        # XXX Allow other method_id in future
        active_process.activateResult(result)
266
      else:
267
        active_process.activateResult(
268
                    ActiveResult(object_path=object,
269 270
                                 method_id=self.method_id,
                                 result=result)) # XXX Allow other method_id in future
271

Jean-Paul Smets's avatar
Jean-Paul Smets committed
272
  def __call__(self, activity_tool):
273
    try:
274
      obj = self.getObject(activity_tool)
275
    except KeyError:
276 277 278 279
      LOG('CMFActivity', ERROR,
          'Message failed in getting an object from the path %r' % \
                  (self.object_path,),
          error=sys.exc_info())
280
      self.setExecutionState(MESSAGE_NOT_EXECUTABLE, context=activity_tool)
281
    else:
282
      try:
283 284
        old_security_manager = getSecurityManager()
        try:
285 286 287
          # Change user if required (TO BE DONE)
          # We will change the user only in order to execute this method
          self.changeUser(self.user_name, activity_tool)
288 289 290
          try:
            # XXX: There is no check to see if user is allowed to access
            # that method !
291 292
            method = getattr(obj, self.method_id)
          except:
293 294 295 296
            LOG('CMFActivity', ERROR,
                'Message failed in getting a method %r from an object %r' % \
                       (self.method_id, obj,),
                error=sys.exc_info())
297
            method = None
298
            self.setExecutionState(MESSAGE_NOT_EXECUTABLE, context=activity_tool)
299
          else:
300
            if activity_tool.activity_timing_log:
301 302 303
              result = activity_timing_method(method, self.args, self.kw)
            else:
              result = method(*self.args, **self.kw)
304 305 306 307 308
        finally:
          setSecurityManager(old_security_manager)

        if method is not None:
          self.activateResult(activity_tool, result, obj)
309
          self.setExecutionState(MESSAGE_EXECUTED)
310
      except:
311
        self.setExecutionState(MESSAGE_NOT_EXECUTED, context=activity_tool)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
312

313 314 315 316 317 318 319
  def validate(self, activity, activity_tool, check_order_validation=1):
    return activity.validate(activity_tool, self,
                             check_order_validation=check_order_validation,
                             **self.activity_kw)

  def getDependentMessageList(self, activity, activity_tool):
    return activity.getDependentMessageList(activity_tool, self, **self.activity_kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
320

321
  def notifyUser(self, activity_tool, retry=False):
322 323
    """Notify the user that the activity failed."""
    portal = activity_tool.getPortalObject()
324
    user_email = portal.getProperty('email_to_address',
325
                       portal.getProperty('email_from_address'))
326

327 328
    email_from_name = portal.getProperty('email_from_name',
                       portal.getProperty('email_from_address'))
329 330 331 332
    call_traceback = ''
    if self.call_traceback:
      call_traceback = 'Created at:\n%s' % self.call_traceback

333
    fail_count = self.line.retry + 1
Julien Muchembled's avatar
typo  
Julien Muchembled committed
334
    if self.getExecutionState() == MESSAGE_NOT_EXECUTABLE:
335 336
      message = "Not executable activity"
    elif retry:
337 338 339 340
      message = "Pending activity already failed %s times" % fail_count
    else:
      message = "Activity failed"
    path = '/'.join(self.object_path)
341
    mail_text = """From: %s <%s>
342
To: %s
343
Subject: %s: %s/%s
344

345
Node: %s
346
Failures: %s
347
User name: %r
348 349
Document: %s
Method: %s
350 351
Arguments: %r
Named Parameters: %r
352 353
%s

Vincent Pelletier's avatar
Vincent Pelletier committed
354
Exception: %s %s
355

356
%s
357 358 359 360
""" % (email_from_name, activity_tool.email_from_address, user_email,
       message, path, self.method_id,
       activity_tool.getCurrentNode(), fail_count,
       self.user_name, path, self.method_id, self.args, self.kw,
361
       call_traceback, self.exc_type, self.exc_value, self.traceback)
362 363 364 365 366 367

    if isinstance(mail_text, unicode):
      # __traceback_info__ can turn the tracebacks into unicode strings, but
      # MailHost.send (in Zope 2.8) will not be able to parse headers if the
      # mail_text is passed as a unicode.
      mail_text = mail_text.encode('utf8')
368 369
    try:
      activity_tool.MailHost.send( mail_text )
Vincent Pelletier's avatar
Vincent Pelletier committed
370 371
    except (socket.error, MailHostError), message:
      LOG('ActivityTool.notifyUser', WARNING, 'Mail containing failure information failed to be sent: %s. Exception was: %s %s\n%s' % (message, self.exc_type, self.exc_value, self.traceback))
372

373
  def reactivate(self, activity_tool, activity=DEFAULT_ACTIVITY):
374 375
    # Reactivate the original object.
    obj= self.getObject(activity_tool)
376
    old_security_manager = getSecurityManager()
377
    try:
378 379 380
      # Change user if required (TO BE DONE)
      # We will change the user only in order to execute this method
      user = self.changeUser(self.user_name, activity_tool)
381
      active_obj = obj.activate(activity=activity, **self.activity_kw)
382 383 384
      getattr(active_obj, self.method_id)(*self.args, **self.kw)
    finally:
      # Use again the previous user
385
      setSecurityManager(old_security_manager)
386

387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
  def setExecutionState(self, is_executed, exc_info=None, log=True, context=None):
    """
      Set message execution state.

      is_executed can be one of MESSAGE_NOT_EXECUTED, MESSAGE_EXECUTED and
      MESSAGE_NOT_EXECUTABLE (variables defined above).
      
      exc_info must be - if given - similar to sys.exc_info() return value.

      log must be - if given - True or False. If True, a log line will be
      emited with failure details. This parameter should only be used when
      invoking this method on a list of messages to avoid log flood. It is
      caller's responsability to output a log line summing up all errors, and
      to store error in Zope's error_log.

      context must be - if given - an object wrapped in acquisition context.
      It is used to access Zope's error_log object. It is not used if log is
      False.

      If given state is not MESSAGE_EXECUTED, it will also store given
      exc_info. If not given, it will extract one using sys.exc_info().
      If final exc_info does not contain any exception, current stack trace
      will be stored instead: it will hopefuly help understand why message
      is in an error state.
    """
    assert is_executed in (MESSAGE_NOT_EXECUTED, MESSAGE_EXECUTED, MESSAGE_NOT_EXECUTABLE)
    self.is_executed = is_executed
    if is_executed != MESSAGE_EXECUTED:
      if exc_info is None:
        exc_info = sys.exc_info()
417 418 419 420 421 422 423
      if exc_info == (None, None, None):
        # Raise a dummy exception, ignore it, fetch it and use it as if it was the error causing message non-execution. This will help identifyting the cause of this misbehaviour.
        try:
          raise Exception, 'Message execution failed, but there is no exception to explain it. This is a dummy exception so that one can track down why we end up here outside of an exception handling code path.'
        except:
          pass
        exc_info = sys.exc_info()
424 425 426 427 428
      if log:
        LOG('ActivityTool', WARNING, 'Could not call method %s on object %s. Activity created at:\n%s' % (self.method_id, self.object_path, self.call_traceback), error=exc_info)
        # push the error in ZODB error_log
        error_log = getattr(context, 'error_log', None)
        if error_log is not None:
429
          error_log.raising(exc_info)
430 431
      self.exc_type = exc_info[0]
      self.exc_value = str(exc_info[1])
432
      self.traceback = ''.join(ExceptionFormatter.format_exception(*exc_info))
433 434 435 436

  def getExecutionState(self):
    return self.is_executed

Jean-Paul Smets's avatar
Jean-Paul Smets committed
437 438
class Method:

439
  def __init__(self, passive_self, activity, active_process, kw, method_id):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
440 441
    self.__passive_self = passive_self
    self.__activity = activity
442
    self.__active_process = active_process
Jean-Paul Smets's avatar
Jean-Paul Smets committed
443 444 445 446
    self.__kw = kw
    self.__method_id = method_id

  def __call__(self, *args, **kw):
447
    m = Message(self.__passive_self, self.__active_process, self.__kw, self.__method_id, args, kw)
448
    portal_activities = self.__passive_self.getPortalObject().portal_activities
449
    if portal_activities.activity_tracking:
450
      activity_tracking_logger.info('queuing message: activity=%s, object_path=%s, method_id=%s, args=%s, kw=%s, activity_kw=%s, user_name=%s' % (self.__activity, '/'.join(m.object_path), m.method_id, m.args, m.kw, m.activity_kw, m.user_name))
451
    activity_dict[self.__activity].queueMessage(portal_activities, m)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
452

453 454
allow_class(Method)

Jean-Paul Smets's avatar
Jean-Paul Smets committed
455 456
class ActiveWrapper:

457
  def __init__(self, passive_self, activity, active_process, **kw):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
458 459
    self.__dict__['__passive_self'] = passive_self
    self.__dict__['__activity'] = activity
460
    self.__dict__['__active_process'] = active_process
Jean-Paul Smets's avatar
Jean-Paul Smets committed
461 462 463 464
    self.__dict__['__kw'] = kw

  def __getattr__(self, id):
    return Method(self.__dict__['__passive_self'], self.__dict__['__activity'],
465
                  self.__dict__['__active_process'],
Jean-Paul Smets's avatar
Jean-Paul Smets committed
466 467
                  self.__dict__['__kw'], id)

468 469 470 471
  def __repr__(self):
    return '<%s at 0x%x to %r>' % (self.__class__.__name__, id(self),
                                   self.__dict__['__passive_self'])

472 473 474
# True when activities cannot be executing any more.
has_processed_shutdown = False

475 476 477 478 479 480 481 482
def cancelProcessShutdown():
  """
    This method reverts the effect of calling "process_shutdown" on activity
    tool.
  """
  global has_processed_shutdown
  is_running_lock.release()
  has_processed_shutdown = False
483

484
class ActivityTool (Folder, UniqueObject):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
485
    """
Jean-Paul Smets's avatar
Jean-Paul Smets committed
486 487 488 489 490 491 492 493 494 495 496 497
    ActivityTool is the central point for activity management.

    Improvement to consider to reduce locks:

      Idea 1: create an SQL tool which accumulate queries and executes them at the end of a transaction,
              thus allowing all SQL transaction to happen in a very short time
              (this would also be a great way of using MyISAM tables)

      Idea 2: do the same at the level of ActivityTool

      Idea 3: do the same at the level of each activity (ie. queueMessage
              accumulates and fires messages at the end of the transactino)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
498 499 500
    """
    id = 'portal_activities'
    meta_type = 'CMF Activity Tool'
501
    portal_type = 'Activity Tool'
502
    allowed_types = ( 'CMF Active Process', )
Jean-Paul Smets's avatar
Jean-Paul Smets committed
503 504
    security = ClassSecurityInfo()

505 506
    manage_options = tuple(
                     [ { 'label' : 'Overview', 'action' : 'manage_overview' }
Jean-Paul Smets's avatar
Jean-Paul Smets committed
507
                     , { 'label' : 'Activities', 'action' : 'manageActivities' }
508
                     , { 'label' : 'LoadBalancing', 'action' : 'manageLoadBalancing'}
509
                     , { 'label' : 'Advanced', 'action' : 'manageActivitiesAdvanced' }
Jean-Paul Smets's avatar
Jean-Paul Smets committed
510
                     ,
511
                     ] + list(Folder.manage_options))
Jean-Paul Smets's avatar
Jean-Paul Smets committed
512 513 514 515

    security.declareProtected( CMFCorePermissions.ManagePortal , 'manageActivities' )
    manageActivities = DTMLFile( 'dtml/manageActivities', globals() )

516 517 518
    security.declareProtected( CMFCorePermissions.ManagePortal , 'manageActivitiesAdvanced' )
    manageActivitiesAdvanced = DTMLFile( 'dtml/manageActivitiesAdvanced', globals() )

519 520
    security.declareProtected( CMFCorePermissions.ManagePortal , 'manage_overview' )
    manage_overview = DTMLFile( 'dtml/explainActivityTool', globals() )
521 522 523 524 525 526
    
    security.declareProtected( CMFCorePermissions.ManagePortal , 'manageLoadBalancing' )
    manageLoadBalancing = DTMLFile( 'dtml/manageLoadBalancing', globals() )
    
    distributingNode = ''
    _nodes = ()
527 528 529
    activity_creation_trace = False
    activity_tracking = False
    activity_timing_log = False
530
    cancel_and_invoke_links_hidden = False
531

532 533 534 535 536
    def SQLDict_setPriority(self, **kw):
      real_SQLDict_setPriority = getattr(self.aq_parent, 'SQLDict_setPriority')
      LOG('ActivityTool', 0, real_SQLDict_setPriority(src__=1, **kw))
      return real_SQLDict_setPriority(**kw)

537 538
    def __init__(self):
        return Folder.__init__(self, ActivityTool.id)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
539

540 541 542
    # Filter content (ZMI))
    def filtered_meta_types(self, user=None):
        # Filters the list of available meta types.
543
        all = Folder.filtered_meta_types(self)
544 545 546 547 548 549
        meta_types = []
        for meta_type in self.all_meta_types():
            if meta_type['name'] in self.allowed_types:
                meta_types.append(meta_type)
        return meta_types

Jean-Paul Smets's avatar
Jean-Paul Smets committed
550 551
    def initialize(self):
      global is_initialized
Sebastien Robin's avatar
Sebastien Robin committed
552
      from Activity import RAMQueue, RAMDict, SQLQueue, SQLDict
Jean-Paul Smets's avatar
Jean-Paul Smets committed
553
      # Initialize each queue
554
      for activity in activity_dict.itervalues():
Jean-Paul Smets's avatar
Jean-Paul Smets committed
555
        activity.initialize(self)
Vincent Pelletier's avatar
Vincent Pelletier committed
556
      is_initialized = True
557 558 559
      
    security.declareProtected(Permissions.manage_properties, 'isSubscribed')
    def isSubscribed(self):
Aurel's avatar
Aurel committed
560
        """
561 562 563 564 565 566 567 568 569 570 571 572
        return True, if we are subscribed to TimerService.
        Otherwise return False.
        """
        service = getTimerService(self)
        if not service:
            LOG('ActivityTool', INFO, 'TimerService not available')
            return False
        
        path = '/'.join(self.getPhysicalPath())
        if path in service.lisSubscriptions():
            return True
        return False
Jean-Paul Smets's avatar
Jean-Paul Smets committed
573

574
    security.declareProtected(Permissions.manage_properties, 'subscribe')
575
    def subscribe(self, REQUEST=None, RESPONSE=None):
576 577
        """ subscribe to the global Timer Service """
        service = getTimerService(self)
578
        url = '%s/manageLoadBalancing?manage_tabs_message=' %self.absolute_url()
579
        if not service:
580
            LOG('ActivityTool', INFO, 'TimerService not available')
581 582 583 584
            url += urllib.quote('TimerService not available')
        else:
            service.subscribe(self)
            url += urllib.quote("Subscribed to Timer Service")
585 586
        if RESPONSE is not None:
            RESPONSE.redirect(url)
587 588

    security.declareProtected(Permissions.manage_properties, 'unsubscribe')
589
    def unsubscribe(self, REQUEST=None, RESPONSE=None):
590 591
        """ unsubscribe from the global Timer Service """
        service = getTimerService(self)
592
        url = '%s/manageLoadBalancing?manage_tabs_message=' %self.absolute_url()
593
        if not service:
594
            LOG('ActivityTool', INFO, 'TimerService not available')
595 596 597 598
            url += urllib.quote('TimerService not available')
        else:
            service.unsubscribe(self)
            url += urllib.quote("Unsubscribed from Timer Service")
599 600
        if RESPONSE is not None:
            RESPONSE.redirect(url)
601

602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
    security.declareProtected(Permissions.manage_properties, 'isActivityTrackingEnabled')
    def isActivityTrackingEnabled(self):
      return self.activity_tracking

    security.declareProtected(Permissions.manage_properties, 'manage_enableActivityTracking')
    def manage_enableActivityTracking(self, REQUEST=None, RESPONSE=None):
        """
          Enable activity tracing.
        """
        self.activity_tracking = True
        if RESPONSE is not None:
          url = '%s/manageActivitiesAdvanced?manage_tabs_message=' % self.absolute_url()
          url += urllib.quote('Tracking log enabled')
          RESPONSE.redirect(url)

    security.declareProtected(Permissions.manage_properties, 'manage_disableActivityTracking')
    def manage_disableActivityTracking(self, REQUEST=None, RESPONSE=None):
        """
          Disable activity tracing.
        """
        self.activity_tracking = False
        if RESPONSE is not None:
          url = '%s/manageActivitiesAdvanced?manage_tabs_message=' % self.absolute_url()
          url += urllib.quote('Tracking log disabled')
          RESPONSE.redirect(url)

    security.declareProtected(Permissions.manage_properties, 'isActivityTimingLoggingEnabled')
    def isActivityTimingLoggingEnabled(self):
      return self.activity_timing_log

    security.declareProtected(Permissions.manage_properties, 'manage_enableActivityTimingLogging')
    def manage_enableActivityTimingLogging(self, REQUEST=None, RESPONSE=None):
        """
          Enable activity timing logging.
        """
        self.activity_timing_log = True
        if RESPONSE is not None:
          url = '%s/manageActivitiesAdvanced?manage_tabs_message=' % self.absolute_url()
          url += urllib.quote('Timing log enabled')
          RESPONSE.redirect(url)

    security.declareProtected(Permissions.manage_properties, 'manage_disableActivityTimingLogging')
    def manage_disableActivityTimingLogging(self, REQUEST=None, RESPONSE=None):
        """
          Disable activity timing logging.
        """
        self.activity_timing_log = False
        if RESPONSE is not None:
          url = '%s/manageActivitiesAdvanced?manage_tabs_message=' % self.absolute_url()
          url += urllib.quote('Timing log disabled')
          RESPONSE.redirect(url)

    security.declareProtected(Permissions.manage_properties, 'isActivityCreationTraceEnabled')
    def isActivityCreationTraceEnabled(self):
      return self.activity_creation_trace

    security.declareProtected(Permissions.manage_properties, 'manage_enableActivityCreationTrace')
    def manage_enableActivityCreationTrace(self, REQUEST=None, RESPONSE=None):
        """
          Enable activity creation trace.
        """
        self.activity_creation_trace = True
        if RESPONSE is not None:
          url = '%s/manageActivitiesAdvanced?manage_tabs_message=' % self.absolute_url()
          url += urllib.quote('Activity creation trace enabled')
          RESPONSE.redirect(url)

    security.declareProtected(Permissions.manage_properties, 'manage_disableActivityCreationTrace')
    def manage_disableActivityCreationTrace(self, REQUEST=None, RESPONSE=None):
        """
          Disable activity creation trace.
        """
        self.activity_creation_trace = False
        if RESPONSE is not None:
          url = '%s/manageActivitiesAdvanced?manage_tabs_message=' % self.absolute_url()
          url += urllib.quote('Activity creation trace disabled')
          RESPONSE.redirect(url)

680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
    security.declareProtected(Permissions.manage_properties, 'isCancelAndInvokeLinksHidden')
    def isCancelAndInvokeLinksHidden(self):
      return self.cancel_and_invoke_links_hidden

    security.declareProtected(Permissions.manage_properties, 'manage_hideCancelAndInvokeLinks')
    def manage_hideCancelAndInvokeLinks(self, REQUEST=None, RESPONSE=None):
        """
        """
        self.cancel_and_invoke_links_hidden = True
        if RESPONSE is not None:
          url = '%s/manageActivitiesAdvanced?manage_tabs_message=' % self.absolute_url()
          url += urllib.quote('Cancel and invoke links hidden')
          RESPONSE.redirect(url)

    security.declareProtected(Permissions.manage_properties, 'manage_showCancelAndInvokeLinks')
    def manage_showCancelAndInvokeLinks(self, REQUEST=None, RESPONSE=None):
        """
        """
        self.cancel_and_invoke_links_hidden = False
        if RESPONSE is not None:
          url = '%s/manageActivitiesAdvanced?manage_tabs_message=' % self.absolute_url()
          url += urllib.quote('Cancel and invoke links visible')
          RESPONSE.redirect(url)

704 705
    def manage_beforeDelete(self, item, container):
        self.unsubscribe()
706 707
        Folder.inheritedAttribute('manage_beforeDelete')(self, item, container)
    
708 709
    def manage_afterAdd(self, item, container):
        self.subscribe()
710 711
        Folder.inheritedAttribute('manage_afterAdd')(self, item, container)
       
712 713
    def getCurrentNode(self):
        """ Return current node in form ip:port """
714 715
        global currentNode
        if currentNode is None:
716
          ip = port = ''
717 718
          from asyncore import socket_map
          for k, v in socket_map.items():
719
              if hasattr(v, 'addr'):
720 721 722
                  # see Zope/lib/python/App/ApplicationManager.py: def getServers(self)
                  type = str(getattr(v, '__class__', 'unknown'))
                  if type == 'ZServer.HTTPServer.zhttp_server':
723
                      ip, port = v.addr
724
                      break
725 726
          if ip == '0.0.0.0':
            ip = socket.gethostbyname(socket.gethostname())
727
          currentNode = '%s:%s' %(ip, port)
728 729 730 731 732 733 734
        return currentNode
        
    security.declarePublic('getDistributingNode')
    def getDistributingNode(self):
        """ Return the distributingNode """
        return self.distributingNode

735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771
    def getNodeList(self, role=None):
      node_dict = self.getNodeDict()
      if role is None:
        result = [x for x in node_dict.keys()]
      else:
        result = [node_id for node_id, node_role in node_dict.items() if node_role == role]
      result.sort()
      return result

    def getNodeDict(self):
      nodes = self._nodes
      if isinstance(nodes, tuple):
        new_nodes = OIBTree()
        new_nodes.update([(x, ROLE_PROCESSING) for x in self._nodes])
        self._nodes = nodes = new_nodes
      return nodes

    def registerNode(self, node):
      node_dict = self.getNodeDict()
      if not node_dict.has_key(node):
        if len(node_dict) == 0: # If we are registering the first node, make
                                # it both the distributing node and a processing
                                # node.
          role = ROLE_PROCESSING
          self.distributingNode = node
        else:
          role = ROLE_IDLE
        self.updateNode(node, role)

    def updateNode(self, node, role):
      node_dict = self.getNodeDict()
      node_dict[node] = role

    security.declareProtected(CMFCorePermissions.ManagePortal, 'getProcessingNodeList')
    def getProcessingNodeList(self):
      return self.getNodeList(role=ROLE_PROCESSING)

772
    security.declareProtected(CMFCorePermissions.ManagePortal, 'getIdleNodeList')
773 774
    def getIdleNodeList(self):
      return self.getNodeList(role=ROLE_IDLE)
775

776 777 778 779
    def _isValidNodeName(self, node_name) :
      """Check we have been provided a good node name"""
      return isinstance(node_name, str) and NODE_RE.match(node_name)
      
780 781
    security.declarePublic('manage_setDistributingNode')
    def manage_setDistributingNode(self, distributingNode, REQUEST=None):
782
        """ set the distributing node """   
783
        if not distributingNode or self._isValidNodeName(distributingNode):
784 785 786 787 788 789 790 791 792 793 794 795 796
          self.distributingNode = distributingNode
          if REQUEST is not None:
              REQUEST.RESPONSE.redirect(
                  REQUEST.URL1 +
                  '/manageLoadBalancing?manage_tabs_message=' +
                  urllib.quote("Distributing Node successfully changed."))
        else :
          if REQUEST is not None:
              REQUEST.RESPONSE.redirect(
                  REQUEST.URL1 +
                  '/manageLoadBalancing?manage_tabs_message=' +
                  urllib.quote("Malformed Distributing Node."))

797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
    security.declareProtected(CMFCorePermissions.ManagePortal, 'manage_delNode')
    def manage_delNode(self, unused_node_list=None, REQUEST=None):
      """ delete selected unused nodes """
      processing_node = self.getDistributingNode()
      updated_processing_node = False
      if unused_node_list is not None:
        node_dict = self.getNodeDict()
        for node in unused_node_list:
          if node in node_dict:
            del node_dict[node]
          if node == processing_node:
            self.processing_node = ''
            updated_processing_node = True
      if REQUEST is not None:
        if unused_node_list is None:
          message = "No unused node selected, nothing deleted."
        else:
          message = "Deleted nodes %r." % (unused_node_list, )
        if updated_processing_node:
          message += "Disabled distributing node because it was deleted."
        REQUEST.RESPONSE.redirect(
          REQUEST.URL1 +
          '/manageLoadBalancing?manage_tabs_message=' +
          urllib.quote(message))

    security.declareProtected(CMFCorePermissions.ManagePortal, 'manage_addToProcessingList')
    def manage_addToProcessingList(self, unused_node_list=None, REQUEST=None):
      """ Change one or more idle nodes into processing nodes """
      if unused_node_list is not None:
        node_dict = self.getNodeDict()
        for node in unused_node_list:
          self.updateNode(node, ROLE_PROCESSING)
      if REQUEST is not None:
        if unused_node_list is None:
          message = "No unused node selected, nothing done."
        else:
          message = "Nodes now procesing: %r." % (unused_node_list, )
        REQUEST.RESPONSE.redirect(
          REQUEST.URL1 +
          '/manageLoadBalancing?manage_tabs_message=' +
          urllib.quote(message))

    security.declareProtected(CMFCorePermissions.ManagePortal, 'manage_removeFromProcessingList')
    def manage_removeFromProcessingList(self, processing_node_list=None, REQUEST=None):
      """ Change one or more procesing nodes into idle nodes """
      if processing_node_list is not None:
        node_dict = self.getNodeDict()
        for node in processing_node_list:
          self.updateNode(node, ROLE_IDLE)
      if REQUEST is not None:
        if processing_node_list is None:
          message = "No used node selected, nothing done."
        else:
          message = "Nodes now unused %r." % (processing_node_list, )
        REQUEST.RESPONSE.redirect(
          REQUEST.URL1 +
          '/manageLoadBalancing?manage_tabs_message=' +
          urllib.quote(message))
855

856 857 858 859 860
    def process_shutdown(self, phase, time_in_phase):
        """
          Prevent shutdown from happening while an activity queue is
          processing a batch.
        """
861
        global has_processed_shutdown
862 863
        if phase == 3 and not has_processed_shutdown:
          has_processed_shutdown = True
864 865 866 867
          LOG('CMFActivity', INFO, "Shutdown: Waiting for activities to finish.")
          is_running_lock.acquire()
          LOG('CMFActivity', INFO, "Shutdown: Activities finished.")

868
    def process_timer(self, tick, interval, prev="", next=""):
869
        """
870 871 872 873 874
        Call distribute() if we are the Distributing Node and call tic()
        with our node number.
        This method is called by TimerService in the interval given
        in zope.conf. The Default is every 5 seconds.
        """
875 876 877 878
        # Prevent TimerService from starting multiple threads in parallel
        acquired = timerservice_lock.acquire(0)
        if not acquired:
          return
879

880 881 882 883 884 885
        # make sure our skin is set-up. On CMF 1.5 it's setup by acquisition,
        # but on 2.2 it's by traversal, and our site probably wasn't traversed
        # by the timerserver request, which goes into the Zope Control_Panel
        # calling it a second time is a harmless and cheap no-op.
        # both setupCurrentSkin and REQUEST are acquired from containers.
        self.setupCurrentSkin(self.REQUEST)
886
        try:
887
          old_sm = getSecurityManager()
888
          try:
889 890 891 892 893 894 895 896 897 898
            try:
              # get owner of portal_catalog, so normally we should be able to
              # have the permission to invoke all activities
              user = self.portal_catalog.getWrappedOwner()
              newSecurityManager(self.REQUEST, user)

              currentNode = self.getCurrentNode()
              self.registerNode(currentNode)
              processing_node_list = self.getNodeList(role=ROLE_PROCESSING)

899
              # only distribute when we are the distributingNode
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
              if (self.getDistributingNode() == currentNode):
                self.distribute(len(processing_node_list))

              # SkinsTool uses a REQUEST cache to store skin objects, as
              # with TimerService we have the same REQUEST over multiple
              # portals, we clear this cache to make sure the cache doesn't
              # contains skins from another portal.
              stool = getToolByName(self, 'portal_skins', None)
              if stool is not None:
                stool.changeSkin(None)

              # call tic for the current processing_node
              # the processing_node numbers are the indices of the elements in the node tuple +1
              # because processing_node starts form 1
              if currentNode in processing_node_list:
                self.tic(processing_node_list.index(currentNode)+1)
            except:
              # Catch ALL exception to avoid killing timerserver.
              LOG('ActivityTool', ERROR, 'process_timer received an exception', error=sys.exc_info())
          finally:
            setSecurityManager(old_sm)
Jérome Perrin's avatar
Jérome Perrin committed
921
        finally:
922
          timerservice_lock.release()
923

Jean-Paul Smets's avatar
Jean-Paul Smets committed
924 925 926 927 928 929
    security.declarePublic('distribute')
    def distribute(self, node_count=1):
      """
        Distribute load
      """
      # Initialize if needed
Vincent Pelletier's avatar
Vincent Pelletier committed
930 931
      if not is_initialized:
        self.initialize()
Jean-Paul Smets's avatar
Jean-Paul Smets committed
932 933

      # Call distribute on each queue
934
      for activity in activity_dict.itervalues():
935
        activity.distribute(aq_inner(self), node_count)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
936

Jean-Paul Smets's avatar
Jean-Paul Smets committed
937
    security.declarePublic('tic')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
938
    def tic(self, processing_node=1, force=0):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
939 940
      """
        Starts again an activity
Jean-Paul Smets's avatar
Jean-Paul Smets committed
941
        processing_node starts from 1 (there is not node 0)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
942
      """
943
      global active_threads
Jean-Paul Smets's avatar
Jean-Paul Smets committed
944 945

      # return if the number of threads is too high
946
      # else, increase the number of active_threads and continue
947 948
      tic_lock.acquire()
      too_many_threads = (active_threads >= max_active_threads)
949
      if not too_many_threads or force:
950
        active_threads += 1
951 952 953
      else:
        tic_lock.release()
        raise RuntimeError, 'Too many threads'
954
      tic_lock.release()
955

Jean-Paul Smets's avatar
Jean-Paul Smets committed
956
      # Initialize if needed
Vincent Pelletier's avatar
Vincent Pelletier committed
957 958
      if not is_initialized:
        self.initialize()
959

960
      inner_self = aq_inner(self)
961

962
      try:
963
        #Sort activity list by priority
964 965 966
        activity_list = sorted(activity_dict.itervalues(),
                               key=lambda activity: activity.getPriority(self))

967
        # Wakeup each queue
968
        for activity in activity_list:
969
          activity.wakeup(inner_self, processing_node)
970

971 972 973 974
        # Process messages on each queue in round robin
        has_awake_activity = 1
        while has_awake_activity:
          has_awake_activity = 0
975
          for activity in activity_list:
976 977 978
            acquired = is_running_lock.acquire(0)
            if acquired:
              try:
979 980
                activity.tic(inner_self, processing_node) # Transaction processing is the responsability of the activity
                has_awake_activity = has_awake_activity or activity.isAwake(inner_self, processing_node)
981 982
              finally:
                is_running_lock.release()
983 984 985 986 987
      finally:
        # decrease the number of active_threads
        tic_lock.acquire()
        active_threads -= 1
        tic_lock.release()
Jean-Paul Smets's avatar
Jean-Paul Smets committed
988

989
    def hasActivity(self, *args, **kw):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
990
      # Check in each queue if the object has deferred tasks
991 992
      # if not argument is provided, then check on self
      if len(args) > 0:
993
        obj = args[0]
994
      else:
995
        obj = self
996
      for activity in activity_dict.itervalues():
997
        if activity.hasActivity(aq_inner(self), obj, **kw):
998 999
          return True
      return False
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1000

1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
    def getActivityBuffer(self, create_if_not_found=True):
      """
        Get activtity buffer for this thread for this activity tool.
        If no activity buffer is found at lowest level and create_if_not_found
        is True, create one.
        Intermediate level is unconditionaly created if non existant because
        chances are it will be used in the instance life.
        Lock is held when checking for intermediate level existance
        because:
         - intermediate level dict must not be created in 2 threads at the
           same time, since one creation would destroy the existing one.
        It's released after that step because:
         - lower level access is at thread scope, thus by definition there
           can be only one access at a time to a key
         - GIL protects us when accessing python instances
      """
1017 1018
      # Safeguard: make sure we are wrapped in  acquisition context before
      # using our path as an activity tool instance-wide identifier.
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037
      assert getattr(self, 'aq_self', None) is not None
      my_instance_key = self.getPhysicalPath()
      my_thread_key = get_ident()
      global_activity_buffer_lock.acquire()
      try:
        if my_instance_key not in global_activity_buffer:
          global_activity_buffer[my_instance_key] = {}
      finally:
        global_activity_buffer_lock.release()
      thread_activity_buffer = global_activity_buffer[my_instance_key]
      if my_thread_key not in thread_activity_buffer:
        if create_if_not_found:
          buffer = ActivityBuffer(activity_tool=self)
        else:
          buffer = None
        thread_activity_buffer[my_thread_key] = buffer
      activity_buffer = thread_activity_buffer[my_thread_key]
      return activity_buffer

1038 1039
    security.declarePrivate('activateObject')
    def activateObject(self, object, activity, active_process, **kw):
Vincent Pelletier's avatar
Vincent Pelletier committed
1040 1041
      if not is_initialized:
        self.initialize()
1042
      self.getActivityBuffer()
1043
      return ActiveWrapper(object, activity, active_process, **kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1044

1045
    def deferredQueueMessage(self, activity, message):
1046 1047
      activity_buffer = self.getActivityBuffer()
      activity_buffer.deferredQueueMessage(self, activity, message)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
1048

1049
    def deferredDeleteMessage(self, activity, message):
1050 1051
      activity_buffer = self.getActivityBuffer()
      activity_buffer.deferredDeleteMessage(self, activity, message)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
1052

Jean-Paul Smets's avatar
Jean-Paul Smets committed
1053
    def getRegisteredMessageList(self, activity):
1054
      activity_buffer = self.getActivityBuffer(create_if_not_found=False)
1055
      if activity_buffer is not None:
1056 1057
        #activity_buffer._register() # This is required if flush flush is called outside activate
        return activity.getRegisteredMessageList(activity_buffer,
1058
                                                 aq_inner(self))
1059 1060
      else:
        return []
Yoshinori Okuji's avatar
Yoshinori Okuji committed
1061

Jean-Paul Smets's avatar
Jean-Paul Smets committed
1062
    def unregisterMessage(self, activity, message):
1063 1064 1065
      activity_buffer = self.getActivityBuffer()
      #activity_buffer._register()
      return activity.unregisterMessage(activity_buffer, aq_inner(self), message)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
1066

1067
    def flush(self, obj, invoke=0, **kw):
Vincent Pelletier's avatar
Vincent Pelletier committed
1068 1069
      if not is_initialized:
        self.initialize()
1070
      self.getActivityBuffer()
1071 1072
      if isinstance(obj, tuple):
        object_path = obj
1073
      else:
1074
        object_path = obj.getPhysicalPath()
1075
      for activity in activity_dict.itervalues():
1076
        activity.flush(aq_inner(self), object_path, invoke=invoke, **kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1077

1078
    def start(self, **kw):
Vincent Pelletier's avatar
Vincent Pelletier committed
1079 1080
      if not is_initialized:
        self.initialize()
1081
      for activity in activity_dict.itervalues():
1082
        activity.start(aq_inner(self), **kw)
1083 1084

    def stop(self, **kw):
Vincent Pelletier's avatar
Vincent Pelletier committed
1085 1086
      if not is_initialized:
        self.initialize()
1087
      for activity in activity_dict.itervalues():
1088
        activity.stop(aq_inner(self), **kw)
1089

Jean-Paul Smets's avatar
Jean-Paul Smets committed
1090
    def invoke(self, message):
1091
      if self.activity_tracking:
1092
        activity_tracking_logger.info('invoking message: object_path=%s, method_id=%s, args=%r, kw=%r, activity_kw=%r, user_name=%s' % ('/'.join(message.object_path), message.method_id, message.args, message.kw, message.activity_kw, message.user_name))
1093
      old_localizer_context = False
1094 1095
      if getattr(self, 'aq_chain', None) is not None:
        # Grab existing acquisition chain and extrach base objects.
1096
        base_chain = [aq_base(x) for x in self.aq_chain]
1097 1098 1099
        # Grab existig request (last chain item) and create a copy.
        request_container = base_chain.pop()
        request = request_container.REQUEST
1100 1101 1102 1103 1104 1105 1106 1107
        # Generate PARENTS value. Sadly, we cannot reuse base_chain since
        # PARENTS items must be wrapped in acquisition
        parents = []
        application = self.getPhysicalRoot().aq_base
        for parent in self.aq_chain:
          if parent.aq_base is application:
            break
          parents.append(parent)
1108 1109
        # XXX: REQUEST.clone() requires PARENTS to be set, and it's not when
        # runing unit tests. Recreate it if it does not exist.
1110 1111
        if getattr(request.other, 'PARENTS', None) is None:
          request.other['PARENTS'] = parents
1112
        # XXX: itools (used by Localizer) requires PATH_INFO to be set, and it's
1113 1114
        # not when runing unit tests. Recreate it if it does not exist.
        if request.environ.get('PATH_INFO') is None:
1115
          request.environ['PATH_INFO'] = '/Control_Panel/timer_service/process_timer'
1116 1117 1118
        
        # restore request information
        new_request = request.clone()
1119
        request_info = message.request_info
1120 1121
        # PARENTS is truncated by clone
        new_request.other['PARENTS'] = parents
1122 1123
        if '_script' in request_info:
          new_request._script = request_info['_script']
1124
        if 'SERVER_URL' in request_info:
1125
          new_request.other['SERVER_URL'] = request_info['SERVER_URL']
1126 1127 1128
        if 'VirtualRootPhysicalPath' in request_info:
          new_request.other['VirtualRootPhysicalPath'] = request_info['VirtualRootPhysicalPath']
        if 'HTTP_ACCEPT_LANGUAGE' in request_info:
1129
          new_request.environ['HTTP_ACCEPT_LANGUAGE'] = request_info['HTTP_ACCEPT_LANGUAGE']
1130 1131
          # Replace Localizer/iHotfix Context, saving existing one
          localizer_context = LocalizerContext(new_request)
1132
          id = get_ident()
1133
          localizer_lock.acquire()
1134
          try:
1135 1136
            old_localizer_context = localizer_contexts.get(id)
            localizer_contexts[id] = localizer_context
1137
          finally:
1138 1139
            localizer_lock.release()
          # Execute Localizer/iHotfix "patch 2"
1140
          new_request.processInputs()
1141 1142

        new_request_container = request_container.__class__(REQUEST=new_request)
1143 1144 1145 1146 1147 1148 1149 1150
        # Recreate acquisition chain.
        my_self = new_request_container
        base_chain.reverse()
        for item in base_chain:
          my_self = item.__of__(my_self)
      else:
        my_self = self
        LOG('CMFActivity.ActivityTool.invoke', INFO, 'Strange: invoke is called outside of acquisition context.')
1151 1152 1153
      try:
        message(my_self)
      finally:
1154 1155 1156 1157
        if my_self is not self: # We rewrapped self
          # Restore default skin selection
          skinnable = self.getPortalObject()
          skinnable.changeSkin(skinnable.getSkinNameFromRequest(request))
1158 1159
        if old_localizer_context is not False:
          # Restore Localizer/iHotfix context
1160
          id = get_ident()
1161
          localizer_lock.acquire()
1162
          try:
1163 1164
            if old_localizer_context is None:
              del localizer_contexts[id]
1165
            else:
1166
              localizer_contexts[id] = old_localizer_context
1167
          finally:
1168
            localizer_lock.release()
1169
      if self.activity_tracking:
1170
        activity_tracking_logger.info('invoked message')
1171 1172 1173
      if my_self is not self: # We rewrapped self
        for held in my_self.REQUEST._held:
          self.REQUEST._hold(held)
1174

1175
    def invokeGroup(self, method_id, message_list, activity, merge_duplicate):
1176
      if self.activity_tracking:
1177 1178 1179
        activity_tracking_logger.info(
          'invoking group messages: method_id=%s, paths=%s'
          % (method_id, ['/'.join(m.object_path) for m in message_list]))
1180 1181 1182
      # Invoke a group method.
      expanded_object_list = []
      new_message_list = []
1183
      path_set = set()
1184 1185 1186
      # Filter the list of messages. If an object is not available, mark its
      # message as non-executable. In addition, expand an object if necessary,
      # and make sure that no duplication happens.
1187
      for m in message_list:
1188 1189
        # alternate method is used to segregate objects which cannot be grouped.
        alternate_method_id = m.activity_kw.get('alternate_method_id')
1190 1191
        try:
          obj = m.getObject(self)
1192
        except KeyError:
1193 1194 1195 1196
          LOG('CMFActivity', ERROR,
              'Message failed in getting an object from the path %r' % \
                  (m.object_path,),
              error=sys.exc_info())
1197
          m.setExecutionState(MESSAGE_NOT_EXECUTABLE, context=self)
1198 1199
          continue
        try:
1200
          if m.hasExpandMethod():
1201
            subobject_list = m.getObjectList(self)
1202
          else:
1203 1204
            subobject_list = (obj,)
          for subobj in subobject_list:
1205 1206 1207 1208
            if merge_duplicate:
              path = subobj.getPath()
              if path in path_set:
                continue
1209
              path_set.add(path)
1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
            if alternate_method_id is not None \
               and hasattr(aq_base(subobj), alternate_method_id):
              # if this object is alternated,
              # generate a new single active object
              activity_kw = m.activity_kw.copy()
              activity_kw.pop('group_method_id', None)
              activity_kw.pop('group_id', None)
              active_obj = subobj.activate(activity=activity, **activity_kw)
              getattr(active_obj, alternate_method_id)(*m.args, **m.kw)
            else:
              expanded_object_list.append((subobj, m.args, m.kw))
1221
          new_message_list.append((m, obj))
1222
        except:
1223
          m.setExecutionState(MESSAGE_NOT_EXECUTED, context=self)
1224

1225 1226
      try:
        if len(expanded_object_list) > 0:
1227 1228
          method = self.unrestrictedTraverse(method_id)
          # FIXME: how to apply security here?
1229 1230 1231
          # NOTE: expanded_object_list must be set to failed objects by the
          #       callee. If it fully succeeds, expanded_object_list must be
          #       empty when returning.
1232
          result = method(expanded_object_list)
1233
        else:
1234 1235 1236
          result = None
      except:
        # In this case, the group method completely failed.
1237
        exc_info = sys.exc_info()
1238
        for m, obj in new_message_list:
1239
          m.setExecutionState(MESSAGE_NOT_EXECUTED, exc_info, log=False)
1240
        LOG('WARNING ActivityTool', 0,
1241
            'Could not call method %s on objects %s' %
1242
            (method_id, [x[0] for x in expanded_object_list]), error=exc_info)
1243 1244 1245
        error_log = getattr(self, 'error_log', None)
        if error_log is not None:
          error_log.raising(exc_info)
1246
      else:
1247 1248
        # Obtain all indices of failed messages.
        # Note that this can be a partial failure.
1249
        failed_message_set = set(id(x[2]) for x in expanded_object_list)
1250
        # Only for succeeded messages, an activity process is invoked (if any).
1251 1252 1253 1254
        for m, obj in new_message_list:
          # We use id of kw dict (persistent object) to know if there is a
          # failed 3-tuple corresponding to Message m.
          if id(m.kw) in failed_message_set:
1255
            m.setExecutionState(MESSAGE_NOT_EXECUTED, context=self)
1256 1257
          else:
            try:
1258
              m.activateResult(self, result, obj)
1259
            except:
1260
              m.setExecutionState(MESSAGE_NOT_EXECUTED, context=self)
1261
            else:
1262
              m.setExecutionState(MESSAGE_EXECUTED, context=self)
1263
      if self.activity_tracking:
1264
        activity_tracking_logger.info('invoked group messages')
1265

1266 1267
    def newMessage(self, activity, path, active_process,
                   activity_kw, method_id, *args, **kw):
1268
      # Some Security Cheking should be made here XXX
Vincent Pelletier's avatar
Vincent Pelletier committed
1269 1270
      if not is_initialized:
        self.initialize()
1271
      self.getActivityBuffer()
1272
      activity_dict[activity].queueMessage(aq_inner(self),
1273
        Message(path, active_process, activity_kw, method_id, args, kw))
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1274

1275
    security.declareProtected( CMFCorePermissions.ManagePortal, 'manageInvoke' )
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1276 1277 1278 1279 1280 1281
    def manageInvoke(self, object_path, method_id, REQUEST=None):
      """
        Invokes all methods for object "object_path"
      """
      if type(object_path) is type(''):
        object_path = tuple(object_path.split('/'))
1282
      self.flush(object_path,method_id=method_id,invoke=1)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1283
      if REQUEST is not None:
1284 1285
        return REQUEST.RESPONSE.redirect('%s/%s' %
                (self.absolute_url(), 'manageActivities'))
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1286

1287
    security.declareProtected( CMFCorePermissions.ManagePortal, 'manageCancel' )
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1288 1289 1290 1291 1292 1293
    def manageCancel(self, object_path, method_id, REQUEST=None):
      """
        Cancel all methods for object "object_path"
      """
      if type(object_path) is type(''):
        object_path = tuple(object_path.split('/'))
1294
      self.flush(object_path,method_id=method_id,invoke=0)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1295
      if REQUEST is not None:
1296 1297
        return REQUEST.RESPONSE.redirect('%s/%s' %
                (self.absolute_url(), 'manageActivities'))
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1298

1299 1300
    security.declareProtected( CMFCorePermissions.ManagePortal,
                               'manageClearActivities' )
1301
    def manageClearActivities(self, keep=1, REQUEST=None):
1302 1303 1304 1305 1306
      """
        Clear all activities and recreate tables.
      """
      folder = getToolByName(self, 'portal_skins').activity

1307
      # Obtain all pending messages.
1308
      message_list_dict = {}
1309
      if keep:
1310
        for activity in activity_dict.itervalues():
1311 1312
          if hasattr(activity, 'dumpMessageList'):
            try:
1313 1314
              message_list_dict[activity.__class__.__name__] =\
                                    activity.dumpMessageList(self)
1315 1316 1317
            except ConflictError:
              raise
            except:
1318 1319 1320
              LOG('ActivityTool', WARNING,
                  'could not dump messages from %s' %
                  (activity,), error=sys.exc_info())
1321 1322

      if getattr(folder, 'SQLDict_createMessageTable', None) is not None:
1323 1324 1325 1326 1327
        try:
          folder.SQLDict_dropMessageTable()
        except ConflictError:
          raise
        except:
1328
          LOG('CMFActivity', WARNING,
1329
              'could not drop the message table',
1330 1331 1332
              error=sys.exc_info())
        folder.SQLDict_createMessageTable()

1333
      if getattr(folder, 'SQLQueue_createMessageTable', None) is not None:
1334 1335 1336 1337 1338
        try:
          folder.SQLQueue_dropMessageTable()
        except ConflictError:
          raise
        except:
1339
          LOG('CMFActivity', WARNING,
1340
              'could not drop the message queue table',
1341 1342 1343
              error=sys.exc_info())
        folder.SQLQueue_createMessageTable()

1344
      # Reactivate the messages.
1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
      for activity, message_list in message_list_dict.iteritems():
        for m in message_list:
          try:
            m.reactivate(aq_inner(self), activity=activity)
          except ConflictError:
            raise
          except:
            LOG('ActivityTool', WARNING,
                'could not reactivate the message %r, %r' %
                (m.object_path, m.method_id), error=sys.exc_info())
1355

1356
      if REQUEST is not None:
1357 1358 1359 1360 1361 1362
        message = 'Activities%20Cleared'
        if keep:
          message = 'Tables%20Recreated'
        return REQUEST.RESPONSE.redirect(
            '%s/manageActivitiesAdvanced?manage_tabs_message=%s' % (
              self.absolute_url(), message))
1363

Jean-Paul Smets's avatar
Jean-Paul Smets committed
1364
    security.declarePublic('getMessageList')
1365
    def getMessageList(self,**kw):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1366 1367 1368
      """
        List messages waiting in queues
      """
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1369
      # Initialize if needed
Vincent Pelletier's avatar
Vincent Pelletier committed
1370 1371
      if not is_initialized:
        self.initialize()
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1372

Jean-Paul Smets's avatar
Jean-Paul Smets committed
1373
      message_list = []
1374
      for activity in activity_dict.itervalues():
Sebastien Robin's avatar
Sebastien Robin committed
1375
        try:
1376
          message_list += activity.getMessageList(aq_inner(self),**kw)
Sebastien Robin's avatar
Sebastien Robin committed
1377 1378
        except AttributeError:
          LOG('getMessageList, could not get message from Activity:',0,activity)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1379 1380
      return message_list

1381 1382 1383 1384 1385 1386
    security.declarePublic('countMessageWithTag')
    def countMessageWithTag(self, value):
      """
        Return the number of messages which match the given tag.
      """
      message_count = 0
1387
      for activity in activity_dict.itervalues():
1388
        message_count += activity.countMessageWithTag(aq_inner(self), value)
Sebastien Robin's avatar
Sebastien Robin committed
1389 1390 1391 1392 1393 1394 1395 1396 1397 1398
      return message_count

    security.declarePublic('countMessage')
    def countMessage(self, **kw):
      """
        Return the number of messages which match the given parameter.

        Parameters allowed:

        method_id : the id of the method
Jérome Perrin's avatar
Jérome Perrin committed
1399
        path : for activities on a particular object
Sebastien Robin's avatar
Sebastien Robin committed
1400 1401 1402 1403
        tag : activities with a particular tag
        message_uid : activities with a particular uid
      """
      message_count = 0
1404
      for activity in activity_dict.itervalues():
1405
        message_count += activity.countMessage(aq_inner(self), **kw)
1406 1407
      return message_count

1408
    security.declareProtected( CMFCorePermissions.ManagePortal , 'newActiveProcess' )
1409
    def newActiveProcess(self, **kw):
1410 1411
      from ActiveProcess import addActiveProcess
      new_id = str(self.generateNewId())
1412
      return addActiveProcess(self, new_id, **kw)
1413

1414
    # Active synchronisation methods
1415
    security.declarePrivate('validateOrder')
1416
    def validateOrder(self, message, validator_id, validation_value):
1417 1418 1419 1420 1421
      message_list = self.getDependentMessageList(message, validator_id, validation_value)
      return len(message_list) > 0

    security.declarePrivate('getDependentMessageList')
    def getDependentMessageList(self, message, validator_id, validation_value):
Vincent Pelletier's avatar
Vincent Pelletier committed
1422 1423
      if not is_initialized:
        self.initialize()
1424
      message_list = []
Vincent Pelletier's avatar
Vincent Pelletier committed
1425
      method_id = "_validate_%s" % validator_id
1426
      for activity in activity_dict.itervalues():
1427 1428 1429 1430 1431 1432
        method = getattr(activity, method_id, None)
        if method is not None:
          result = method(aq_inner(self), message, validation_value)
          if result:
            message_list.extend([(activity, m) for m in result])
      return message_list
1433

Yoshinori Okuji's avatar
Yoshinori Okuji committed
1434 1435
    # Required for tests (time shift)
    def timeShift(self, delay):
Vincent Pelletier's avatar
Vincent Pelletier committed
1436 1437
      if not is_initialized:
        self.initialize()
1438
      for activity in activity_dict.itervalues():
1439
        activity.timeShift(aq_inner(self), delay)
Yoshinori Okuji's avatar
Yoshinori Okuji committed
1440

1441
InitializeClass(ActivityTool)