From: Michael Tremer Date: Sat, 12 Dec 2015 23:27:25 +0000 (+0100) Subject: Commit all collected data in the write cache to disk when generating a graph X-Git-Tag: 004~1 X-Git-Url: http://git.ipfire.org/?p=collecty.git;a=commitdiff_plain;h=ca9b922127a96486956622dc1452b6560ceabfd3 Commit all collected data in the write cache to disk when generating a graph Therefore we will always get the latest data drawn in the graph image even when writing data to disk is happening delayed. Signed-off-by: Michael Tremer --- diff --git a/src/collecty/daemon.py b/src/collecty/daemon.py index d4e9b4a..695ca7f 100644 --- a/src/collecty/daemon.py +++ b/src/collecty/daemon.py @@ -381,6 +381,33 @@ class WriteQueue(threading.Thread): self.log.critical(_("Could not update RRD database %s: %s") \ % (filename, e)) + def commit_file(self, filename): + """ + Commits all data that is in the write queue for the given + RRD database. + """ + results, others = [], [] + + # We will have to walk through the entire queue since we cannot + # ready any items selectively. Everything that belongs to our + # transaction is kept. Everything else will be put back into the + # queue. + while not self._queue.empty(): + result = self._queue.get() + + if result.file == filename: + results.append(result) + else: + others.append(result) + + # Put back all items that did not match + for result in others: + self._queue.put(result) + + # Write everything else to disk + if results: + self._commit_file(filename, results) + class QueueObject(object): def __init__(self, file, time, data): diff --git a/src/collecty/plugins/base.py b/src/collecty/plugins/base.py index 04207c7..a4d2d72 100644 --- a/src/collecty/plugins/base.py +++ b/src/collecty/plugins/base.py @@ -454,6 +454,9 @@ class Object(object): # Make sure that the RRD database has been created self.create() + # Write everything to disk that is in the write queue + self.collecty.write_queue.commit_file(self.file) + class GraphTemplate(object): # A unique name to identify this graph template. @@ -578,6 +581,11 @@ class GraphTemplate(object): return files def generate_graph(self, interval=None, **kwargs): + # Make sure that all collected data is in the database + # to get a recent graph image + if self.object: + self.object.commit() + args = self._make_command_line(interval, **kwargs) self.log.info(_("Generating graph %s") % self)