1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 # Copyright (C) 2010-2012 OpenERP SA (<http://www.openerp.com>)
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Affero General Public License as
10 # published by the Free Software Foundation, either version 3 of the
11 # License, or (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU Affero General Public License for more details.
18 # You should have received a copy of the GNU Affero General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ##############################################################################
36 import openerp.modules
39 from openerp.release import nt_service_name
42 #.apidoc title: RPC Services
44 """ Classes of this module implement the network protocols that the
45 OpenERP server uses to communicate with remote clients.
47 Some classes are mostly utilities, whose API need not be visible to
48 the average user/developer. Study them only if you are about to
49 implement an extension to the network protocols, or need to debug some
50 low-level behavior of the wire.
53 _logger = logging.getLogger(__name__)
55 def load_server_wide_modules():
56 for m in openerp.conf.server_wide_modules:
58 openerp.modules.module.load_openerp_module(m)
63 The `web` module is provided by the addons found in the `openerp-web` project.
64 Maybe you forgot to add those addons in your addons_path configuration."""
65 _logger.exception('Failed to load server-wide module `%s`.%s', m, msg)
67 start_internal_done = False
68 main_thread_id = threading.currentThread().ident
71 global start_internal_done
72 if start_internal_done:
74 openerp.netsvc.init_logger()
75 openerp.modules.loading.open_openerp_namespace()
77 # Export (for RPC) services.
78 web_services.start_service()
80 load_server_wide_modules()
81 start_internal_done = True
84 """ Start all services including http, and cron """
86 # Start the WSGI server.
87 wsgi_server.start_service()
88 # Start the main cron thread.
92 """ Stop all services. """
95 wsgi_server.stop_service()
97 _logger.info("Initiating shutdown")
98 _logger.info("Hit CTRL-C again or send a second signal to force the shutdown.")
100 # Manually join() all threads before calling sys.exit() to allow a second signal
101 # to trigger _force_quit() in case some non-daemon threads won't exit cleanly.
102 # threading.Thread.join() should not mask signals (at least in python 2.5).
103 me = threading.currentThread()
104 _logger.debug('current thread: %r', me)
105 for thread in threading.enumerate():
106 _logger.debug('process %r (%r)', thread, thread.isDaemon())
107 if thread != me and not thread.isDaemon() and thread.ident != main_thread_id:
108 while thread.isAlive():
109 _logger.debug('join and sleep')
110 # Need a busyloop here as thread.join() masks signals
111 # and would prevent the forced shutdown.
116 openerp.modules.registry.RegistryManager.delete_all()
119 def start_services_workers():
120 import openerp.service.workers
121 openerp.multi_process = True
122 openerp.service.workers.Multicorn(openerp.service.wsgi_server.application).run()
125 """reexecute openerp-server process with (nearly) the same arguments"""
126 if openerp.tools.osutil.is_running_as_nt_service():
127 subprocess.call('net stop {0} && net start {0}'.format(nt_service_name), shell=True)
128 exe = os.path.basename(sys.executable)
129 strip_args = ['-d', '-u']
131 args = [x for i, x in enumerate(a) if x not in strip_args and a[max(i - 1, 0)] not in strip_args]
132 if not args or args[0] != exe:
134 os.execv(sys.executable, args)
136 def restart_server():
137 if openerp.multi_process:
138 raise NotImplementedError("Multicorn is not supported (but gunicorn was)")
139 pid = openerp.wsgi.core.arbiter_pid
140 os.kill(pid, signal.SIGHUP)
147 # run in a thread to let the current thread return response to the caller.
148 threading.Thread(target=reborn).start()
150 openerp.phoenix = True
151 os.kill(os.getpid(), signal.SIGINT)
153 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: