Skip to content

Commit 57b3b18

Browse files
committed
BUILD: changed service for windows , PGPRO-4107
1 parent 7548e15 commit 57b3b18

File tree

1 file changed

+61
-21
lines changed

1 file changed

+61
-21
lines changed

service_win32.py

Lines changed: 61 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,36 @@
22
import win32service
33
import win32event
44
import win32evtlogutil
5-
#import servicemanager
5+
import servicemanager
66
import os
7+
import sys
8+
9+
# import socket
10+
# import time
11+
import logging
12+
13+
# import socket
14+
# import os
15+
# import sys
16+
# import glob
17+
# import time
18+
# import traceback
19+
# import json
20+
# import threading
21+
# import signal
22+
# import codecs
23+
# import re
724

825
from threading import Thread
926
from mamonsu.lib.config import Config
1027
from mamonsu.lib.supervisor import Supervisor
1128

29+
# logging.basicConfig(
30+
# filename = 'c:\\Temp\\hello-service.log',
31+
# level = logging.DEBUG,
32+
# format = '[helloworld-service] %(levelname)-7.7s %(message)s'
33+
# )
34+
1235
class MamonsuSvc(win32serviceutil.ServiceFramework):
1336

1437
_svc_name_ = 'mamonsu'
@@ -26,21 +49,31 @@ def SvcStop(self):
2649

2750
def SvcDoRun(self):
2851

29-
# __file__ == 'service_win32.py'
30-
exe_dir = os.path.dirname(os.path.dirname(__file__))
31-
os.chdir(exe_dir)
52+
53+
54+
# determine if application is a script file or frozen exe
55+
if getattr(sys, 'frozen', False):
56+
exe_dir = os.path.dirname(sys.executable)
57+
elif __file__:
58+
# exe_dir = C:\WINDOWS\system32 for service
59+
exe_dir = os.path.dirname(os.path.abspath(__file__))
3260

33-
# win32evtlogutil.ReportEvent(
34-
# self._svc_name_,
35-
# servicemanager.PYS_SERVICE_STARTED,
36-
# 0,
37-
# servicemanager.EVENTLOG_INFORMATION_TYPE,
38-
# (self._svc_name_, ''))
61+
logging.info("exe_dir="+exe_dir)
3962

40-
config_file = os.path.join(exe_dir, 'agent_win32.conf')
41-
# config = Config(config_file)
42-
config = Config('c:\\mamonsu\\agent_win32.conf')
63+
win32evtlogutil.ReportEvent(
64+
self._svc_name_,
65+
servicemanager.PYS_SERVICE_STARTED,
66+
0,
67+
servicemanager.EVENTLOG_INFORMATION_TYPE,
68+
(self._svc_name_, ''))
4369

70+
# logging.basicConfig( filename = os.path.join(exe_dir, 'agent.log'), level = logging.DEBUG, format = '[helloworld-service] %(levelname)-7.7s %(message)s' )
71+
72+
# config_file = os.path.join(exe_dir, 'agent_win32.conf')
73+
config_file = os.path.join(exe_dir, 'agent.conf')
74+
logging.info(config_file)
75+
config = Config(config_file)
76+
# config = Config('c:\\mamonsu\\agent_win32.conf')
4477
supervisor = Supervisor(config)
4578
# win32evtlogutil.ReportEvent(
4679
# self._svc_name_,
@@ -52,19 +85,26 @@ def SvcDoRun(self):
5285
thread = Thread(target=supervisor.start)
5386
thread.daemon = True
5487
thread.start()
55-
5688
while True:
5789
rc = win32event.WaitForSingleObject(
5890
self.hWaitStop, win32event.INFINITE)
5991
if rc == win32event.WAIT_OBJECT_0:
60-
# win32evtlogutil.ReportEvent(
61-
# self._svc_name_,
62-
# servicemanager.PYS_SERVICE_STOPPED,
63-
# 0,
64-
# servicemanager.EVENTLOG_INFORMATION_TYPE,
65-
# (self._svc_name_, ''))
92+
win32evtlogutil.ReportEvent(
93+
self._svc_name_,
94+
servicemanager.PYS_SERVICE_STOPPED,
95+
0,
96+
servicemanager.EVENTLOG_INFORMATION_TYPE,
97+
(self._svc_name_, ''))
6698
break
6799

68100

101+
#if __name__ == '__main__':
102+
# win32serviceutil.HandleCommandLine(MamonsuSvc)
103+
69104
if __name__ == '__main__':
70-
win32serviceutil.HandleCommandLine(MamonsuSvc)
105+
if len(sys.argv) == 1:
106+
servicemanager.Initialize()
107+
servicemanager.PrepareToHostSingle(MamonsuSvc)
108+
servicemanager.StartServiceCtrlDispatcher()
109+
else:
110+
win32serviceutil.HandleCommandLine(MamonsuSvc)

0 commit comments

Comments
 (0)