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

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

Defined a better procedure if the DB connection is lost
Defined a very very basic error page

Line 
1'''
2Created on 9 Jan 2012
3
4@author: mnagni
5'''
6from MolesManager.moles3epb import Moles3EPBFactory
7from libs.migration.client import MigrationThread
8from MolesManager.settings import RUN_MIGRATION, MIGRATION_INTERVAL,\
9    MIGRATION_DB_CONNECTION, MIGRATION_DB_SCRIPT, MOLES3_DB_CONNECTION,\
10    MOLES3_DB_SCRIPT, EVENTS_DB, INFO_DB_CONNECTION
11from libs.commons_db import DbManager
12from libs.migration.InfodbEPB import InfodbEPBFactory
13from libs.migration.MigrationEPB import MigrationEPBFactory
14from sqlalchemy.exc import SQLAlchemyError
15from django.core.context_processors import csrf
16from django.shortcuts import render_to_response
17
18
19
20class MolesSessionMiddleware(object):
21   
22    #This attribute should be not here.
23    #unfortunately I cannot find any start/stop signals from Django
24    #_migrationThread = MigrationThread(interval = MIGRATION_INTERVAL)
25    _epbInitialized = False
26    _infodbEPBFactory = None   
27    _moles3EPBFactory = None
28    _migrationEPBFactory = None
29   
30    def _doInitialization(self):
31
32        infoDB = DbManager(INFO_DB_CONNECTION)
33        MolesSessionMiddleware._infodbEPBFactory = InfodbEPBFactory(infoDB)
34        #InfodbEPB.overrrideDBManager(infoDB)
35           
36        migrationDB = DbManager(MIGRATION_DB_CONNECTION, MIGRATION_DB_SCRIPT)
37        MolesSessionMiddleware._migrationEPBFactory = MigrationEPBFactory(migrationDB)               
38
39        molesDB = DbManager(MOLES3_DB_CONNECTION, MOLES3_DB_SCRIPT, session_event_manager=EVENTS_DB)
40        MolesSessionMiddleware._moles3EPBFactory = Moles3EPBFactory(molesDB)
41       
42        MolesSessionMiddleware._migrationThread = MigrationThread( \
43            MolesSessionMiddleware._moles3EPBFactory.createMoles3EPB(), \
44            MolesSessionMiddleware._migrationEPBFactory.createMigrationEPB(), \
45            interval = MIGRATION_INTERVAL )
46        MolesSessionMiddleware._epbInitialized = True     
47           
48        self._migration() #see the note on  MolesSessionMiddleware._migration     
49   
50    """
51    def _getMolesSession(self):
52        '''
53            @deprecated: db session is going to be removed from all GUI-related instances
54        '''
55        if not MolesSessionMiddleware._epbInitialized:
56            self._doInitialization()
57
58        return self._getNewMolesSession()
59    """
60   
61    def _migration(self, runMigration = RUN_MIGRATION):
62        if runMigration and not MolesSessionMiddleware._migrationThread.isAlive():
63            #t.setDaemon(False)
64            MolesSessionMiddleware._migrationThread.start()
65           
66    """
67        Represents the access to the Moles database.
68        Creates from the existing db connections pools a new session
69        to be used by all the DB operations involved in the actual HTTPRequest
70    """ 
71   
72    def process_request(self, request): 
73        try:
74            if not MolesSessionMiddleware._epbInitialized:
75                self._doInitialization()
76                 
77            request.moles3EPB = MolesSessionMiddleware._moles3EPBFactory.createMoles3EPB()                   
78        except SQLAlchemyError as ex:
79            MolesSessionMiddleware._epbInitialized = False
80            return self.process_exception(request, ex)
81
82    def process_response(self, request, response):
83        if hasattr(request, "moles3EPB"):           
84            request.moles3EPB.close()
85           
86        return response
87
88    def process_exception(self, request, exception):
89        try:
90            c = {}
91            c['exception'] = exception
92            c.update(csrf(request))
93            return render_to_response('errorTemplate.html', c)
94        except SQLAlchemyError as ex:
95            MolesSessionMiddleware._epbInitialized = False           
96       
Note: See TracBrowser for help on using the repository browser.