Interception de ConnectionStateError qui peut survenir lorsque des images sont suppri...
[Portfolio.git] / manipulation.py
index 8c057d6..dcba20d 100755 (executable)
@@ -1,24 +1,25 @@
 # -*- coding: utf-8 -*-
 # -*- coding: utf-8 -*-
-####################################################
-# Copyright © 2009 Luxia SAS. All rights reserved. #
-#                                                  #
-# Contributors:                                    #
-#  - Benoît Pin <pinbe@luxia.fr>                   #
-####################################################
+############################################################
+# Copyright © 2005-2010  Benoît PIN <benoit.pin@ensmp.fr>  #
+# Plinn - http://plinn.org                                 #
+#                                                          #
+# This program is free software; you can redistribute it   #
+# and/or modify it under the terms of the Creative Commons #
+# "Attribution-Noncommercial 2.0 Generic"                  #
+# http://creativecommons.org/licenses/by-nc/2.0/           #
+############################################################
 """ Image threaded batch computation module
 """ Image threaded batch computation module
-
-$Id: manipulation.py 1391 2009-09-16 23:36:05Z pin $
-$URL: http://svn.luxia.fr/svn/labo/projects/zope/Portfolio/trunk/manipulation.py $
 """
 
 import threading
 import logging
 import atexit
 from types import StringTypes
 """
 
 import threading
 import logging
 import atexit
 from types import StringTypes
-#import Zope2
 from math import ceil
 import transaction
 from ZODB.POSException import ConflictError
 from math import ceil
 import transaction
 from ZODB.POSException import ConflictError
+from ZODB.POSException import ConnectionStateError
+from zope.site.hooks import setSite
 from cStringIO import StringIO
 
 console = logging.getLogger('[manipulation thread]')
 from cStringIO import StringIO
 
 console = logging.getLogger('[manipulation thread]')
@@ -38,23 +39,6 @@ class ImageQueueProcessorThread(threading.Thread) :
                        itemsPath = [itemsPath]
                for i in itemsPath :
                        self.queueAdd(i)
                        itemsPath = [itemsPath]
                for i in itemsPath :
                        self.queueAdd(i)
-
-#      def __init__(self, portal_path, itemPath):
-#              threading.Thread.__init__(self)
-#              self.app = app = Zope2.app()
-#              self.portal = portal = app.unrestrictedTraverse(portal_path)
-#              self.imgTool = portal.portal_image_manipulation
-#              self.queue = []
-#              if itemPath :
-#                      self.queueAdd(itemPath)
-#              else :
-#                      ctool = portal.portal_catalog
-#                      brains = ctool.unrestrictedSearchResults(portal_type='Photo', tiles_available=0)
-#                      for b in brains :
-#                              self.queueAdd(b.getPath())
-       
-       def __del__(self) :
-               print "ayé, c'est la fin !"
        
        @property
        def queueSize(self) :
        
        @property
        def queueSize(self) :
@@ -68,15 +52,17 @@ class ImageQueueProcessorThread(threading.Thread) :
                #atexit.register(self.stop)
                import Zope2
                app = Zope2.app()
                #atexit.register(self.stop)
                import Zope2
                app = Zope2.app()
+               portal = app.unrestrictedTraverse(self.portal_path)
+               setSite(portal)
                while not self.__stopped and self.queueSize :
                        self._process(app)
                
                con = app._p_jar
                while not self.__stopped and self.queueSize :
                        self._process(app)
                
                con = app._p_jar
-               con.close()
-               #con.close()
+               try :
+                       con.close()
+               except ConnectionStateError, e :
+                       console.warn('ConnectionStateError raised before finished.')
                console.info('process finished.')
                console.info('process finished.')
-               #print con
-               #print con.transaction_manager
                
 
        def stop(self):
                
 
        def stop(self):
@@ -84,12 +70,7 @@ class ImageQueueProcessorThread(threading.Thread) :
                self.__stopped = True
        
        def _process(self, app) :
                self.__stopped = True
        
        def _process(self, app) :
-               
                path = self.queue.pop(0)
                path = self.queue.pop(0)
-               
-               #import Zope2
-               #app = Zope2.app()
-               
                try :
                        p = app.unrestrictedTraverse(path)
                except KeyError :
                try :
                        p = app.unrestrictedTraverse(path)
                except KeyError :
@@ -145,13 +126,14 @@ class ImageQueueProcessorThread(threading.Thread) :
                                pass
                        
                        p.tiles_available = 1
                                pass
                        
                        p.tiles_available = 1
+                       assert p._getCatalogTool()
                        p.reindexObject(idxs=['tiles_available'])
                        transaction.commit()
 
                except ConflictError :
                        console.warn('Resync after ZODB ConflicError')
                        transaction.abort()
                        p.reindexObject(idxs=['tiles_available'])
                        transaction.commit()
 
                except ConflictError :
                        console.warn('Resync after ZODB ConflicError')
                        transaction.abort()
-                       portal = app.unrestrictedTraverse(portal_path)
+                       portal = app.unrestrictedTraverse(self.portal_path)
                        portal._p_jar.sync()
                        self.queueAdd(path)
                        return
                        portal._p_jar.sync()
                        self.queueAdd(path)
                        return