Changeset 1392


Ignore:
Timestamp:
02/22/07 16:32:40 (6 years ago)
Author:
mglb1
Message:

Use a lock so that we only have one thread executing the session maintenance
routine at a time. If a second thread tries to run at the same time, exit
immediately. The only scenario for this to happen is if the first thread has
blocked on IO and is waiting for it to complete. So there is no point starting
a second thread which is going to do the same thing...

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ccsd/trunk/crcnetd/_utils/ccsd_session.py

    r1391 r1392  
    870870def sessionMaintenance(*args, **kwargs): 
    871871    """Checks for expired sessions and removes them""" 
    872     start = time.time() 
    873     asession = getSessionE(ADMIN_SESSION_ID) 
    874      
    875     # Loop through the list of sessions and remove expired ones 
    876     for sessionID,session in ccsd_session.sessions.items(): 
    877         # Never expire the admin session 
    878         if sessionID==ADMIN_SESSION_ID: continue 
    879         if session.isExpired(): continue 
    880         # Not expired, update db expiry time if we're expiring "soon" 
    881         if session.lifetime() < MAINT_INTERVAL * 3: 
    882             sql = "UPDATE sessions SET expires=%s WHERE sid=%s" 
    883             res = asession.execute(sql, \ 
    884                     (time.ctime(session.expires), session.session_id)) 
    885  
    886     # Also delete expired login cookies 
    887     session = getSessionE(ADMIN_SESSION_ID) 
    888     sql = "DELETE FROM cookies WHERE expires<NOW()" 
    889     res = session.execute(sql, ()) 
    890     log_debug("Session maintenance completed successfully in %.3f seconds" % \ 
    891             (time.time()-start)) 
    892      
     872    global sessionLock 
     873 
     874    # Ensure that only one thread is executing session maintenance at once 
     875    if not sessionLock.acquire(False): 
     876        log_warn("A previous session maintenance task is still in progress!") 
     877        return 
     878    try: 
     879        ct = threading.currentThread() 
     880        ct.setName("sessionMaintenance") 
     881        start = time.time() 
     882        asession = getSessionE(ADMIN_SESSION_ID) 
     883         
     884        # Loop through the list of sessions and remove expired ones 
     885        for sessionID,session in ccsd_session.sessions.items(): 
     886            # Never expire the admin session 
     887            if sessionID==ADMIN_SESSION_ID: continue 
     888            if session.isExpired(): continue 
     889            # Not expired, update db expiry time if we're expiring "soon" 
     890            if session.lifetime() < MAINT_INTERVAL * 3: 
     891                sql = "UPDATE sessions SET expires=%s WHERE sid=%s" 
     892                res = asession.execute(sql, \ 
     893                        (time.ctime(session.expires), session.session_id)) 
     894 
     895        # Also delete expired login cookies 
     896        session = getSessionE(ADMIN_SESSION_ID) 
     897        sql = "DELETE FROM cookies WHERE expires<NOW()" 
     898        res = session.execute(sql, ()) 
     899        log_debug("Session maintenance completed successfully in %.3f " \ 
     900                "seconds" % (time.time()-start)) 
     901    except: 
     902        log_error("Failed to complete session maintenace", sys.exc_info()) 
     903    sessionLock.release() 
     904 
    893905@catchEvent("shutdown") 
    894906def shutdownSessions(*args, **kwargs): 
     
    11601172##################################################################### 
    11611173def initSessions(): 
     1174    global sessionLock 
    11621175    try: 
    11631176        # Get database connection parameters 
     
    11791192        # Load any other saved sessions 
    11801193        ccsd_session.sessions.update(loadSessions()) 
     1194         
     1195        # Initialise the lock 
     1196        sessionLock = threading.RLock() 
    11811197 
    11821198        log_info("Successfully loaded sessions") 
Note: See TracChangeset for help on using the changeset viewer.