Advertisement
Guest User

Untitled

a guest
Apr 12th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.64 KB | None | 0 0
  1. import win32service
  2. import win32serviceutil
  3. import win32event
  4. import psycopg2
  5.  
  6. class PG_Janitor(win32serviceutil.ServiceFramework):
  7.     # you can NET START/STOP the service by the following name
  8.     _svc_name_ = "PG_Janitor"
  9.     # this text shows up as the service name in the Service
  10.     # Control Manager (SCM)
  11.     _svc_display_name_ = "Pulitore connessioni"
  12.     # this text shows up as the description in the SCM
  13.     _svc_description_ = "Elimina le connessioni idle di polyedro"
  14.    
  15.     def __init__(self, args):
  16.         import win32service
  17. import win32serviceutil
  18. import win32event
  19. import psycopg2
  20.  
  21. class PG_Janitor(win32serviceutil.ServiceFramework):
  22.     # you can NET START/STOP the service by the following name
  23.     _svc_name_ = "PG_Janitor"
  24.     # this text shows up as the service name in the Service
  25.     # Control Manager (SCM)
  26.     _svc_display_name_ = "Pulitore connessioni"
  27.     # this text shows up as the description in the SCM
  28.     _svc_description_ = "Elimina le connessioni idle di polyedro"
  29.    
  30.     def __init__(self, args):
  31.        
  32.         self.conn = psycopg2.connect(database='trasferte_anas', host='localhost', user='postgres', password='postgres')
  33.         self.cur = self.conn.cursor()
  34.         win32serviceutil.ServiceFramework.__init__(self,args)
  35.         # create an event to listen for stop requests on
  36.         self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
  37.    
  38.     # core logic of the service  
  39.     def SvcDoRun(self):
  40.         import servicemanager
  41.         # list of services to monitor
  42.         ls_svc = ('TpDB', 'TpHttpd')
  43.         # if this event is fired evebefore we havestarted the first operation, we break
  44.         rc = None
  45.         # check if our list of prerequisites is started
  46.         started = False
  47.         while not started and rc != win32event.WAIT_OBJECT_0:
  48.             # check every 30 seconds, I intentionally "waste" 30 seconds here to err on the safe side
  49.             started = all([win32serviceutil.QueryServiceStatus(svc)[1] == win32service.SERVICE_RUNNING for svc in ls_svc])
  50.             rc = win32event.WaitForSingleObject(self.hWaitStop, 30000)
  51.        
  52.         sql = """\
  53.            SELECT pg_terminate_backend(pid)
  54.            FROM pg_stat_activity
  55.            WHERE datname in ('dbportale', 'trasferte_anas')
  56.                AND pid <> pg_backend_pid()
  57.                AND state = 'idle'
  58.                AND state_change < current_timestamp - INTERVAL '30' MINUTE
  59.            UNION ALL
  60.            SELECT pg_terminate_backend(pid)
  61.            FROM pg_stat_activity
  62.            WHERE datname = 'dbportale'
  63.                AND pid <> pg_backend_pid()
  64.                AND age(clock_timestamp(), query_start) > INTERVAL '120' SECOND
  65.                AND QUERY ILIKE 'SELECT bp098_id, bp098_name, bp098_key, bp098_value, bp098_time, bp098_idses FROM bp098_tbcache WHERE bp098_key = % AND bp098_name = ''licenze_check'' AND bp098_idses  IS NULL'; """        
  66.         # if the stop event hasn't been fired keep looping
  67.         while rc != win32event.WAIT_OBJECT_0:
  68.             self.cur.execute(sql)
  69.             self.conn.commit()
  70.             # block for 2 minutes and listen for a stop event
  71.             rc = win32event.WaitForSingleObject(self.hWaitStop, 120000)
  72.        
  73.         self.conn.commit()
  74.         self.cur.close()
  75.         self.conn.close()
  76.    
  77.     # called when we're being shut down    
  78.     def SvcStop(self):
  79.         # tell the SCM we're shutting down
  80.         self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
  81.         # fire the stop event
  82.         win32event.SetEvent(self.hWaitStop)
  83.        
  84. if __name__ == '__main__':
  85.     win32serviceutil.HandleCommandLine(PG_Janitor)
  86.         self.conn = psycopg2.connect(database='trasferte_anas', host='localhost', user='postgres', password='postgres')
  87.         self.cur = conn.cursor()
  88.         win32serviceutil.ServiceFramework.__init__(self,args)
  89.         # create an event to listen for stop requests on
  90.         self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
  91.    
  92.     # core logic of the service  
  93.     def SvcDoRun(self):
  94.         import servicemanager
  95.        
  96.         rc = None
  97.         sql = """\
  98.            SELECT pg_terminate_backend(pid)
  99.            FROM pg_stat_activity
  100.            WHERE datname in ('dbportale', 'trasferte_anas')
  101.                AND pid <> pg_backend_pid()
  102.                AND state = 'idle'
  103.                AND state_change < current_timestamp - INTERVAL '10' MINUTE
  104.            UNION ALL
  105.            SELECT pg_terminate_backend(pid)
  106.            FROM pg_stat_activity
  107.            WHERE datname = 'dbportale'
  108.                AND pid <> pg_backend_pid()
  109.                AND age(clock_timestamp(), query_start) > INTERVAL '120' SECOND
  110.                AND QUERY ILIKE 'SELECT bp098_id, bp098_name, bp098_key, bp098_value, bp098_time, bp098_idses FROM bp098_tbcache WHERE bp098_key = % AND bp098_name = ''licenze_check'' AND bp098_idses  IS NULL'; """        
  111.         # if the stop event hasn't been fired keep looping
  112.         while rc != win32event.WAIT_OBJECT_0:
  113.             self.cur.execute(sql)
  114.             self.conn.commit()
  115.             # block for 60 seconds and listen for a stop event
  116.             rc = win32event.WaitForSingleObject(self.hWaitStop, 60000)
  117.        
  118.         self.conn.commit()
  119.         self.cur.close()
  120.         self.conn.close()
  121.    
  122.     # called when we're being shut down    
  123.     def SvcStop(self):
  124.         # tell the SCM we're shutting down
  125.         self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
  126.         # fire the stop event
  127.         win32event.SetEvent(self.hWaitStop)
  128.        
  129. if __name__ == '__main__':
  130.     win32serviceutil.HandleCommandLine(PG_Janitor)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement