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 import win32serviceutil
35 if hasattr(sys, 'frozen'):
37 from setup import generate_files
41 class OpenERPServerService(win32serviceutil.ServiceFramework):
43 _svc_name_ = meta.nt_service_name
44 _svc_display_name_ = "%s %s" % (meta.description, meta.serie)
46 def __init__(self, args):
47 win32serviceutil.ServiceFramework.__init__(self, args)
48 # a reference to the server's process
49 self.terpprocess = None
52 # Before we do anything, tell the SCM we are starting the stop process.
53 self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
54 # stop the running OpenERP Server: say it's a normal exit
55 win32api.TerminateProcess(int(self.terpprocess._handle), 0)
56 servicemanager.LogInfoMsg("OpenERP Server stopped correctly")
59 # The server finds now its configuration automatically on Windows
60 # We start the ERP Server as an independent process, but we keep its handle
61 # The server's binary must be one directory above the service's binary (when py2exe'd the python libraries shouldn' mix)
62 service_dir = os.path.dirname(sys.argv[0])
63 server_dir = os.path.split(service_dir)[0]
64 server_path = os.path.join(server_dir, 'server', 'openerp-server.exe')
65 self.terpprocess = subprocess.Popen([server_path], cwd=server_dir, creationflags=win32process.CREATE_NO_WINDOW)
69 servicemanager.LogInfoMsg("OpenERP Server up and running")
70 # exit with same exit code as OpenERP process
71 sys.exit(self.terpprocess.wait())
74 def option_handler(opts):
75 # configure the service to auto restart on failures...
76 subprocess.call(['sc', 'failure', meta.nt_service_name, 'reset=', '0', 'actions=', 'restart/0/restart/0/restart/0'])
78 if __name__ == '__main__':
79 # Do with the service whatever option is passed in the command line
80 win32serviceutil.HandleCommandLine(OpenERPServerService, customOptionHandler=option_handler)
82 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: