Skip to content

Commit 8a287e8

Browse files
committed
Changed service for Windows. Config initialization moved to __main__ function for logging [PGPRO-4107]
1 parent b985bac commit 8a287e8

File tree

1 file changed

+18
-43
lines changed

1 file changed

+18
-43
lines changed

service_win32.py

Lines changed: 18 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,13 @@
66
import os
77
import sys
88

9-
# import socket
10-
# import time
119
import logging
1210

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
24-
2511
from threading import Thread
2612
from mamonsu.lib.config import Config
2713
from mamonsu.lib.supervisor import Supervisor
2814

29-
# logging.basicConfig(
30-
# filename = 'c:\\Temp\\hello-service.log',
31-
# level = logging.DEBUG,
32-
# format = '[helloworld-service] %(levelname)-7.7s %(message)s'
33-
# )
15+
config = None
3416

3517
class MamonsuSvc(win32serviceutil.ServiceFramework):
3618

@@ -49,38 +31,20 @@ def SvcStop(self):
4931

5032
def SvcDoRun(self):
5133

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__))
60-
61-
logging.info("exe_dir="+exe_dir)
62-
6334
win32evtlogutil.ReportEvent(
6435
self._svc_name_,
6536
servicemanager.PYS_SERVICE_STARTED,
6637
0,
6738
servicemanager.EVENTLOG_INFORMATION_TYPE,
6839
(self._svc_name_, ''))
6940

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')
7741
supervisor = Supervisor(config)
78-
# win32evtlogutil.ReportEvent(
79-
# self._svc_name_,
80-
# servicemanager.PYS_SERVICE_STOPPED,
81-
# 0,
82-
# servicemanager.EVENTLOG_INFORMATION_TYPE,
83-
# (self._svc_name_, ''))
42+
win32evtlogutil.ReportEvent(
43+
self._svc_name_,
44+
servicemanager.PYS_SERVICE_STOPPED,
45+
0,
46+
servicemanager.EVENTLOG_INFORMATION_TYPE,
47+
(self._svc_name_, ''))
8448

8549
thread = Thread(target=supervisor.start)
8650
thread.daemon = True
@@ -102,6 +66,17 @@ def SvcDoRun(self):
10266
# win32serviceutil.HandleCommandLine(MamonsuSvc)
10367

10468
if __name__ == '__main__':
69+
# determine if application is a script file or frozen exe
70+
if getattr(sys, 'frozen', False):
71+
exe_dir = os.path.dirname(sys.executable)
72+
elif __file__:
73+
# exe_dir = C:\WINDOWS\system32 for service
74+
exe_dir = os.path.dirname(os.path.abspath(__file__))
75+
76+
# initializing in this place for logging
77+
config_file = os.path.join(exe_dir, 'agent.conf')
78+
config = Config(config_file)
79+
10580
if len(sys.argv) == 1:
10681
servicemanager.Initialize()
10782
servicemanager.PrepareToHostSingle(MamonsuSvc)

0 commit comments

Comments
 (0)