# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    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/>.
#
##############################################################################

import logging
import os
import signal
import sys
import threading
import time

import http_server
import netrpc_server
import web_services
import websrv_lib

import openerp.cron
import openerp.modules
import openerp.netsvc
import openerp.osv
import openerp.tools
import openerp.service.wsgi_server
import openerp.service.workers

#.apidoc title: RPC Services

""" Classes of this module implement the network protocols that the
    OpenERP server uses to communicate with remote clients.

    Some classes are mostly utilities, whose API need not be visible to
    the average user/developer. Study them only if you are about to
    implement an extension to the network protocols, or need to debug some
    low-level behavior of the wire.
"""

_logger = logging.getLogger(__name__)

def load_server_wide_modules():
    for m in openerp.conf.server_wide_modules:
        try:
            openerp.modules.module.load_openerp_module(m)
        except Exception:
            msg = ''
            if m == 'web':
                msg = """
The `web` module is provided by the addons found in the `openerp-web` project.
Maybe you forgot to add those addons in your addons_path configuration."""
            _logger.exception('Failed to load server-wide module `%s`.%s', m, msg)

start_internal_done = False

def start_internal():
    global start_internal_done
    if start_internal_done:
        return
    openerp.netsvc.init_logger()
    openerp.modules.loading.open_openerp_namespace()
    # Instantiate local services (this is a legacy design).
    openerp.osv.osv.start_object_proxy()
    # Export (for RPC) services.
    web_services.start_web_services()
    load_server_wide_modules()
    start_internal_done = True

def start_services():
    """ Start all services including http, netrpc and cron """
    start_internal()

    # Initialize the HTTP stack.
    netrpc_server.init_servers()

    # Start the main cron thread.
    if openerp.conf.max_cron_threads:
        openerp.cron.start_master_thread()

    # Start the top-level servers threads (normally HTTP, HTTPS, and NETRPC).
    openerp.netsvc.Server.startAll()

    # Start the WSGI server.
    openerp.service.wsgi_server.start_server()

def stop_services():
    """ Stop all services. """
    # stop scheduling new jobs; we will have to wait for the jobs to complete below
    openerp.cron.cancel_all()

    openerp.netsvc.Server.quitAll()
    openerp.service.wsgi_server.stop_server()
    _logger.info("Initiating shutdown")
    _logger.info("Hit CTRL-C again or send a second signal to force the shutdown.")

    # Manually join() all threads before calling sys.exit() to allow a second signal
    # to trigger _force_quit() in case some non-daemon threads won't exit cleanly.
    # threading.Thread.join() should not mask signals (at least in python 2.5).
    me = threading.currentThread()
    _logger.debug('current thread: %r', me)
    for thread in threading.enumerate():
        _logger.debug('process %r (%r)', thread, thread.isDaemon())
        if thread != me and not thread.isDaemon():
            while thread.isAlive():
                _logger.debug('join and sleep')
                # Need a busyloop here as thread.join() masks signals
                # and would prevent the forced shutdown.
                thread.join(0.05)
                time.sleep(0.05)

    _logger.debug('--')
    openerp.modules.registry.RegistryManager.delete_all()
    logging.shutdown()

def start_services_workers():
    openerp.multi_process = True # Nah!

    openerp.service.workers.Multicorn(openerp.service.wsgi_server.application).run()

def restart_server():
    pid = openerp.wsgi.core.arbiter_pid
    if pid:
        os.kill(pid, signal.SIGHUP)
    else:
        openerp.phoenix = True
        signame = 'CTRL_C_EVENT' if os.name == 'nt' else 'SIGINT'
        sig = getattr(signal, signame)
        os.kill(os.getpid(), sig)

        #strip_args = ['-d', '-u']
        #a = sys.argv[:]
        #args = [x for i, x in enumerate(a) if x not in strip_args and a[max(i - 1, 0)] not in strip_args]

        #stop_services()
        #os.execv(sys.executable, [sys.executable] + args)

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

@@ -1015,19 +1015,8 @@
 nal%0A
-import sys%0A
 impo
@@ -1045,27 +1045,8 @@
 me%0A%0A
-import http_server%0A
 impo
@@ -1085,26 +1085,8 @@
 ices
-%0Aimport websrv_lib
 %0A%0Aim
@@ -4438,16 +4438,8 @@
 True
- # Nah!%0A
 %0A   
@@ -4539,24 +4539,141 @@
 t_server():%0A
+    if openerp.multi_process:%0A        raise NotImplementedError(%22Multicorn is not supported (but gunicorn was)%22)%0A    
     pid = op
@@ -4704,20 +4704,8 @@
 pid%0A
-    if pid:%0A
     
@@ -4878,24 +4878,24 @@
 l, signame)%0A
+
         os.k
@@ -4921,261 +4921,8 @@
 g)%0A%0A
-        #strip_args = %5B'-d', '-u'%5D%0A        #a = sys.argv%5B:%5D%0A        #args = %5Bx for i, x in enumerate(a) if x not in strip_args and a%5Bmax(i - 1, 0)%5D not in strip_args%5D%0A%0A        #stop_services()%0A        #os.execv(sys.executable, %5Bsys.executable%5D + args)%0A%0A
 # vi

correct restart of openerp