Advertisement
Guest User

Untitled

a guest
Apr 12th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.12 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.        
  17.         self.conn = psycopg2.connect(database='trasferte_anas', host='localhost', user='postgres', password='postgres')
  18.         self.cur = self.conn.cursor()
  19.         win32serviceutil.ServiceFramework.__init__(self,args)
  20.         # create an event to listen for stop requests on
  21.         self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
  22.    
  23.     # core logic of the service  
  24.     def SvcDoRun(self):
  25.         import servicemanager
  26.         # list of services to monitor
  27.         ls_svc = ('TpDB', 'TpHttpd')
  28.         # if this event is fired evebefore we havestarted the first operation, we break
  29.         rc = None
  30.         # check if our list of prerequisites is started
  31.         started = False
  32.         while not started and rc != win32event.WAIT_OBJECT_0:
  33.             # check every 30 seconds, I intentionally "waste" 30 seconds here to err on the safe side
  34.             started = all([win32serviceutil.QueryServiceStatus(svc)[1] == win32service.SERVICE_RUNNING for svc in ls_svc])
  35.             rc = win32event.WaitForSingleObject(self.hWaitStop, 30000)
  36.        
  37.         sql = """\
  38.            SELECT pg_terminate_backend(pid)
  39.            FROM pg_stat_activity
  40.            WHERE datname in ('dbportale', 'trasferte_anas')
  41.                AND pid <> pg_backend_pid()
  42.                AND state = 'idle'
  43.                AND state_change < current_timestamp - INTERVAL '30' MINUTE
  44.            UNION ALL
  45.            SELECT pg_terminate_backend(pid)
  46.            FROM pg_stat_activity
  47.            WHERE datname = 'dbportale'
  48.                AND pid <> pg_backend_pid()
  49.                AND age(clock_timestamp(), query_start) > INTERVAL '120' SECOND
  50.                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'; """        
  51.         # if the stop event hasn't been fired keep looping
  52.         while rc != win32event.WAIT_OBJECT_0:
  53.             self.cur.execute(sql)
  54.             self.conn.commit()
  55.             # block for 2 minutes and listen for a stop event
  56.             rc = win32event.WaitForSingleObject(self.hWaitStop, 120000)
  57.        
  58.         self.conn.commit()
  59.         self.cur.close()
  60.         self.conn.close()
  61.    
  62.     # called when we're being shut down    
  63.     def SvcStop(self):
  64.         # tell the SCM we're shutting down
  65.         self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
  66.         # fire the stop event
  67.         win32event.SetEvent(self.hWaitStop)
  68.        
  69. if __name__ == '__main__':
  70.     win32serviceutil.HandleCommandLine(PG_Janitor)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement