2 # TinyERPServerService.py
\r
4 # Script installing Tiny ERP as Windows service
\r
6 # Win32 python extensions modules
\r
7 import win32serviceutil
\r
12 import servicemanager
\r
19 class TinyERPServerService(win32serviceutil.ServiceFramework):
\r
21 _svc_name_ = "tinyerp-service"
\r
22 _svc_display_name_ = "Tiny ERP Server"
\r
24 _svc_description_ = "Tiny ERP Server service"
\r
26 def __init__(self, args):
\r
27 win32serviceutil.ServiceFramework.__init__(self, args)
\r
28 # Create an event which we will use to wait on.
\r
29 # The "service stop" request will set this event.
\r
30 self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
\r
31 # a reference to the server's process
\r
32 self.terpprocess = None
\r
33 # info if the service terminates correctly or if the server crashed
\r
34 self.stopping = False
\r
38 # Before we do anything, tell the SCM we are starting the stop process.
\r
39 self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
\r
40 # stop the running TERP Server: say it's a normal exit
\r
41 win32api.TerminateProcess(int(self.terpprocess._handle), 0)
\r
42 servicemanager.LogInfoMsg("Tiny ERP Server stopped correctly")
\r
44 win32event.SetEvent(self.hWaitStop)
\r
47 def StartTERP(self):
\r
48 # The server finds now its configuration automatically on Windows
\r
49 # We start the ERP Server as an independent process, but we keep its handle
\r
50 # The server's binary must be one directory above the service's binary (when py2exe'd the python libraries shouldn' mix)
\r
51 service_dir = os.path.dirname(sys.argv[0])
\r
52 server_dir = os.path.split(service_dir)[0]
\r
53 server_path = os.path.join(server_dir, 'tinyerp-server.exe')
\r
54 self.terpprocess = subprocess.Popen([server_path], \
\r
56 creationflags=win32process.CREATE_NO_WINDOW)
\r
59 def StartControl(self,ws):
\r
60 # this listens to the Service Manager's events
\r
61 win32event.WaitForSingleObject(ws, win32event.INFINITE)
\r
62 self.stopping = True
\r
65 # Start Tiny ERP Server itself
\r
67 # start the loop waiting for the Service Manager's stop signal
\r
68 thread.start_new_thread(self.StartControl, (self.hWaitStop,))
\r
69 # Log a info message that the server is running
\r
70 servicemanager.LogInfoMsg("Tiny ERP Server up and running")
\r
71 # verification if the server is really running, else quit with an error
\r
72 self.terpprocess.wait()
\r
73 if not self.stopping:
\r
74 sys.exit("Tiny ERP Server check: server not running, check the logfile for more info")
\r
78 if __name__=='__main__':
\r
79 # Do with the service whatever option is passed in the command line
\r
80 win32serviceutil.HandleCommandLine(TinyERPServerService)
\r