[REM] asynchronous database creation method
authorXavier Morel <xmo@openerp.com>
Tue, 15 Jul 2014 10:26:55 +0000 (12:26 +0200)
committerXavier Morel <xmo@openerp.com>
Tue, 15 Jul 2014 10:26:55 +0000 (12:26 +0200)
It's broken (though easy to fix) and not very useful, if third parties want to
create databases asynchronously they can handle the asynchronicity on the
client-side (an HTTP request is easy to make asynchronously after all) and
call the synchronous `create_database()`.

fixes #1137, after a fashion

openerp/modules/loading.py
openerp/service/db.py

index eb703e2..656511e 100644 (file)
@@ -52,7 +52,7 @@ _test_logger = logging.getLogger('openerp.tests')
 def load_module_graph(cr, graph, status=None, perform_checks=True, skip_modules=None, report=None):
     """Migrates+Updates or Installs all module nodes from ``graph``
        :param graph: graph of module nodes to load
-       :param status: status dictionary for keeping track of progress
+       :param status: deprecated parameter, unused, left to avoid changing signature in 8.0
        :param perform_checks: whether module descriptors should be checked for validity (prints warnings
                               for same cases)
        :param skip_modules: optional list of module names (packages) which have previously been loaded and can be skipped
@@ -120,9 +120,6 @@ def load_module_graph(cr, graph, status=None, perform_checks=True, skip_modules=
             if kind in ('demo', 'test'):
                 threading.currentThread().testing = False
 
-    if status is None:
-        status = {}
-
     processed_modules = []
     loaded_modules = []
     registry = openerp.registry(cr.dbname)
@@ -164,7 +161,6 @@ def load_module_graph(cr, graph, status=None, perform_checks=True, skip_modules=
         if hasattr(package, 'init') or hasattr(package, 'update') or package.state in ('to install', 'to upgrade'):
             registry.setup_models(cr)
             init_module_models(cr, package.name, models)
-        status['progress'] = float(index) / len(graph)
 
         # Can't put this line out of the loop: ir.module.module will be
         # registered by init_module_models() above.
@@ -186,7 +182,6 @@ def load_module_graph(cr, graph, status=None, perform_checks=True, skip_modules=
             _load_data(cr, module_name, idref, mode, kind='data')
             has_demo = hasattr(package, 'demo') or (package.dbdemo and package.state != 'installed')
             if has_demo:
-                status['progress'] = (index + 0.75) / len(graph)
                 _load_data(cr, module_name, idref, mode, kind='demo')
                 cr.execute('update ir_module_module set demo=%s where id=%s', (True, module_id))
                 modobj.invalidate_cache(cr, SUPERUSER_ID, ['demo'], [module_id])
@@ -272,9 +267,6 @@ def load_marked_modules(cr, graph, states, force, progressdict, report, loaded_m
     return processed_modules
 
 def load_modules(db, force_demo=False, status=None, update_module=False):
-    # TODO status['progress'] reporting is broken: used twice (and reset each
-    # time to zero) in load_module_graph, not fine-grained enough.
-    # It should be a method exposed by the registry.
     initialize_sys_path()
 
     force = []
index afe136e..048c160 100644 (file)
@@ -22,17 +22,12 @@ import security
 
 _logger = logging.getLogger(__name__)
 
-self_actions = {}
-self_id = 0
-self_id_protect = threading.Semaphore()
-
 class DatabaseExists(Warning):
     pass
 
 # This should be moved to openerp.modules.db, along side initialize().
 def _initialize_db(id, db_name, demo, lang, user_password):
     try:
-        self_actions[id]['progress'] = 0
         db = openerp.sql_db.db_connect(db_name)
         with closing(db.cursor()) as cr:
             # TODO this should be removed as it is done by RegistryManager.new().
@@ -41,7 +36,7 @@ def _initialize_db(id, db_name, demo, lang, user_password):
             cr.commit()
 
         registry = openerp.modules.registry.RegistryManager.new(
-            db_name, demo, self_actions[id], update_module=True)
+            db_name, demo, None, update_module=True)
 
         with closing(db.cursor()) as cr:
             if lang:
@@ -54,13 +49,9 @@ def _initialize_db(id, db_name, demo, lang, user_password):
             registry['res.users'].write(cr, SUPERUSER_ID, [SUPERUSER_ID], values)
 
             cr.execute('SELECT login, password FROM res_users ORDER BY login')
-            self_actions[id].update(users=cr.dictfetchall(), clean=True)
             cr.commit()
-
     except Exception, e:
-        self_actions[id].update(clean=False, exception=e)
         _logger.exception('CREATE DATABASE failed:')
-        self_actions[id]['traceback'] = traceback.format_exc()
 
 def dispatch(method, params):
     if method in ['create', 'get_progress', 'drop', 'dump', 'restore', 'rename',
@@ -90,34 +81,8 @@ def _create_empty_database(name):
             cr.autocommit(True)     # avoid transaction block
             cr.execute("""CREATE DATABASE "%s" ENCODING 'unicode' TEMPLATE "%s" """ % (name, chosen_template))
 
-def exp_create(db_name, demo, lang, user_password='admin'):
-    self_id_protect.acquire()
-    global self_id
-    self_id += 1
-    id = self_id
-    self_id_protect.release()
-
-    self_actions[id] = {'clean': False}
-
-    _create_empty_database(db_name)
-
-    _logger.info('CREATE DATABASE %s', db_name.lower())
-    create_thread = threading.Thread(target=_initialize_db,
-                                     args=(id, db_name, demo, lang, user_password))
-    create_thread.start()
-    self_actions[id]['thread'] = create_thread
-    return id
-
 def exp_create_database(db_name, demo, lang, user_password='admin'):
     """ Similar to exp_create but blocking."""
-    self_id_protect.acquire()
-    global self_id
-    self_id += 1
-    id = self_id
-    self_id_protect.release()
-
-    self_actions[id] = {'clean': False}
-
     _logger.info('Create database `%s`.', db_name)
     _create_empty_database(db_name)
     _initialize_db(id, db_name, demo, lang, user_password)
@@ -137,26 +102,6 @@ def exp_duplicate_database(db_original_name, db_name):
         shutil.copytree(from_fs, to_fs)
     return True
 
-def exp_get_progress(id):
-    if self_actions[id]['thread'].isAlive():
-#       return openerp.modules.init_progress[db_name]
-        return min(self_actions[id].get('progress', 0), 0.95), []
-    else:
-        clean = self_actions[id]['clean']
-        if clean:
-            users = self_actions[id]['users']
-            for user in users:
-                # Remove the None passwords as they can't be marshalled by XML-RPC.
-                if user['password'] is None:
-                    user['password'] = ''
-            self_actions.pop(id)
-            return 1.0, users
-        else:
-            a = self_actions.pop(id)
-            exc, tb = a['exception'], a['traceback']
-            raise Exception, exc, tb
-
-
 def _drop_conn(cr, db_name):
     # Try to terminate all other connections that might prevent
     # dropping the database