Python Win32 service doesnt start except in debug mode

Viewed 47

I have a Django app that I'm trying to run via Cherrypy on Windows Server. I have below code in the service creation script.

import os
import sys

service_directory = os.path.dirname(__file__)
source_directory = os.path.abspath(service_directory)
os.chdir(source_directory)
venv_base = os.path.abspath(os.path.join(source_directory, ".venv"))
print(venv_base)
sys.path.append(".")
old_os_path = os.environ['PATH']
os.environ['PATH'] = os.path.join(venv_base, "Scripts")+ os.pathsep + old_os_path
site_packages = os.path.join(venv_base, "Lib", "site-packages")
prev_sys_path = list(sys.path)
import site
site.addsitedir(site_packages)
sys.real_prefix = sys.prefix
sys.prefix = venv_base
new_sys_path = list()
for item in list(sys.path):
    if item not in prev_sys_path:
        new_sys_path.append(item)
        sys.path.remove(item)
sys.path[:0] = new_sys_path

import pathlib
import subprocess
import traceback
import servicemanager
import win32event
import win32service
import win32serviceutil
from win32api import SetConsoleCtrlHandler

class AppServerSvc(win32serviceutil.ServiceFramework):
    """
    Service Create class
    """
    _svc_name_ = "MyService"  # service name
    _svc_display_name_ = "MyService"  # display name

    def __init__(self, args):
        """
        Constructor
        """
        win32serviceutil.ServiceFramework.__init__(self, args)
        SetConsoleCtrlHandler(lambda x: True, True)
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)

    def SvcStop(self):
        """
        Function to stop the windows service
        """
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        win32event.SetEvent(self.hWaitStop)
        self.proc.terminate()

    def SvcDoRun(self):
        """
        Function to create and start the windows service
        """
        self.ReportServiceStatus(win32service.SERVICE_START_PENDING)
        servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
                              servicemanager.PYS_SERVICE_STARTED,
                              (self._svc_name_, ''))
        try:
            self.main()

        except Exception as exp_msg:
            servicemanager.LogErrorMsg(traceback.format_exc())
            os._exit(-1)

    def main(self):
        """
        Script to run as the windows service
        """
        #env_file.load(f'{os.path.join(pathlib.Path(__file__).parent.absolute(), "config.env")}')
        #settings.set_env_variables()
        self.ReportServiceStatus(win32service.SERVICE_RUNNING)
        self.proc = subprocess.Popen(f"python"
                                     f" {os.path.join(pathlib.Path(__file__).parent.absolute(), 'cherryd.py')} 1"
                                     , stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
                                     stdin=subprocess.DEVNULL)
        stdout, stderr = self.proc.communicate()
        print(stdout)
        if (self.proc.poll() == 0):
            servicemanager.LogMsg(f"Successfully started the process with PID: {self.proc.pid}")
        else:
            servicemanager.LogErrorMsg(f"Failed to start the services")


if __name__ == '__main__':
    # Start of the process
    win32serviceutil.HandleCommandLine(AppServerSvc)

I'm able to install this service using python service.py install, no issues here. When I start the service python service.py start I get error Error starting service: The service did not respond to the start or control request in a timely fashion.

When I do python service.py debug, everything works normal and I'm able to access the app using FQDN. Why does the service not work when started normally and work only during debug mode.

Have read multiple posts on similar errors but nothing helped. I'm using Python 3.8 and have added the venv Scripts directory to PATH as well. Can someone please help.

Update Started the service with my user account and it worked, still don't understand how it worked in debug mode.

0 Answers
Related