Changeset 6360


Ignore:
Timestamp:
21/01/10 14:14:38 (10 years ago)
Author:
domlowe
Message:

Added symlink checker thread that runs periodically according to time period set in ini file.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cows/trunk/cows/pylons/config.py

    r6102 r6360  
    1111""" 
    1212 
     13import os, time 
    1314import pylons 
     15from threading import Thread 
     16import logging 
     17log=logging.getLogger(__name__) 
    1418 
    1519def configCSML(): 
     
    2428            k2 = '.'.join(parts[2:]) 
    2529            config[k2] = pylons.config[k] 
     30     
     31    #set up the CSML symlink checker to check all symlinks in the csmlstore 
     32    #Symlink checker is currently set to check hourly but can be modified in the pylons ini file by changing csml.cows.symlink_check_interval (in minutes).   
     33    if config.has_key('symlink_check_interval'): 
     34        minutes=float(config['symlink_check_interval']) 
     35    else: 
     36        #default to hourly checks 
     37        minutes=60 
     38    paths = getCSMLPaths(config['csmlstore']) 
     39    startSymLinkChecker(paths, minutes) 
     40         
     41def getCSMLPaths(csmldirectory):    
     42    paths=[] 
     43    for dirpath, dirnames, filenames in os.walk(csmldirectory): 
     44        for filename in filenames: 
     45            file_id, ext = os.path.splitext(filename) 
     46            if ext in ('.xml', '.csml'): 
     47                paths.append(os.path.join(dirpath, filename)) 
     48    return paths 
     49 
     50def startSymLinkChecker(paths, minutes): 
     51    checkerProcess = SymlinkChecker(paths, minutes) 
     52    checkerProcess.start() 
     53 
     54class SymlinkChecker(Thread): 
     55    ''' This creates a new thread which sleeps most of the time, but occasionally checks that the CSML symlinks are working.''' 
     56    def __init__ (self,paths, sleepminutes): 
     57        Thread.__init__(self) 
     58        self.setDaemon(True) 
     59        self.paths=paths 
     60        self.sleepminutes=sleepminutes  
     61     
     62    def run(self): 
     63        while 1: 
     64            for path in self.paths: 
     65                if os.path.islink(path): 
     66                    log.info('Checking symlink %s'%path) 
     67                    #it's a symlink, get the link path  
     68                    symlinkpath=os.readlink(path) 
     69                    #this may be relative so make sure it isn't 
     70                    fullsymlinkpath=os.path.join(os.path.abspath(os.path.dirname(path) ), symlinkpath) 
     71                    #now check it's not broken. 
     72                    if not os.path.exists(fullsymlinkpath): 
     73                        log.error('INVESTIGATE: Symlink %s is a broken symlink ' % path) 
     74                    else: 
     75                        log.info('Symlink OK: %s '%path) 
     76            time.sleep(self.sleepminutes*60) 
     77         
     78         
     79         
     80                
     81 
     82 
     83     
Note: See TracChangeset for help on using the changeset viewer.