diff options
| author | Christophe Simonis <chs@openerp.com> | 2012-11-26 19:12:00 +0100 |
|---|---|---|
| committer | Christophe Simonis <chs@openerp.com> | 2012-11-26 19:12:00 +0100 |
| commit | ec58393f72bc5a73977c8d45152fb2f581b9aa9b (patch) | |
| tree | 4bff749566981c7cb412eb3a354b66f603d7cdb3 /win32 | |
| parent | bb6f654c0d028ddc26fcac8c5d4b7b738c344169 (diff) | |
[FIX] service win32: improve service: stop as same exitcode as openerp-server + auto config service to restart if exits with non-zero exitcode
bzr revid: chs@openerp.com-20121126181200-01y0jqh46xsis7fb
Diffstat (limited to 'win32')
| -rw-r--r-- | win32/OpenERPServerService.py | 77 |
1 files changed, 39 insertions, 38 deletions
diff --git a/win32/OpenERPServerService.py b/win32/OpenERPServerService.py index bf7c83e74a4..efd9243fc31 100644 --- a/win32/OpenERPServerService.py +++ b/win32/OpenERPServerService.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- ############################################################################## -# +# # OpenERP, Open Source Management Solution # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). # @@ -15,14 +15,12 @@ # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License -# along with this program. If not, see <http://www.gnu.org/licenses/>. +# along with this program. If not, see <http://www.gnu.org/licenses/>. # ############################################################################## -# Win32 python extensions modules import win32serviceutil import win32service -import win32event import win32api import win32process import servicemanager @@ -30,35 +28,27 @@ import servicemanager import sys import subprocess import os -import thread + +from ..openerp.release import serie class OpenERPServerService(win32serviceutil.ServiceFramework): # required info - _svc_name_ = "openerp-server-6.1" - _svc_display_name_ = "OpenERP Server 6.1" + _svc_name_ = "openerp-server-" + serie + _svc_display_name_ = "OpenERP Server " + serie # optionnal info - _svc_description_ = "OpenERP Server 6.1 service" + _svc_description_ = "OpenERP Server %s service" % (serie,) def __init__(self, args): win32serviceutil.ServiceFramework.__init__(self, args) - # Create an event which we will use to wait on. - # The "service stop" request will set this event. - self.hWaitStop = win32event.CreateEvent(None, 0, 0, None) # a reference to the server's process self.terpprocess = None - # info if the service terminates correctly or if the server crashed - self.stopping = False - def SvcStop(self): # Before we do anything, tell the SCM we are starting the stop process. self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) - # stop the running TERP Server: say it's a normal exit + # stop the running OpenERP Server: say it's a normal exit win32api.TerminateProcess(int(self.terpprocess._handle), 0) servicemanager.LogInfoMsg("OpenERP Server stopped correctly") - # And set my event. - win32event.SetEvent(self.hWaitStop) - def StartTERP(self): # The server finds now its configuration automatically on Windows @@ -69,29 +59,40 @@ class OpenERPServerService(win32serviceutil.ServiceFramework): server_path = os.path.join(server_dir, 'server', 'openerp-server.exe') self.terpprocess = subprocess.Popen([server_path], cwd=server_dir, creationflags=win32process.CREATE_NO_WINDOW) - - def StartControl(self,ws): - # this listens to the Service Manager's events - win32event.WaitForSingleObject(ws, win32event.INFINITE) - self.stopping = True - def SvcDoRun(self): - # Start OpenERP Server itself self.StartTERP() - # start the loop waiting for the Service Manager's stop signal - thread.start_new_thread(self.StartControl, (self.hWaitStop,)) - # Log a info message that the server is running servicemanager.LogInfoMsg("OpenERP Server up and running") - # verification if the server is really running, else quit with an error - self.terpprocess.wait() - if not self.stopping: - sys.exit("OpenERP Server check: server not running, check the logfile for more info") - - - -if __name__=='__main__': + # exit with same exit code as OpenERP process + sys.exit(self.terpprocess.wait()) + + +def option_handler(opts): + # configure the service to auto restart on failures... + service_name = OpenERPServerService._svc_name + + hscm = win32service.OpenSCManager(None, None, win32service.SC_MANAGER_ALL_ACCESS) + try: + hs = win32serviceutil.SmartOpenService(hscm, service_name, win32service.SERVICE_ALL_ACCESS) + try: + service_failure_actions = { + 'ResetPeriod': 0, # Time in ms after which to reset the failure count to zero. + 'RebootMsg': u'', # Not using reboot option + 'Command': u'', # Not using run-command option + 'Actions': [ + (win32service.SC_ACTION_RESTART, 10), # action, delay in ms + (win32service.SC_ACTION_RESTART, 10), + (win32service.SC_ACTION_RESTART, 10), + ] + } + win32service.ChangeServiceConfig2(hs, win32service.SERVICE_CONFIG_FAILURE_ACTIONS, service_failure_actions) + finally: + win32service.CloseServiceHandle(hs) + finally: + win32service.CloseServiceHandle(hscm) + + +if __name__ == '__main__': # Do with the service whatever option is passed in the command line - win32serviceutil.HandleCommandLine(OpenERPServerService) + win32serviceutil.HandleCommandLine(OpenERPServerService, customOptionHandler=option_handler) # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: - |
