Changed service for Windows. Config initialization moved to __main__ function for logging
@@ -6,31 +6,13 @@ import servicemanager
import os
import sys
-# import socket
-# import time
import logging
-# import socket
-# import os
-# import sys
-# import glob
-# import time
-# import traceback
-# import json
-# import threading
-# import signal
-# import codecs
-# import re
-
from threading import Thread
from mamonsu.lib.config import Config
from mamonsu.lib.supervisor import Supervisor
-# logging.basicConfig(
- # filename = 'c:\\Temp\\hello-service.log',
- # level = logging.DEBUG,
- # format = '[helloworld-service] %(levelname)-7.7s %(message)s'
-# )
+config = None
class MamonsuSvc(win32serviceutil.ServiceFramework):
@@ -49,17 +31,6 @@ class MamonsuSvc(win32serviceutil.ServiceFramework):
def SvcDoRun(self):
-
-
- # determine if application is a script file or frozen exe
- if getattr(sys, 'frozen', False):
- exe_dir = os.path.dirname(sys.executable)
- elif __file__:
- # exe_dir = C:\WINDOWS\system32 for service
- exe_dir = os.path.dirname(os.path.abspath(__file__))
-
- logging.info("exe_dir="+exe_dir)
-
win32evtlogutil.ReportEvent(
self._svc_name_,
servicemanager.PYS_SERVICE_STARTED,
@@ -67,20 +38,13 @@ class MamonsuSvc(win32serviceutil.ServiceFramework):
servicemanager.EVENTLOG_INFORMATION_TYPE,
(self._svc_name_, ''))
- # logging.basicConfig( filename = os.path.join(exe_dir, 'agent.log'), level = logging.DEBUG, format = '[helloworld-service] %(levelname)-7.7s %(message)s' )
-
- # config_file = os.path.join(exe_dir, 'agent_win32.conf')
- config_file = os.path.join(exe_dir, 'agent.conf')
- logging.info(config_file)
- config = Config(config_file)
- # config = Config('c:\\mamonsu\\agent_win32.conf')
supervisor = Supervisor(config)
- # win32evtlogutil.ReportEvent(
- # self._svc_name_,
- # servicemanager.PYS_SERVICE_STOPPED,
- # 0,
- # servicemanager.EVENTLOG_INFORMATION_TYPE,
- # (self._svc_name_, ''))
+ win32evtlogutil.ReportEvent(
+ self._svc_name_,
+ servicemanager.PYS_SERVICE_STOPPED,
+ 0,
+ servicemanager.EVENTLOG_INFORMATION_TYPE,
+ (self._svc_name_, ''))
thread = Thread(target=supervisor.start)
thread.daemon = True
@@ -102,6 +66,17 @@ class MamonsuSvc(win32serviceutil.ServiceFramework):
# win32serviceutil.HandleCommandLine(MamonsuSvc)
if __name__ == '__main__':
+ # determine if application is a script file or frozen exe
+ if getattr(sys, 'frozen', False):
+ exe_dir = os.path.dirname(sys.executable)
+ elif __file__:
+ # exe_dir = C:\WINDOWS\system32 for service
+ exe_dir = os.path.dirname(os.path.abspath(__file__))
+
+ # initializing in this place for logging
+ config_file = os.path.join(exe_dir, 'agent.conf')
+ config = Config(config_file)
+
if len(sys.argv) == 1:
servicemanager.Initialize()
servicemanager.PrepareToHostSingle(MamonsuSvc)
