Changeset 806
- Timestamp:
- Aug 1, 2008, 1:11:58 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/packages/sipb-xen-base/files/usr/share/python-support/sipb-xen-base/invirt/config.py
r796 r806 16 16 I assume I have the permissions to write to the cache directory. 17 17 """ 18 18 19 # Namespace container for various state variables, so that they can be 19 20 # updated by closures. … … 21 22 22 23 if force_refresh: 23 ns.do_refresh = True24 do_refresh = True 24 25 else: 25 26 src_mtime = getmtime(src_path) 26 27 try: cache_mtime = getmtime(cache_path) 27 except OSError: ns.do_refresh = True28 else: ns.do_refresh = src_mtime > cache_mtime28 except OSError: do_refresh = True 29 else: do_refresh = src_mtime > cache_mtime 29 30 30 if not ns.do_refresh:31 if not do_refresh: 31 32 # Try reading from the cache first. This must be transactionally 32 33 # isolated from concurrent writes to prevent reading an incomplete 33 34 # (changing) version of the data (but the transaction can share the 34 # lock with other concurrent reads). 35 @with_lock_file(lock_file, False) 36 def read_cache(): 37 try: ns.cfg = with_closing(file(cache_path)) ( 35 # lock with other concurrent reads). This isolation is accomplished 36 # using an atomic filesystem rename in the refreshing stage. 37 try: ns.cfg = with_closing(file(cache_path)) ( 38 38 lambda f: json.read(f.read())) 39 except: ns.do_refresh = True39 except: do_refresh = True 40 40 41 if ns.do_refresh:41 if do_refresh: 42 42 # Atomically reload the source and regenerate the cache. The read and 43 43 # write must be a single transaction, or a stale version may be 44 # written. 45 @with_lock_file(lock_file) 46 def refresh_cache(): 47 import yaml 44 # written (if another read/write of a more recent configuration 45 # is interleaved). The final atomic rename is to keep this 46 # transactionally isolated from the above cache read. If we fail to 47 # acquire the lock, just try to load the master configuration. 48 import yaml 49 try: loader = yaml.CSafeLoader 50 except: loader = yaml.SafeLoader 51 try: 52 @with_lock_file(lock_file) 53 def refresh_cache(): 54 ns.cfg = with_closing(file(src_path)) ( 55 lambda f: yaml.load(f, loader)) 56 try: with_closing(file(cache_path + '.tmp', 'w')) ( 57 lambda f: f.write(json.write(ns.cfg))) 58 except: pass # silent failure 59 else: os.rename(cache_path + '.tmp', cache_path) 60 except IOError: 48 61 ns.cfg = with_closing(file(src_path)) ( 49 lambda f: yaml.load(f, yaml.CSafeLoader)) 50 try: with_closing(file(cache_path, 'w')) ( 51 lambda f: f.write(json.write(ns.cfg))) 52 except: pass # silent failure 62 lambda f: yaml.load(f, loader)) 53 63 return ns.cfg 54 64
Note: See TracChangeset
for help on using the changeset viewer.