source: cows/trunk/cows/pylons/config.py @ 6360

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cows/trunk/cows/pylons/config.py@6360
Revision 6360, 2.7 KB checked in by domlowe, 11 years ago (diff)

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

Line 
1# BSD Licence
2# Copyright (c) 2009, Science & Technology Facilities Council (STFC)
3# All rights reserved.
4#
5# See the LICENSE file in the source distribution of this software for
6# the full license text.
7
8"""
9Miscellaneous utility functions.
10
11"""
12
13import os, time
14import pylons
15from threading import Thread
16import logging
17log=logging.getLogger(__name__)
18
19def configCSML():
20    """Initialise the configuration of the CSML backend from pylons.config.
21
22    """
23    from cows.service.imps.csmlbackend.config import config
24   
25    for k in pylons.config:
26        parts = k.split('.')
27        if parts[:2] == ['cows', 'csml']:
28            k2 = '.'.join(parts[2:])
29            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 TracBrowser for help on using the repository browser.