loopquery.py
changeset 65 ea79c89e9517
parent 62 af637235ca81
child 66 3cd28308d67b
equal deleted inserted replaced
64:687e18e5ca93 65:ea79c89e9517
    16 class LoopQueryTool(toolbase.ToolBase):
    16 class LoopQueryTool(toolbase.ToolBase):
    17     def __init__(self):
    17     def __init__(self):
    18         toolbase.ToolBase.__init__(self, name='loopquery', desc='Run query in loop.')
    18         toolbase.ToolBase.__init__(self, name='loopquery', desc='Run query in loop.')
    19         self.parser.add_argument('target', nargs='?', metavar='target', type=str, help='Target database')
    19         self.parser.add_argument('target', nargs='?', metavar='target', type=str, help='Target database')
    20         self.parser.add_argument('-c', dest='config', type=str, help='Additional config file (besides pgtoolkit.conf).')
    20         self.parser.add_argument('-c', dest='config', type=str, help='Additional config file (besides pgtoolkit.conf).')
    21         self.parser.add_argument('-q', dest='queries', nargs='*', help='Queries to run.')
    21         self.parser.add_argument('-q', dest='queries', metavar='QUERY', nargs='*', help='Queries to run.')
    22         self.parser.add_argument('--mins', dest='delay_mins', type=int, help='Delay between queries in minutes.')
    22         self.parser.add_argument('--mins', dest='delay_mins', type=int, help='Delay between queries in minutes.')
    23         self.parser.add_argument('--secs', dest='delay_secs', type=int, help='Delay between queries in seconds.')
    23         self.parser.add_argument('--secs', dest='delay_secs', type=int, help='Delay between queries in seconds.')
    24 
    24 
    25         self.config.add_argument('target', type=str, default=None)
    25         self.config.add_argument('target', type=str, default=None)
    26         self.config.add_argument('queries', type=list, default=[])
    26         self.config.add_argument('queries', type=list, default=[])
    35     def init(self):
    35     def init(self):
    36         toolbase.ToolBase.init(self)
    36         toolbase.ToolBase.init(self)
    37         if self.args.config:
    37         if self.args.config:
    38             self.config.load(self.args.config)
    38             self.config.load(self.args.config)
    39         self.queries = self.args.queries or self.config.queries
    39         self.queries = self.args.queries or self.config.queries
    40         self.delay_mins = self.config.delay_mins
    40         self.delay_mins = self.args.delay_mins or self.config.delay_mins
    41         self.delay_secs = self.args.delay_secs or self.config.delay_secs
    41         self.delay_secs = self.args.delay_secs or self.config.delay_secs
    42         if self.config.log_path:
    42         if self.config.log_path:
    43             self.init_file_logs(self.config.log_path)
    43             self.init_file_logs(self.config.log_path)
    44         self.prepare_conns(target = self.args.target or self.config.target)
    44         self.prepare_conns(target = self.args.target or self.config.target)
    45 
    45 
    55         handler.setFormatter(format)
    55         handler.setFormatter(format)
    56         handler.setLevel(logging.DEBUG)
    56         handler.setLevel(logging.DEBUG)
    57         logging.getLogger('pgmanager_notices').addHandler(handler)
    57         logging.getLogger('pgmanager_notices').addHandler(handler)
    58 
    58 
    59     def main(self):
    59     def main(self):
    60         self.prepare()
    60         self.reset()
    61         while True:
    61         while True:
    62             self.wait()
    62             self.wait()
    63             self.action()
    63             self.action()
    64 
    64 
    65     def prepare(self):
    65     def reset(self):
    66         """Check current time, set next action time."""
    66         """Check current time, set next action time."""
    67         dt = datetime.today()
    67         dt = datetime.today()
    68         dt = dt.replace(second = 0, microsecond = 0)
    68         dt = dt.replace(second = 0, microsecond = 0)
    69         self.next_action_time = dt + timedelta(minutes = self.delay_mins,
    69         self.next_action_time = dt + timedelta(minutes = self.delay_mins,
    70                                                seconds = self.delay_secs)
    70                                                seconds = self.delay_secs)
    77             td = self.next_action_time - now
    77             td = self.next_action_time - now
    78             self.log.debug('Sleep %ds', td.seconds + td.microseconds/1e6)
    78             self.log.debug('Sleep %ds', td.seconds + td.microseconds/1e6)
    79             time.sleep(td.seconds + td.microseconds/1e6)
    79             time.sleep(td.seconds + td.microseconds/1e6)
    80         self.next_action_time += timedelta(minutes = self.delay_mins,
    80         self.next_action_time += timedelta(minutes = self.delay_mins,
    81                                            seconds = self.delay_secs)
    81                                            seconds = self.delay_secs)
       
    82         # in case that action took too long and next planned time would
       
    83         # be in past -> reset planner
       
    84         if self.next_action_time < now:
       
    85             self.reset()
    82 
    86 
    83     def action(self):
    87     def action(self):
    84         """Execute the queries."""
    88         """Execute the queries."""
    85         for q in self.queries:
    89         for q in self.queries:
    86             self.log.info('%s', q)
    90             self.log.info('%s', q)