loopquery.py
changeset 51 bdc44f96cb0b
child 62 af637235ca81
equal deleted inserted replaced
50:f71d3abbb18f 51:bdc44f96cb0b
       
     1 #!/usr/bin/env python3.2
       
     2 """
       
     3 loopquery
       
     4 
       
     5 Execute some query in loop, waiting for some time interval before next run.
       
     6 
       
     7 """
       
     8 
       
     9 from pgtoolkit import toolbase
       
    10 
       
    11 import time
       
    12 
       
    13 
       
    14 class LoopQueryTool(toolbase.SimpleTool):
       
    15     def __init__(self):
       
    16         toolbase.SimpleTool.__init__(self, name='loopquery', desc='Run query in loop.')
       
    17         self.parser.add_argument('-q', dest='query', type=str, help='Query to run.')
       
    18         self.parser.add_argument('-i', dest='interval', type=float, help='Run interval in seconds.')
       
    19         self.init()
       
    20 
       
    21     def main(self):
       
    22         while True:
       
    23             self.log.info('Executing: %s', self.args.query)
       
    24             with self.pgm.cursor('target') as curs:
       
    25                 curs.execute(self.args.query)
       
    26             self.log.info('Done.')
       
    27             time.sleep(self.args.interval)
       
    28 
       
    29 
       
    30 tool = LoopQueryTool()
       
    31 tool.main()
       
    32