KERNEL: add win32 compile
[odoo/odoo.git] / win32 / TinyERPServerService.py
1 #\r
2 # TinyERPServerService.py\r
3 #\r
4 # Script installing Tiny ERP as Windows service\r
5 \r
6 # Win32 python extensions modules\r
7 import win32serviceutil\r
8 import win32service\r
9 import win32event\r
10 import win32api\r
11 import win32process\r
12 import servicemanager\r
13 \r
14 import sys\r
15 import subprocess\r
16 import os\r
17 import thread\r
18 \r
19 class TinyERPServerService(win32serviceutil.ServiceFramework):\r
20     # required info\r
21     _svc_name_ = "tinyerp-service"\r
22     _svc_display_name_ = "Tiny ERP Server"\r
23     # optionnal info\r
24     _svc_description_ = "Tiny ERP Server service"\r
25 \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
35 \r
36 \r
37     def SvcStop(self):\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
43         # And set my event.\r
44         win32event.SetEvent(self.hWaitStop)\r
45 \r
46 \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
55                                             cwd=server_dir,\r
56                                             creationflags=win32process.CREATE_NO_WINDOW)\r
57 \r
58 \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
63 \r
64     def SvcDoRun(self):\r
65         # Start Tiny ERP Server itself\r
66         self.StartTERP()\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
75 \r
76 \r
77 \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