[FIX] Fix date widgets styling
[odoo/odoo.git] / openerp-server
index 30d08f2..71a52d5 100755 (executable)
@@ -27,7 +27,7 @@ OpenERP is an ERP+CRM program for small and medium businesses.
 The whole source code is distributed under the terms of the
 GNU Public Licence.
 
-(c) 2003-TODAY, Fabien Pinckaers - OpenERP s.a.
+(c) 2003-TODAY, Fabien Pinckaers - OpenERP SA
 """
 
 import logging
@@ -88,19 +88,27 @@ def setup_pid_file():
 
 def preload_registry(dbname):
     """ Preload a registry, and start the cron."""
-    db, pool = openerp.pooler.get_db_and_pool(dbname, update_module=config['init'] or config['update'], pooljobs=False)
-    pool.get('ir.cron').restart(db.dbname)
+    try:
+        db, registry = openerp.pooler.get_db_and_pool(dbname, update_module=config['init'] or config['update'], pooljobs=False)
+
+        # jobs will start to be processed later, when openerp.cron.start_master_thread() is called by openerp.service.start_services()
+        registry.schedule_cron_jobs()
+    except Exception:
+        logging.exception('Failed to initialize database `%s`.', dbname)
 
 def run_test_file(dbname, test_file):
     """ Preload a registry, possibly run a test file, and start the cron."""
-    db, pool = openerp.pooler.get_db_and_pool(dbname, update_module=config['init'] or config['update'], pooljobs=False)
+    try:
+        db, registry = openerp.pooler.get_db_and_pool(dbname, update_module=config['init'] or config['update'], pooljobs=False)
+        cr = db.cursor()
+        logger = logging.getLogger('server')
+        logger.info('loading test file %s', test_file)
+        openerp.tools.convert_yaml_import(cr, 'base', file(test_file), {}, 'test', True)
+        cr.rollback()
+        cr.close()
+    except Exception:
+        logging.exception('Failed to initialize database `%s` and run test file `%s`.', dbname, test_file)
 
-    cr = db.cursor()
-    logger = logging.getLogger('server')
-    logger.info('loading test file %s', test_file)
-    openerp.tools.convert_yaml_import(cr, 'base', file(test_file), {}, 'test', True)
-    cr.rollback()
-    cr.close()
 
 def export_translation():
     config = openerp.tools.config
@@ -136,27 +144,6 @@ def import_translation():
     cr.commit()
     cr.close()
 
-def start_services():
-    http_server = openerp.service.http_server
-    netrpc_server = openerp.service.netrpc_server
-
-    # Instantiate local services (this is a legacy design).
-    openerp.osv.osv.start_object_proxy()
-    # Export (for RPC) services.
-    openerp.service.web_services.start_web_services()
-
-    # Initialize the HTTP stack.
-    http_server.init_servers()
-    http_server.init_xmlrpc()
-    http_server.init_static_http()
-    netrpc_server.init_servers()
-
-    # Start the main cron thread.
-    openerp.netsvc.start_agent()
-
-    # Start the top-level servers threads (normally HTTP, HTTPS, and NETRPC).
-    openerp.netsvc.Server.startAll()
-
 # Variable keeping track of the number of calls to the signal handler defined
 # below. This variable is monitored by ``quit_on_signals()``.
 quit_signals_received = 0
@@ -192,9 +179,12 @@ def dumpstacks(sig, frame):
 def setup_signal_handlers():
     """ Register the signal handler defined above. """
     SIGNALS = map(lambda x: getattr(signal, "SIG%s" % x), "INT TERM".split())
-    map(lambda sig: signal.signal(sig, signal_handler), SIGNALS)
     if os.name == 'posix':
+        map(lambda sig: signal.signal(sig, signal_handler), SIGNALS)
         signal.signal(signal.SIGQUIT, dumpstacks)
+    elif os.name == 'nt':
+        import win32api
+        win32api.SetConsoleCtrlHandler(lambda sig: signal_handler(sig, None), 1)
 
 def quit_on_signals():
     """ Wait for one or two signals then shutdown the server.
@@ -204,34 +194,23 @@ def quit_on_signals():
 
     """
     # Wait for a first signal to be handled. (time.sleep will be interrupted
-    # by the signal handler.)
-    while quit_signals_received == 0:
-        time.sleep(60)
+    # by the signal handler.) The try/except is for the win32 case.
+    try:
+        while quit_signals_received == 0:
+            time.sleep(60)
+    except KeyboardInterrupt, e:
+        pass
 
-    openerp.netsvc.Agent.quit()
-    openerp.netsvc.Server.quitAll()
-    config = openerp.tools.config
     if config['pidfile']:
         os.unlink(config['pidfile'])
-    logger = logging.getLogger('server')
-    logger.info("Initiating shutdown")
-    logger.info("Hit CTRL-C again or send a second signal to force the sutdown.")
-    logging.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)
-    for thread in threading.enumerate():
-        if thread != threading.currentThread() and not thread.isDaemon():
-            while thread.isAlive():
-                # need a busyloop here as thread.join() masks signals
-                # and would present the forced shutdown
-                thread.join(0.05)
-                time.sleep(0.05)
+
+    openerp.service.stop_services()
     sys.exit(0)
 
 if __name__ == "__main__":
 
+    os.environ["TZ"] = "UTC"
+
     check_root_user()
     openerp.tools.config.parse_config(sys.argv[1:])
     check_postgres_user()
@@ -240,6 +219,8 @@ if __name__ == "__main__":
 
     config = openerp.tools.config
 
+    setup_signal_handlers()
+
     if config["test_file"]:
         run_test_file(config['db_name'], config['test_file'])
         sys.exit(0)
@@ -252,6 +233,26 @@ if __name__ == "__main__":
         import_translation()
         sys.exit(0)
 
+    if not config["stop_after_init"]:
+        # Some module register themselves when they are loaded so we need the
+        # services to be running before loading any registry.
+        openerp.service.start_services()
+
+    for m in openerp.conf.server_wide_modules:
+        try:
+            __import__(m)
+            # Call any post_load hook.
+            info = openerp.modules.module.load_information_from_description_file(m)
+            if info['post_load']:
+                getattr(sys.modules[m], info['post_load'])()
+        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."""
+            logging.exception('Failed to load server-wide module `%s`.%s', m, msg)
+
     if config['db_name']:
         for dbname in config['db_name'].split(','):
             preload_registry(dbname)
@@ -259,19 +260,7 @@ if __name__ == "__main__":
     if config["stop_after_init"]:
         sys.exit(0)
 
-    for m in openerp.conf.server_wide_modules:
-        __import__(m)
-        # Register a WSGI entry point if any.
-        info = openerp.modules.module.load_information_from_description_file(m)
-        if info['wsgi']:
-            openerp.wsgi.register_wsgi_handler(getattr(sys.modules[m], info['wsgi']))
-
-    #openerp.wsgi.serve()
-
-
     setup_pid_file()
-    setup_signal_handlers()
-    start_services()
     logger = logging.getLogger('server')
     logger.info('OpenERP server is running, waiting for connections...')
     quit_on_signals()