-
Notifications
You must be signed in to change notification settings - Fork 0
/
HlmService.py
64 lines (52 loc) · 1.83 KB
/
HlmService.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
import servicemanager
import socket
import sys
import win32event
import win32service
import win32serviceutil
import HLM_PV_Import.__main__ as main_
from HLM_PV_Import.settings import Service
from HLM_PV_Import.logger import log_exception, logger
class PVImportService(win32serviceutil.ServiceFramework):
_svc_name_ = Service.NAME
_svc_display_name_ = Service.DISPLAY_NAME
_svc_description_ = Service.DESCRIPTION
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
socket.setdefaulttimeout(60)
# noinspection PyBroadException
# noinspection PyPep8Naming
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
try:
self.stop()
except Exception:
log_exception(*sys.exc_info())
win32event.SetEvent(self.hWaitStop)
self.ReportServiceStatus(win32service.SERVICE_STOPPED)
# noinspection PyBroadException
# noinspection PyPep8Naming
def SvcDoRun(self):
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_, ''))
try:
self.main()
except Exception:
log_exception(*sys.exc_info())
@staticmethod
def main():
logger.info("Starting service")
main_.main()
@staticmethod
def stop():
logger.info("Stop request received")
main_.pv_import.stop()
if __name__ == '__main__':
if len(sys.argv) == 1:
servicemanager.Initialize()
servicemanager.PrepareToHostSingle(PVImportService)
servicemanager.StartServiceCtrlDispatcher()
else:
win32serviceutil.HandleCommandLine(PVImportService)