forked from Image-X-Institute/The-Real-Time-Imaging-Database
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwinservice.py
71 lines (60 loc) · 2.57 KB
/
winservice.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import pythoncom
import win32serviceutil
import win32service
import win32event
import servicemanager
import win32api
import socket
import sys
from application import app
import config
import logging
import tempfile
import os
class RealtimeImagingDBService(win32serviceutil.ServiceFramework):
_svc_name_ = "RealtimeImagingDBService"
_svc_display_name_ = "Image-X Real-time Imaging DB"
_svc_description_ = "This service uses RESTful API to serve "\
"information about Clinical data "
def __init__(self, args):
super().__init__(args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
socket.setdefaulttimeout(60)
def log(self, msg):
servicemanager.LogInfoMsg(str(msg))
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
self.ReportServiceStatus(win32service.SERVICE_STOPPED)
logging.info("realtimeImagingDB service stopped")
def SvcDoRun(self):
self.ReportServiceStatus(win32service.SERVICE_START_PENDING)
try:
self.ReportServiceStatus(win32service.SERVICE_RUNNING)
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED, (self._svc_name_, ''))
self.main()
except Exception as ex:
# self.log('Exception while starting service')
logging.warn("caught an exception while trying to start the service - " + str(ex))
self.SvcStop()
def main(self):
rc = None
while rc != win32event.WAIT_OBJECT_0:
logging.info("RealtimeImagingDB Starting service")
app.run (host=config.LISTENING_HOST,
port=config.LISTENING_PORT,
debug=False, use_reloader=False)
rc = win32event.WaitForSingleObject(self.hWaitStop, 24*60*60*1000)
logging.info("Exiting the RealtimeImagingDBService::main()\n")
if __name__ == '__main__':
logfilePath = os.path.join(tempfile.gettempdir(), config.LOGFILE_NAME)
print("logging to ", logfilePath)
logging.basicConfig(filename=logfilePath)
if len(sys.argv) == 1:
servicemanager.Initialize()
servicemanager.PrepareToHostSingle(RealtimeImagingDBService)
servicemanager.StartServiceCtrlDispatcher()
else:
win32api.SetConsoleCtrlHandler(lambda x: True, True)
win32serviceutil.HandleCommandLine(RealtimeImagingDBService)