Ignore:
Timestamp:
Aug 1, 2008, 1:11:58 AM (16 years ago)
Author:
y_z
Message:

just read the master configuration if the cache refresh fails

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  
    1616    I assume I have the permissions to write to the cache directory.
    1717    """
     18
    1819    # Namespace container for various state variables, so that they can be
    1920    # updated by closures.
     
    2122
    2223    if force_refresh:
    23         ns.do_refresh = True
     24        do_refresh = True
    2425    else:
    2526        src_mtime = getmtime(src_path)
    2627        try:            cache_mtime   = getmtime(cache_path)
    27         except OSError: ns.do_refresh = True
    28         else:           ns.do_refresh = src_mtime > cache_mtime
     28        except OSError: do_refresh = True
     29        else:           do_refresh = src_mtime > cache_mtime
    2930
    30     if not ns.do_refresh:
     31    if not do_refresh:
    3132        # Try reading from the cache first.  This must be transactionally
    3233        # isolated from concurrent writes to prevent reading an incomplete
    3334        # (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)) (
    3838                lambda f: json.read(f.read()))
    39             except: ns.do_refresh = True
     39        except: do_refresh = True
    4040
    41     if ns.do_refresh:
     41    if do_refresh:
    4242        # Atomically reload the source and regenerate the cache.  The read and
    4343        # 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:
    4861            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))
    5363    return ns.cfg
    5464
Note: See TracChangeset for help on using the changeset viewer.