Adapt error message accordingly.
[odoo/odoo.git] / win32 / OpenERPServerService.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 #
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.
11 #
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.
16 #
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/>.
19 #
20 ##############################################################################
21
22 import win32serviceutil
23 import win32service
24 import win32api
25 import win32process
26 import servicemanager
27
28 import sys
29 import subprocess
30 import os
31
32 try:
33     import meta
34 except ImportError:
35     if hasattr(sys, 'frozen'):
36         raise
37     from setup import generate_files
38     generate_files()
39     import meta     # noqa
40
41 class OpenERPServerService(win32serviceutil.ServiceFramework):
42     # required info
43     _svc_name_ = meta.nt_service_name
44     _svc_display_name_ = "%s %s" % (meta.description, meta.serie)
45
46     def __init__(self, args):
47         win32serviceutil.ServiceFramework.__init__(self, args)
48         # a reference to the server's process
49         self.terpprocess = None
50
51     def SvcStop(self):
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")
57
58     def StartTERP(self):
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)
66
67     def SvcDoRun(self):
68         self.StartTERP()
69         servicemanager.LogInfoMsg("OpenERP Server up and running")
70         # exit with same exit code as OpenERP process
71         sys.exit(self.terpprocess.wait())
72
73
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'])
77
78 if __name__ == '__main__':
79     # Do with the service whatever option is passed in the command line
80     win32serviceutil.HandleCommandLine(OpenERPServerService, customOptionHandler=option_handler)
81
82 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: