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