Я создаю сценарий python с непрерывным уведомлением о запросе и пытаюсь создать его как службу windows.
Ниже мой код:
import servicemanager
import socket
import sys
import win32event
import win32service
import win32serviceutil
import cx_Oracle
def notify(message):
print(message)
for query in message.queries:
print(message.queries)
for tab in query.tables:
print("Table:", tab.name)
print("Operation:", tab.operation)
for row in tab.rows:
if row.operation & cx_Oracle.OPCODE_INSERT:
with open('C:\\Users\\John_\\OneDrive\\Documents\\John Joshua\\UBA\\Work\\NIGERIA\\Banquity\\src\\working_fine\\TestService.log', 'a') as f:
f.write(row.rowid)
if row.operation & cx_Oracle.OPCODE_UPDATE:
with open('C:\\Users\\John_\\OneDrive\\Documents\\John Joshua\\UBA\\Work\\NIGERIA\\Banquity\\src\\working_fine\\TestService.log', 'a') as f:
f.write(row.rowid)
class TestService(win32serviceutil.ServiceFramework):
_svc_name_ = "TestService"
_svc_display_name_ = "Test Service"
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
socket.setdefaulttimeout(60)
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
def SvcDoRun(self):
rc = None
while rc != win32event.WAIT_OBJECT_0:
with open('C:\\TestService.log', 'a') as f:
f.write('test service running...\n')
f.write('write')
f.write('Calling Queue')
connection = cx_Oracle.Connection('username/password@hostname:port/ORA_SID', events=True)
subscribe = connection.subscribe(namespace=cx_Oracle.SUBSCR_NAMESPACE_DBCHANGE, callback=notify, operations=cx_Oracle.OPCODE_UPDATE, qos = cx_Oracle.SUBSCR_QOS_QUERY | cx_Oracle.SUBSCR_QOS_ROWIDS| cx_Oracle.SUBSCR_QOS_BEST_EFFORT)
subscribe.registerquery('SELECT * FROM CUSTOM.CARD_PRODUCTS')
input('Wait\n')
rc = win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)
if __name__ == '__main__':
if len(sys.argv) == 1:
servicemanager.Initialize()
servicemanager.PrepareToHostSingle(TestService)
servicemanager.StartServiceCtrlDispatcher()
else:
win32serviceutil.HandleCommandLine(TestService)
При запуске службы windows она просто запускается и останавливается. Любые указатели, чтобы сделать это как услугу, были бы полезны. Спасибо.