""" Models registries.
"""
-import threading
-
+from contextlib import contextmanager
import logging
+import threading
import openerp.sql_db
import openerp.osv.orm
+import openerp.tools
import openerp.modules.db
import openerp.tools.config
+from openerp.tools import assertion_report
+
+_logger = logging.getLogger(__name__)
class Registry(object):
""" Model registry for a particular database.
"""
def __init__(self, db_name):
- self.models = {} # model name/model instance mapping
+ self.models = {} # model name/model instance mapping
self._sql_error = {}
self._store_function = {}
self._init = True
self._init_parent = {}
+ self._assertion_report = assertion_report.assertion_report()
+ self.fields_by_model = None
+
+ # modules fully loaded (maintained during init phase by `loading` module)
+ self._init_modules = set()
+
self.db_name = db_name
self.db = openerp.sql_db.db_connect(db_name)
+ # In monoprocess cron jobs flag (pooljobs)
+ self.cron = False
+
+ # Inter-process signaling (used only when openerp.multi_process is True):
+ # The `base_registry_signaling` sequence indicates the whole registry
+ # must be reloaded.
+ # The `base_cache_signaling sequence` indicates all caches must be
+ # invalidated (i.e. cleared).
+ self.base_registry_signaling_sequence = 1
+ self.base_cache_signaling_sequence = 1
+
+ # Flag indicating if at least one model cache has been cleared.
+ # Useful only in a multi-process context.
+ self._any_cache_cleared = False
+
cr = self.db.cursor()
has_unaccent = openerp.modules.db.has_unaccent(cr)
if openerp.tools.config['unaccent'] and not has_unaccent:
- logger = logging.getLogger('unaccent')
- logger.warning("The option --unaccent was given but no unaccent() function was found in database.")
+ _logger.warning("The option --unaccent was given but no unaccent() function was found in database.")
self.has_unaccent = openerp.tools.config['unaccent'] and has_unaccent
cr.close()
""" Return a model for a given name or raise KeyError if it doesn't exist."""
return self.models[model_name]
- def instanciate(self, module, cr):
- """ Instanciate all the classes of a given module for a particular db."""
+ def load(self, cr, module):
+ """ Load a given module in the registry.
- res = []
+ At the Python level, the modules are already loaded, but not yet on a
+ per-registry level. This method populates a registry with the given
+ modules, i.e. it instanciates all the classes of a the given module
+ and registers them in the registry.
+ """
+ models_to_load = [] # need to preserve loading order
# Instantiate registered classes (via the MetaModel automatic discovery
# or via explicit constructor call), and add them to the pool.
- for cls in openerp.osv.orm.MetaModel.module_to_models.get(module, []):
- res.append(cls.create_instance(self, cr))
-
- return res
+ for cls in openerp.osv.orm.MetaModel.module_to_models.get(module.name, []):
+ # models register themselves in self.models
+ model = cls.create_instance(self, cr)
+ if model._name not in models_to_load:
+ # avoid double-loading models whose declaration is split
+ models_to_load.append(model._name)
+ return [self.models[m] for m in models_to_load]
+
+ def schedule_cron_jobs(self):
+ """ Make the cron thread care about this registry/database jobs.
+ This will initiate the cron thread to check for any pending jobs for
+ this registry/database as soon as possible. Then it will continuously
+ monitor the ir.cron model for future jobs. See openerp.cron for
+ details.
+ """
+ self.cron = True
def clear_caches(self):
""" Clear the caches
-
This clears the caches associated to methods decorated with
``tools.ormcache`` or ``tools.ormcache_multi`` for all the models.
"""
for model in self.models.itervalues():
model.clear_caches()
+ # Special case for ir_ui_menu which does not use openerp.tools.ormcache.
+ ir_ui_menu = self.models.get('ir.ui.menu')
+ if ir_ui_menu:
+ ir_ui_menu.clear_cache()
+
+
+ # Useful only in a multi-process context.
+ def reset_any_cache_cleared(self):
+ self._any_cache_cleared = False
+
+ # Useful only in a multi-process context.
+ def any_cache_cleared(self):
+ return self._any_cache_cleared
+
+ @classmethod
+ def setup_multi_process_signaling(cls, cr):
+ if not openerp.multi_process:
+ return
+
+ # Inter-process signaling:
+ # The `base_registry_signaling` sequence indicates the whole registry
+ # must be reloaded.
+ # The `base_cache_signaling sequence` indicates all caches must be
+ # invalidated (i.e. cleared).
+ cr.execute("""SELECT sequence_name FROM information_schema.sequences WHERE sequence_name='base_registry_signaling'""")
+ if not cr.fetchall():
+ cr.execute("""CREATE SEQUENCE base_registry_signaling INCREMENT BY 1 START WITH 1""")
+ cr.execute("""SELECT nextval('base_registry_signaling')""")
+ cr.execute("""CREATE SEQUENCE base_cache_signaling INCREMENT BY 1 START WITH 1""")
+ cr.execute("""SELECT nextval('base_cache_signaling')""")
+
+ @contextmanager
+ def cursor(self, auto_commit=True):
+ cr = self.db.cursor()
+ try:
+ yield cr
+ if auto_commit:
+ cr.commit()
+ finally:
+ cr.close()
+
class RegistryManager(object):
""" Model registries manager.
registries (essentially database connection/model registry pairs).
"""
-
# Mapping between db name and model registry.
# Accessed through the methods below.
registries = {}
registries_lock = threading.RLock()
-
@classmethod
def get(cls, db_name, force_demo=False, status=None, update_module=False,
pooljobs=True):
""" Return a registry for a given database name."""
- with cls.registries_lock:
- if db_name in cls.registries:
- registry = cls.registries[db_name]
- else:
- registry = cls.new(db_name, force_demo, status,
- update_module, pooljobs)
- return registry
-
+ try:
+ return cls.registries[db_name]
+ except KeyError:
+ return cls.new(db_name, force_demo, status,
+ update_module, pooljobs)
@classmethod
def new(cls, db_name, force_demo=False, status=None,
cr = registry.db.cursor()
try:
+ Registry.setup_multi_process_signaling(cr)
registry.do_parent_store(cr)
registry.get('ir.actions.report.xml').register_all(cr)
cr.commit()
finally:
cr.close()
- if pooljobs:
- registry.get('ir.cron').restart(registry.db.dbname)
-
- return registry
+ if pooljobs:
+ registry.schedule_cron_jobs()
+ return registry
@classmethod
def delete(cls, db_name):
- """ Delete the registry linked to a given database. """
+ """Delete the registry linked to a given database. """
with cls.registries_lock:
if db_name in cls.registries:
+ cls.registries[db_name].clear_caches()
del cls.registries[db_name]
+ @classmethod
+ def delete_all(cls):
+ """Delete all the registries. """
+ with cls.registries_lock:
+ for db_name in cls.registries.keys():
+ cls.delete(db_name)
@classmethod
def clear_caches(cls, db_name):
- """ Clear the caches
+ """Clear caches
This clears the caches associated to methods decorated with
``tools.ormcache`` or ``tools.ormcache_multi`` for all the models
if db_name in cls.registries:
cls.registries[db_name].clear_caches()
+ @classmethod
+ def check_registry_signaling(cls, db_name):
+ if openerp.multi_process and db_name in cls.registries:
+ registry = cls.get(db_name, pooljobs=False)
+ cr = registry.db.cursor()
+ try:
+ cr.execute("""
+ SELECT base_registry_signaling.last_value,
+ base_cache_signaling.last_value
+ FROM base_registry_signaling, base_cache_signaling""")
+ r, c = cr.fetchone()
+ # Check if the model registry must be reloaded (e.g. after the
+ # database has been updated by another process).
+ if registry.base_registry_signaling_sequence != r:
+ _logger.info("Reloading the model registry after database signaling.")
+ # Don't run the cron in the Gunicorn worker.
+ registry = cls.new(db_name, pooljobs=False)
+ registry.base_registry_signaling_sequence = r
+ # Check if the model caches must be invalidated (e.g. after a write
+ # occured on another process). Don't clear right after a registry
+ # has been reload.
+ elif registry.base_cache_signaling_sequence != c:
+ _logger.info("Invalidating all model caches after database signaling.")
+ registry.base_cache_signaling_sequence = c
+ registry.clear_caches()
+ registry.reset_any_cache_cleared()
+ # One possible reason caches have been invalidated is the
+ # use of decimal_precision.write(), in which case we need
+ # to refresh fields.float columns.
+ for model in registry.models.values():
+ for column in model._columns.values():
+ if hasattr(column, 'digits_change'):
+ column.digits_change(cr)
+ finally:
+ cr.close()
+
+ @classmethod
+ def signal_caches_change(cls, db_name):
+ if openerp.multi_process and db_name in cls.registries:
+ # Check the registries if any cache has been cleared and signal it
+ # through the database to other processes.
+ registry = cls.get(db_name, pooljobs=False)
+ if registry.any_cache_cleared():
+ _logger.info("At least one model cache has been cleared, signaling through the database.")
+ cr = registry.db.cursor()
+ r = 1
+ try:
+ cr.execute("select nextval('base_cache_signaling')")
+ r = cr.fetchone()[0]
+ finally:
+ cr.close()
+ registry.base_cache_signaling_sequence = r
+ registry.reset_any_cache_cleared()
+
+ @classmethod
+ def signal_registry_change(cls, db_name):
+ if openerp.multi_process and db_name in cls.registries:
+ registry = cls.get(db_name, pooljobs=False)
+ cr = registry.db.cursor()
+ r = 1
+ try:
+ cr.execute("select nextval('base_registry_signaling')")
+ r = cr.fetchone()[0]
+ finally:
+ cr.close()
+ registry.base_registry_signaling_sequence = r
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: