source: mauRepo/MolesManager/trunk/src/MolesManager/molesSessionMiddleware.py @ 8358

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/mauRepo/MolesManager/trunk/src/MolesManager/molesSessionMiddleware.py@8358
Revision 8358, 4.7 KB checked in by mnagni, 7 years ago (diff)

Major refactoring about migration (now handles better create/update, even if the single updates have to be quite fully implemented)
Added the connection pool from SQLAlchemy

RevLine 
[8054]1'''
[8358]2BSD Licence
3Copyright (c) 2012, Science & Technology Facilities Council (STFC)
4All rights reserved.
5
6Redistribution and use in source and binary forms, with or without modification,
7are permitted provided that the following conditions are met:
8
9    * Redistributions of source code must retain the above copyright notice,
10        this list of conditions and the following disclaimer.
11    * Redistributions in binary form must reproduce the above copyright notice,
12        this list of conditions and the following disclaimer in the documentation
13        and/or other materials provided with the distribution.
14    * Neither the name of the Science & Technology Facilities Council (STFC)
15        nor the names of its contributors may be used to endorse or promote
16        products derived from this software without specific prior written permission.
17
18THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
22BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
23OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
[8054]29Created on 9 Jan 2012
30
[8358]31@author: Maurizio Nagni
[8054]32'''
[8325]33from MolesManager.moles3epb import Moles3EPBFactory
[8077]34from libs.migration.client import MigrationThread
[8155]35from MolesManager.settings import RUN_MIGRATION, MIGRATION_INTERVAL,\
36    MIGRATION_DB_CONNECTION, MIGRATION_DB_SCRIPT, MOLES3_DB_CONNECTION,\
[8223]37    MOLES3_DB_SCRIPT, EVENTS_DB, INFO_DB_CONNECTION
[8155]38from libs.commons_db import DbManager
[8326]39from libs.migration.InfodbEPB import InfodbEPBFactory
[8325]40from libs.migration.MigrationEPB import MigrationEPBFactory
[8358]41from sqlalchemy import exc
[8332]42from django.core.context_processors import csrf
43from django.shortcuts import render_to_response
[8054]44
[8088]45
[8229]46
[8054]47class MolesSessionMiddleware(object):
[8077]48   
49    #This attribute should be not here.
50    #unfortunately I cannot find any start/stop signals from Django
[8325]51    #_migrationThread = MigrationThread(interval = MIGRATION_INTERVAL)
[8155]52    _epbInitialized = False
[8326]53    _infodbEPBFactory = None   
[8325]54    _moles3EPBFactory = None
55    _migrationEPBFactory = None
[8077]56   
[8229]57    def _doInitialization(self):
[8332]58
[8351]59        infoDB = DbManager(INFO_DB_CONNECTION)
[8326]60        MolesSessionMiddleware._infodbEPBFactory = InfodbEPBFactory(infoDB)
61        #InfodbEPB.overrrideDBManager(infoDB)
[8229]62           
[8351]63        migrationDB = DbManager(MIGRATION_DB_CONNECTION, MIGRATION_DB_SCRIPT)
[8325]64        MolesSessionMiddleware._migrationEPBFactory = MigrationEPBFactory(migrationDB)               
[8229]65
[8351]66        molesDB = DbManager(MOLES3_DB_CONNECTION, MOLES3_DB_SCRIPT, session_event_manager=EVENTS_DB)
[8325]67        MolesSessionMiddleware._moles3EPBFactory = Moles3EPBFactory(molesDB)
68       
69        MolesSessionMiddleware._migrationThread = MigrationThread( \
[8337]70            MolesSessionMiddleware._moles3EPBFactory.createEPB(), \
71            MolesSessionMiddleware._migrationEPBFactory.createEPB(), \
[8334]72            MolesSessionMiddleware._infodbEPBFactory.createEPB(), \
[8325]73            interval = MIGRATION_INTERVAL )
[8323]74        MolesSessionMiddleware._epbInitialized = True     
[8332]75           
[8323]76        self._migration() #see the note on  MolesSessionMiddleware._migration     
[8229]77   
[8077]78    def _migration(self, runMigration = RUN_MIGRATION):
79        if runMigration and not MolesSessionMiddleware._migrationThread.isAlive():
80            #t.setDaemon(False)
81            MolesSessionMiddleware._migrationThread.start()
[8358]82   
[8323]83    def process_request(self, request): 
[8332]84        try:
85            if not MolesSessionMiddleware._epbInitialized:
86                self._doInitialization()
87                 
[8337]88            request.moles3EPB = MolesSessionMiddleware._moles3EPBFactory.createEPB()                   
[8358]89        except exc.DBAPIError, e:
90            # an exception is raised, Connection is invalidated.
91            #if e.connection_invalidated:
92            #    print "Connection was invalidated!"
93            return self.process_exception(request, e)
[8054]94
95    def process_response(self, request, response):
[8332]96        if hasattr(request, "moles3EPB"):           
97            request.moles3EPB.close()
98           
[8054]99        return response
100
101    def process_exception(self, request, exception):
[8358]102        c = {}
103        c['exception'] = exception
104        c.update(csrf(request))
105        return render_to_response('errorTemplate.html', c)       
Note: See TracBrowser for help on using the repository browser.