OSDN Git Service

[IMP] mejoras en opentck3: clases para manejo de sqlite y lectura del CSV Salidapazo...
[ipzuni/ip_zuni.git] / server / 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 # Win32 python extensions modules
23 import win32serviceutil
24 import win32service
25 import win32event
26 import win32api
27 import win32process
28 import servicemanager
29
30 import sys
31 import subprocess
32 import os
33 import thread
34
35 class OpenERPServerService(win32serviceutil.ServiceFramework):
36     # required info
37     _svc_name_ = "openerp-server-6.1"
38     _svc_display_name_ = "OpenERP Server 6.1"
39     # optionnal info
40     _svc_description_ = "OpenERP Server 6.1 service"
41
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
50         self.stopping = False
51
52
53     def SvcStop(self):
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")
59         # And set my event.
60         win32event.SetEvent(self.hWaitStop)
61
62
63     def StartTERP(self):
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)
71
72
73     def StartControl(self,ws):
74         # this listens to the Service Manager's events
75         win32event.WaitForSingleObject(ws, win32event.INFINITE)
76         self.stopping = True
77
78     def SvcDoRun(self):
79         # Start OpenERP Server itself
80         self.StartTERP()
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()
87         if not self.stopping:
88             sys.exit("OpenERP Server check: server not running, check the logfile for more info")
89
90
91
92 if __name__=='__main__':
93     # Do with the service whatever option is passed in the command line
94     win32serviceutil.HandleCommandLine(OpenERPServerService)
95
96 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
97