Launchpad automatic translations update.
[odoo/odoo.git] / openerp / service / __init__.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
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>)
7 #
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.
12 #
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.
17 #
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/>.
20 #
21 ##############################################################################
22
23 import logging
24 import threading
25 import time
26
27 import cron
28 import netrpc_server
29 import web_services
30 import web_services
31 import wsgi_server
32
33 import openerp.modules
34 import openerp.netsvc
35 import openerp.osv
36 import openerp.tools
37
38 #.apidoc title: RPC Services
39
40 """ Classes of this module implement the network protocols that the
41     OpenERP server uses to communicate with remote clients.
42
43     Some classes are mostly utilities, whose API need not be visible to
44     the average user/developer. Study them only if you are about to
45     implement an extension to the network protocols, or need to debug some
46     low-level behavior of the wire.
47 """
48
49 _logger = logging.getLogger(__name__)
50
51 def load_server_wide_modules():
52     for m in openerp.conf.server_wide_modules:
53         try:
54             openerp.modules.module.load_openerp_module(m)
55         except Exception:
56             msg = ''
57             if m == 'web':
58                 msg = """
59 The `web` module is provided by the addons found in the `openerp-web` project.
60 Maybe you forgot to add those addons in your addons_path configuration."""
61             _logger.exception('Failed to load server-wide module `%s`.%s', m, msg)
62
63 start_internal_done = False
64 main_thread_id = threading.currentThread().ident
65
66 def start_internal():
67     global start_internal_done
68     if start_internal_done:
69         return
70     openerp.netsvc.init_logger()
71     openerp.modules.loading.open_openerp_namespace()
72
73     # Instantiate local services (this is a legacy design).
74     openerp.osv.osv.start_object_proxy()
75     # Export (for RPC) services.
76     web_services.start_service()
77
78     load_server_wide_modules()
79     start_internal_done = True
80
81 def start_services():
82     """ Start all services including http, netrpc and cron """
83     start_internal()
84     # Initialize the NETRPC server.
85     netrpc_server.start_service()
86     # Start the WSGI server.
87     wsgi_server.start_service()
88     # Start the main cron thread.
89     cron.start_service()
90
91 def stop_services():
92     """ Stop all services. """
93     # stop services
94     cron.stop_service()
95     netrpc_server.stop_service()
96     wsgi_server.stop_service()
97
98     _logger.info("Initiating shutdown")
99     _logger.info("Hit CTRL-C again or send a second signal to force the shutdown.")
100     logging.shutdown()
101
102     # Manually join() all threads before calling sys.exit() to allow a second signal
103     # to trigger _force_quit() in case some non-daemon threads won't exit cleanly.
104     # threading.Thread.join() should not mask signals (at least in python 2.5).
105     me = threading.currentThread()
106     for thread in threading.enumerate():
107         if thread != me and not thread.isDaemon() and thread.ident != main_thread_id:
108             while thread.isAlive():
109                 # Need a busyloop here as thread.join() masks signals
110                 # and would prevent the forced shutdown.
111                 thread.join(0.05)
112                 time.sleep(0.05)
113
114     openerp.modules.registry.RegistryManager.delete_all()
115
116 def start_services_workers():
117     import openerp.service.workers
118     openerp.multi_process = True
119
120     openerp.service.workers.Multicorn(openerp.service.wsgi_server.application).run()
121
122
123 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: