From 50116fa6bb87a394a1a5ab6bb13785bf06ba142a Mon Sep 17 00:00:00 2001 From: Rudi Grinberg Date: Mon, 23 Jul 2012 14:14:17 -0400 Subject: [PATCH] cc-4105: Added toucher thread to update last ran time --- .../media-monitor2/media/monitor/toucher.py | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 python_apps/media-monitor2/media/monitor/toucher.py diff --git a/python_apps/media-monitor2/media/monitor/toucher.py b/python_apps/media-monitor2/media/monitor/toucher.py new file mode 100644 index 000000000..39c6d159b --- /dev/null +++ b/python_apps/media-monitor2/media/monitor/toucher.py @@ -0,0 +1,76 @@ +# -*- coding: utf-8 -*- +import media.monitor.pure as mmp +import os +from media.monitor.log import Loggable +from media.monitor.exceptions import CouldNotCreateIndexFile + +class Toucher(Loggable): + def __init__(self,path): + self.path = path + def __call__(self): + try: mmp.fondle(self.path) + except Exception as e: + self.logger.info("Failed to touch file: '%s'. Logging exception." % self.path) + self.logger.info(str(e)) + +""" +http://code.activestate.com/lists/python-ideas/8982/ +""" +from datetime import datetime + +import threading + +class RepeatTimer(threading.Thread): + def __init__(self, interval, callable, args=[], kwargs={}): + threading.Thread.__init__(self) + # interval_current shows number of milliseconds in currently triggered + self.interval_current = interval + # interval_new shows number of milliseconds for next + self.interval_new = interval + self.callable = callable + self.args = args + self.kwargs = kwargs + self.event = threading.Event() + self.event.set() + self.activation_dt = None + self.__timer = None + + def run(self): + while self.event.is_set(): + self.activation_dt = datetime.utcnow() + self.__timer = threading.Timer(self.interval_new, + self.callable, + self.args, + self.kwargs) + self.interval_current = self.interval_new + self.__timer.start() + self.__timer.join() + + def cancel(self): + self.event.clear() + if self.__timer is not None: + self.__timer.cancel() + + def trigger(self): + self.callable(*self.args, **self.kwargs) + if self.__timer is not None: + self.__timer.cancel() + + def change_interval(self, value): + self.interval_new = value + + +class ToucherThread(Loggable): + """docstring for ToucherThread""" + def __init__(self, path, interval=5): + if not os.path.exists(path): + try: + f = open(path,'w') + f.write('') + f.close() + except Exception as e: + raise CouldNotCreateIndexFile(path,e) + cb = Toucher(path) + t = RepeatTimer(interval, cb) + t.start() +