loopquery.py
changeset 65 ea79c89e9517
parent 62 af637235ca81
child 66 3cd28308d67b
--- a/loopquery.py	Thu Jan 31 13:41:48 2013 +0100
+++ b/loopquery.py	Tue Feb 05 11:26:18 2013 +0100
@@ -18,7 +18,7 @@
         toolbase.ToolBase.__init__(self, name='loopquery', desc='Run query in loop.')
         self.parser.add_argument('target', nargs='?', metavar='target', type=str, help='Target database')
         self.parser.add_argument('-c', dest='config', type=str, help='Additional config file (besides pgtoolkit.conf).')
-        self.parser.add_argument('-q', dest='queries', nargs='*', help='Queries to run.')
+        self.parser.add_argument('-q', dest='queries', metavar='QUERY', nargs='*', help='Queries to run.')
         self.parser.add_argument('--mins', dest='delay_mins', type=int, help='Delay between queries in minutes.')
         self.parser.add_argument('--secs', dest='delay_secs', type=int, help='Delay between queries in seconds.')
 
@@ -37,7 +37,7 @@
         if self.args.config:
             self.config.load(self.args.config)
         self.queries = self.args.queries or self.config.queries
-        self.delay_mins = self.config.delay_mins
+        self.delay_mins = self.args.delay_mins or self.config.delay_mins
         self.delay_secs = self.args.delay_secs or self.config.delay_secs
         if self.config.log_path:
             self.init_file_logs(self.config.log_path)
@@ -57,12 +57,12 @@
         logging.getLogger('pgmanager_notices').addHandler(handler)
 
     def main(self):
-        self.prepare()
+        self.reset()
         while True:
             self.wait()
             self.action()
 
-    def prepare(self):
+    def reset(self):
         """Check current time, set next action time."""
         dt = datetime.today()
         dt = dt.replace(second = 0, microsecond = 0)
@@ -79,6 +79,10 @@
             time.sleep(td.seconds + td.microseconds/1e6)
         self.next_action_time += timedelta(minutes = self.delay_mins,
                                            seconds = self.delay_secs)
+        # in case that action took too long and next planned time would
+        # be in past -> reset planner
+        if self.next_action_time < now:
+            self.reset()
 
     def action(self):
         """Execute the queries."""