Author: tmckay
Date: 2011-08-29 18:00:52 +0000 (Mon, 29 Aug 2011)
New Revision: 4939
Modified:
trunk/sage/python/sage/wallaby/wallabyoperations.py
Log:
Add try...finally blocks around any locks that are not completely trivial.
Change refresh to take *items
Modified: trunk/sage/python/sage/wallaby/wallabyoperations.py
===================================================================
--- trunk/sage/python/sage/wallaby/wallabyoperations.py 2011-08-29 15:47:22 UTC (rev
4938)
+++ trunk/sage/python/sage/wallaby/wallabyoperations.py 2011-08-29 18:00:52 UTC (rev
4939)
@@ -267,7 +267,7 @@
self._maintain_cache.join(timeout)
log.debug("WallabyOperations: stopped cache maintenance
thread")
- def refresh(self, items=()):
+ def refresh(self, *items):
'''
Wake the caching thread if asleep and cause it to iterate.
@@ -276,21 +276,22 @@
Attributes of WBTypes define valid values for elements of "items"
'''
self._condition.acquire()
- if len(items) == 0:
- do_notify = True
- for attr, val in self._cache.iteritems():
- val.refresh = True
- else:
- do_notify = False
- for attr in items:
- if attr in self._cache:
- do_notify = True
- self._cache[attr].refresh = True
+ try:
+ if len(items) == 0:
+ do_notify = True
+ for attr, val in self._cache.iteritems():
+ val.refresh = True
+ else:
+ do_notify = False
+ for attr in items:
+ if attr in self._cache:
+ do_notify = True
+ self._cache[attr].refresh = True
+ if do_notify:
+ self._condition.notify()
+ finally:
+ self._condition.release()
- if do_notify:
- self._condition.notify()
- self._condition.release()
-
def get_data(self, which):
'''
Return a list of cached values for the specified category.
@@ -303,9 +304,11 @@
'''
d = []
self._lock.acquire()
- if which in self._cache:
- d = self._cache[which].data.values()
- self._lock.release()
+ try:
+ if which in self._cache:
+ d = self._cache[which].data.values()
+ finally:
+ self._lock.release()
return d
def get_names(self, which):
@@ -320,9 +323,11 @@
'''
d = []
self._lock.acquire()
- if which in self._cache:
- d = self._cache[which].data.keys()
- self._lock.release()
+ try:
+ if which in self._cache:
+ d = self._cache[which].data.keys()
+ finally:
+ self._lock.release()
return d
def get_node_by_name(self, name):
@@ -374,9 +379,11 @@
else:
n = tag.name
self._lock.acquire()
- if n in self._nodes_by_tag:
- names = self._nodes_by_tag[n]
- self._lock.release()
+ try:
+ if n in self._nodes_by_tag:
+ names = self._nodes_by_tag[n]
+ finally:
+ self._lock.release()
return names
def get_tag_names(self, node):
@@ -392,9 +399,11 @@
else:
n = node.name
self._lock.acquire()
- if n in self._cache[WBTypes.NODES].data:
- names = self._cache[WBTypes.NODES].data[n].getTags()
- self._lock.release()
+ try:
+ if n in self._cache[WBTypes.NODES].data:
+ names = self._cache[WBTypes.NODES].data[n].getTags()
+ finally:
+ self._lock.release()
return names
def create_tag(self, name):
@@ -405,7 +414,7 @@
'''
try:
self._store.addTag(name)
- self.refresh((WBTypes.GROUPS,WBTypes.TAGS))
+ self.refresh(WBTypes.GROUPS,WBTypes.TAGS)
except Exception, e:
log.debug("WallabyOperations: create_tag, exception suppressed,
%s" % str(e))
return False
@@ -437,29 +446,36 @@
tags -- the new set of tags for the node,
list or tuple of strings
'''
+ status = False
if type(node) in (str, unicode):
n = node
else:
n = node.name
self._lock.acquire()
- if n in self._cache[WBTypes.NODES].data:
- n = self._cache[WBTypes.NODES].data[n]
- self._lock.release()
try:
- # cast to list in case tags is a tuple, list required
- start = time.time()
- n.modifyTags("REPLACE", list(tags), create_missing_tags=True)
- n.update()
- # refresh the groups and tags so that membership informatio
- # is up to date.
- self.refresh(WBTypes.GROUPS,WBTypes.TAGS)
- except Exception, e:
- log.debug("WallabyOperations: edit_tags, exception suppressed,
%s" % str(e))
- return False
- delta = time.time() - start
- log.debug("WallabyOperations: edit_tags %s" % delta)
- return True
+ n = n in self._cache[WBTypes.NODES].data and \
+ self._cache[WBTypes.NODES].data[n] or None
+ finally:
+ self._lock.release()
+ if n is None:
+ log.debug("WallabyOperations: edit_tags, node not found %s" %
str(n))
+ else:
+ try:
+ # cast to list in case tags is a tuple, list required
+ start = time.time()
+ n.modifyTags("REPLACE", list(tags),
create_missing_tags=True)
+ n.update()
+ status = True
+ # refresh the groups and tags so that membership information
+ # is up to date.
+ self.refresh(WBTypes.GROUPS,WBTypes.TAGS)
+ delta = time.time() - start
+ log.debug("WallabyOperations: edit_tags %s" % delta)
+ except Exception, e:
+ log.debug("WallabyOperations: edit_tags, exception suppressed,
%s" % str(e))
+ return status
+
def edit_features(self, group, *features):
'''
Replace existing features in a group with the specified features.
@@ -469,24 +485,30 @@
features -- the new set of features for the group,
list or tuple of strings
'''
+ status = False
if type(group) in (str, unicode):
g = group
else:
g = group.name
self._lock.acquire()
- if g in self._cache[WBTypes.GROUPS].data:
- g = self._cache[WBTypes.GROUPS].data[g]
- self._lock.release()
try:
- start = time.time()
- g.modifyFeatures("REPLACE", features)
- g.update()
- except Exception, e:
- log.debug("WallabyOperations: edit_features, exception suppressed,
%s" % str(e))
- return False
- delta = time.time() - start
- log.debug("WallabyOperations: edit_features %s" % delta)
- return True
+ g = g in self._cache[WBTypes.GROUPS].data and \
+ self._cache[WBTypes.GROUPS].data[g] or None
+ finally:
+ self._lock.release()
+ if g is None:
+ log.debug("WallabyOperations: edit_features, group not found
%s" % str(g))
+ else:
+ try:
+ start = time.time()
+ g.modifyFeatures("REPLACE", features)
+ g.update()
+ status = True
+ delta = time.time() - start
+ log.debug("WallabyOperations: edit_features %s" % delta)
+ except Exception, e:
+ log.debug("WallabyOperations: edit_features, exception
suppressed, %s" % str(e))
+ return status
def set_interval(self, which, refresh):
'''
@@ -552,17 +574,21 @@
nodes_by_tag[g.name] = nodes
self._lock.acquire()
- self._cache[WBTypes.TAGS].data = self._to_dict(tags)
- self._cache[WBTypes.TAGS].reset_remaining(len(tags) == 0)
- self._nodes_by_tag = nodes_by_tag
- self._lock.release()
+ try:
+ self._cache[WBTypes.TAGS].data = self._to_dict(tags)
+ self._cache[WBTypes.TAGS].reset_remaining(len(tags) == 0)
+ self._nodes_by_tag = nodes_by_tag
+ finally:
+ self._lock.release()
log.debug("WallabyOperations: %s list updated (%s items)" %
(WBTypes.TAGS, len(tags)))
def _set_cache(self, attr, data):
self._lock.acquire()
- self._cache[attr].data = self._to_dict(data)
- self._cache[attr].reset_remaining(len(data) == 0)
- self._lock.release()
+ try:
+ self._cache[attr].data = self._to_dict(data)
+ self._cache[attr].reset_remaining(len(data) == 0)
+ finally:
+ self._lock.release()
log.debug("WallabyOperations: %s list updated (%s items)" % (attr,
len(data)))
def _to_dict(self, data):
@@ -571,9 +597,11 @@
def _lookup_by_name(self, which, name):
n = None
self._lock.acquire()
- if name in self._cache[which].data:
- n = self._cache[which].data[name]
- self._lock.release()
+ try:
+ if name in self._cache[which].data:
+ n = self._cache[which].data[name]
+ finally:
+ self._lock.release()
return n
class CacheData(object):