src/pyams_scheduler/zodb.py
changeset 0 48483b0b26fa
equal deleted inserted replaced
-1:000000000000 0:48483b0b26fa
       
     1 #
       
     2 # Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
       
     3 # All Rights Reserved.
       
     4 #
       
     5 # This software is subject to the provisions of the Zope Public License,
       
     6 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
       
     7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
       
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
       
    10 # FOR A PARTICULAR PURPOSE.
       
    11 #
       
    12 
       
    13 __docformat__ = 'restructuredtext'
       
    14 
       
    15 
       
    16 # import standard library
       
    17 
       
    18 # import interfaces
       
    19 from pyams_scheduler.interfaces.zodb import IZODBPackingTask
       
    20 from pyams_utils.interfaces.zeo import IZEOConnection
       
    21 from zope.component.interfaces import ISite
       
    22 
       
    23 # import packages
       
    24 from pyams_scheduler.task import Task
       
    25 from pyams_utils.traversing import get_parent
       
    26 from zope.interface import implementer
       
    27 from zope.schema.fieldproperty import FieldProperty
       
    28 
       
    29 
       
    30 @implementer(IZODBPackingTask)
       
    31 class ZODBPackingTask(Task):
       
    32     """ZODB packing task"""
       
    33 
       
    34     zeo_connection = FieldProperty(IZODBPackingTask['zeo_connection'])
       
    35     pack_time = FieldProperty(IZODBPackingTask['pack_time'])
       
    36 
       
    37     def run(self, report):
       
    38         site = get_parent(self, ISite)
       
    39         sm = site.getSiteManager()
       
    40         zeo_connection = sm.queryUtility(IZEOConnection, self.zeo_connection)
       
    41         if zeo_connection is None:
       
    42             report.write("Can't find ZEO connection. Task aborted.")
       
    43             return
       
    44         report.write("Loaded ZEO connection {0}\n".format(self.zeo_connection))
       
    45         report.write("Packing transactions older than {0} days\n".format(self.pack_time))
       
    46         storage, db = zeo_connection.get_connection(get_storage=True)
       
    47         try:
       
    48             db.pack(days=self.pack_time)
       
    49             report.write("\nPack successful.\n")
       
    50         finally:
       
    51             storage.close()