[MERGE] Merged Dhruti's branch for the fix of avoiding crash when reservation is...
[odoo/odoo.git] / win32 / OpenERPServerService.py
1 # -*- encoding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution   
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
6 #    $Id$
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU General Public License as published by
10 #    the Free Software Foundation, either version 3 of the License, or
11 #    (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU General Public License for more details.
17 #
18 #    You should have received a copy of the GNU General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23 # Win32 python extensions modules\r
24 import win32serviceutil\r
25 import win32service\r
26 import win32event\r
27 import win32api\r
28 import win32process\r
29 import servicemanager\r
30 \r
31 import sys\r
32 import subprocess\r
33 import os\r
34 import thread\r
35 \r
36 class OpenERPServerService(win32serviceutil.ServiceFramework):\r
37     # required info\r
38     _svc_name_ = "openerp-service"\r
39     _svc_display_name_ = "OpenERP Server"\r
40     # optionnal info\r
41     _svc_description_ = "OpenERP Server service"\r
42 \r
43     def __init__(self, args):\r
44         win32serviceutil.ServiceFramework.__init__(self, args)\r
45         # Create an event which we will use to wait on.\r
46         # The "service stop" request will set this event.\r
47         self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)\r
48         # a reference to the server's process\r
49         self.terpprocess = None\r
50         # info if the service terminates correctly or if the server crashed\r
51         self.stopping = False\r
52 \r
53 \r
54     def SvcStop(self):\r
55         # Before we do anything, tell the SCM we are starting the stop process.\r
56         self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)\r
57         # stop the running TERP Server: say it's a normal exit\r
58         win32api.TerminateProcess(int(self.terpprocess._handle), 0)\r
59         servicemanager.LogInfoMsg("OpenERP Server stopped correctly")\r
60         # And set my event.\r
61         win32event.SetEvent(self.hWaitStop)\r
62 \r
63 \r
64     def StartTERP(self):\r
65         # The server finds now its configuration automatically on Windows\r
66         # We start the ERP Server as an independent process, but we keep its handle\r
67         # The server's binary must be one directory above the service's binary (when py2exe'd the python libraries shouldn' mix)\r
68         service_dir = os.path.dirname(sys.argv[0])\r
69         server_dir = os.path.split(service_dir)[0]\r
70         server_path = os.path.join(server_dir, 'openerp-server.exe')\r
71         self.terpprocess = subprocess.Popen([server_path], cwd=server_dir, creationflags=win32process.CREATE_NO_WINDOW)\r
72 \r
73 \r
74     def StartControl(self,ws):\r
75         # this listens to the Service Manager's events\r
76         win32event.WaitForSingleObject(ws, win32event.INFINITE)\r
77         self.stopping = True\r
78 \r
79     def SvcDoRun(self):
80         # Start OpenERP Server itself\r
81         self.StartTERP()\r
82         # start the loop waiting for the Service Manager's stop signal\r
83         thread.start_new_thread(self.StartControl, (self.hWaitStop,))\r
84         # Log a info message that the server is running\r
85         servicemanager.LogInfoMsg("OpenERP Server up and running")\r
86         # verification if the server is really running, else quit with an error\r
87         self.terpprocess.wait()\r
88         if not self.stopping:\r
89             sys.exit("OpenERP Server check: server not running, check the logfile for more info")\r
90 \r
91 \r
92 \r
93 if __name__=='__main__':\r
94     # Do with the service whatever option is passed in the command line\r
95     win32serviceutil.HandleCommandLine(OpenERPServerService)\r
96
97 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
98