# The default interval, when all data is written to disk.
SUBMIT_INTERVAL = 300
- HEARTBEAT = 2
-
def __init__(self):
self.config = configparser.ConfigParser()
self.instances = []
# Indicates whether this process should be running or not.
self.running = True
+ self.timer = plugins.Timer(self.SUBMIT_INTERVAL, heartbeat=2)
# Add all automatic plugins.
self.add_autocreate_plugins()
i.start()
# Regularly submit all data to disk.
- counter = self.SUBMIT_INTERVAL / self.HEARTBEAT
while self.running:
- time.sleep(self.HEARTBEAT)
- counter -= 1
+ self.timer.reset()
- if counter == 0:
+ if self.timer.wait():
self.submit_all()
- counter = self.SUBMIT_INTERVAL / self.HEARTBEAT
# Wait until all instances are finished.
while self.instances:
log.debug(_("Received shutdown signal"))
self.running = False
+ if self.timer:
+ self.timer.cancel()
# Propagating shutdown to all threads.
for i in self.instances:
from ..constants import *
from ..i18n import _
+class Timer(object):
+ def __init__(self, timeout, heartbeat=1):
+ self.timeout = timeout
+ self.heartbeat = heartbeat
+
+ self.reset()
+
+ def reset(self):
+ # Save start time.
+ self.start = time.time()
+
+ # Has this timer been killed?
+ self.killed = False
+
+ @property
+ def elapsed(self):
+ return time.time() - self.start
+
+ def cancel(self):
+ self.killed = True
+
+ def wait(self):
+ while self.elapsed < self.timeout and not self.killed:
+ time.sleep(self.heartbeat)
+
+ return self.elapsed > self.timeout
+
+
class Plugin(threading.Thread):
# The name of this plugin.
name = None
self.log = logging.getLogger("collecty.plugins.%s" % self.name)
self.log.propagate = 1
- # Keepalive options
- self.heartbeat = 2
- self.running = True
-
self.data = []
# Create the database file.
# Run some custom initialization.
self.init()
+ # Keepalive options
+ self.running = True
+ self.timer = Timer(self.interval)
+
self.log.info(_("Successfully initialized."))
def __repr__(self):
def run(self):
self.log.debug(_("Started."))
- counter = 0
while self.running:
- if counter == 0:
+ # Reset the timer.
+ self.timer.reset()
+
+ # Wait until the timer has successfully elapsed.
+ if self.timer.wait():
self.log.debug(_("Collecting..."))
self._read()
- self.log.debug(_("Sleeping for %.4fs.") % self.interval)
-
- counter = self.interval / self.heartbeat
-
- time.sleep(self.heartbeat)
- counter -= 1
-
self._submit()
self.log.debug(_("Stopped."))
self.log.debug(_("Received shutdown signal."))
self.running = False
+ # Kill any running timers.
+ if self.timer:
+ self.timer.cancel()
+
@property
def now(self):
"""