1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ##############################################################################
22 """ Models registries.
25 from contextlib import contextmanager
30 import openerp.osv.orm
32 import openerp.modules.db
33 import openerp.tools.config
34 from openerp.tools import assertion_report
36 _logger = logging.getLogger(__name__)
38 class Registry(object):
39 """ Model registry for a particular database.
41 The registry is essentially a mapping between model names and model
42 instances. There is one registry instance per database.
46 def __init__(self, db_name):
47 self.models = {} # model name/model instance mapping
49 self._store_function = {}
51 self._init_parent = {}
52 self._assertion_report = assertion_report.assertion_report()
53 self.fields_by_model = None
55 # modules fully loaded (maintained during init phase by `loading` module)
56 self._init_modules = set()
58 self.db_name = db_name
59 self.db = openerp.sql_db.db_connect(db_name)
61 # In monoprocess cron jobs flag (pooljobs)
64 # Inter-process signaling (used only when openerp.multi_process is True):
65 # The `base_registry_signaling` sequence indicates the whole registry
67 # The `base_cache_signaling sequence` indicates all caches must be
68 # invalidated (i.e. cleared).
69 self.base_registry_signaling_sequence = 1
70 self.base_cache_signaling_sequence = 1
72 # Flag indicating if at least one model cache has been cleared.
73 # Useful only in a multi-process context.
74 self._any_cache_cleared = False
77 has_unaccent = openerp.modules.db.has_unaccent(cr)
78 if openerp.tools.config['unaccent'] and not has_unaccent:
79 _logger.warning("The option --unaccent was given but no unaccent() function was found in database.")
80 self.has_unaccent = openerp.tools.config['unaccent'] and has_unaccent
83 def do_parent_store(self, cr):
84 for o in self._init_parent:
85 self.get(o)._parent_store_compute(cr)
89 """ Return the list of model names in this registry."""
90 return self.models.keys()
92 def add(self, model_name, model):
93 """ Add or replace a model in the registry."""
94 self.models[model_name] = model
96 def get(self, model_name):
97 """ Return a model for a given name or None if it doesn't exist."""
98 return self.models.get(model_name)
100 def __getitem__(self, model_name):
101 """ Return a model for a given name or raise KeyError if it doesn't exist."""
102 return self.models[model_name]
104 def load(self, cr, module):
105 """ Load a given module in the registry.
107 At the Python level, the modules are already loaded, but not yet on a
108 per-registry level. This method populates a registry with the given
109 modules, i.e. it instanciates all the classes of a the given module
110 and registers them in the registry.
116 # Instantiate registered classes (via the MetaModel automatic discovery
117 # or via explicit constructor call), and add them to the pool.
118 for cls in openerp.osv.orm.MetaModel.module_to_models.get(module.name, []):
119 res.append(cls.create_instance(self, cr))
123 def schedule_cron_jobs(self):
124 """ Make the cron thread care about this registry/database jobs.
125 This will initiate the cron thread to check for any pending jobs for
126 this registry/database as soon as possible. Then it will continuously
127 monitor the ir.cron model for future jobs. See openerp.cron for
132 def clear_caches(self):
134 This clears the caches associated to methods decorated with
135 ``tools.ormcache`` or ``tools.ormcache_multi`` for all the models.
137 for model in self.models.itervalues():
139 # Special case for ir_ui_menu which does not use openerp.tools.ormcache.
140 ir_ui_menu = self.models.get('ir.ui.menu')
142 ir_ui_menu.clear_cache()
145 # Useful only in a multi-process context.
146 def reset_any_cache_cleared(self):
147 self._any_cache_cleared = False
149 # Useful only in a multi-process context.
150 def any_cache_cleared(self):
151 return self._any_cache_cleared
154 def setup_multi_process_signaling(cls, cr):
155 if not openerp.multi_process:
158 # Inter-process signaling:
159 # The `base_registry_signaling` sequence indicates the whole registry
161 # The `base_cache_signaling sequence` indicates all caches must be
162 # invalidated (i.e. cleared).
163 cr.execute("""SELECT sequence_name FROM information_schema.sequences WHERE sequence_name='base_registry_signaling'""")
164 if not cr.fetchall():
165 cr.execute("""CREATE SEQUENCE base_registry_signaling INCREMENT BY 1 START WITH 1""")
166 cr.execute("""SELECT nextval('base_registry_signaling')""")
167 cr.execute("""CREATE SEQUENCE base_cache_signaling INCREMENT BY 1 START WITH 1""")
168 cr.execute("""SELECT nextval('base_cache_signaling')""")
171 def cursor(self, auto_commit=True):
172 cr = self.db.cursor()
181 class RegistryManager(object):
182 """ Model registries manager.
184 The manager is responsible for creation and deletion of model
185 registries (essentially database connection/model registry pairs).
188 # Mapping between db name and model registry.
189 # Accessed through the methods below.
191 registries_lock = threading.RLock()
194 def get(cls, db_name, force_demo=False, status=None, update_module=False,
196 """ Return a registry for a given database name."""
198 return cls.registries[db_name]
200 return cls.new(db_name, force_demo, status,
201 update_module, pooljobs)
204 def new(cls, db_name, force_demo=False, status=None,
205 update_module=False, pooljobs=True):
206 """ Create and return a new registry for a given database name.
208 The (possibly) previous registry for that database name is discarded.
211 import openerp.modules
212 with cls.registries_lock:
213 registry = Registry(db_name)
215 # Initializing a registry will call general code which will in turn
216 # call registries.get (this object) to obtain the registry being
217 # initialized. Make it available in the registries dictionary then
218 # remove it if an exception is raised.
220 cls.registries[db_name] = registry
222 # This should be a method on Registry
223 openerp.modules.load_modules(registry.db, force_demo, status, update_module)
225 del cls.registries[db_name]
228 cr = registry.db.cursor()
230 Registry.setup_multi_process_signaling(cr)
231 registry.do_parent_store(cr)
232 registry.get('ir.actions.report.xml').register_all(cr)
238 registry.schedule_cron_jobs()
243 def delete(cls, db_name):
244 """Delete the registry linked to a given database. """
245 with cls.registries_lock:
246 if db_name in cls.registries:
247 cls.registries[db_name].clear_caches()
248 del cls.registries[db_name]
252 """Delete all the registries. """
253 with cls.registries_lock:
254 for db_name in cls.registries.keys():
258 def clear_caches(cls, db_name):
261 This clears the caches associated to methods decorated with
262 ``tools.ormcache`` or ``tools.ormcache_multi`` for all the models
263 of the given database name.
265 This method is given to spare you a ``RegistryManager.get(db_name)``
266 that would loads the given database if it was not already loaded.
268 with cls.registries_lock:
269 if db_name in cls.registries:
270 cls.registries[db_name].clear_caches()
273 def check_registry_signaling(cls, db_name):
274 if openerp.multi_process and db_name in cls.registries:
275 registry = cls.get(db_name, pooljobs=False)
276 cr = registry.db.cursor()
279 SELECT base_registry_signaling.last_value,
280 base_cache_signaling.last_value
281 FROM base_registry_signaling, base_cache_signaling""")
283 # Check if the model registry must be reloaded (e.g. after the
284 # database has been updated by another process).
285 if registry.base_registry_signaling_sequence != r:
286 _logger.info("Reloading the model registry after database signaling.")
287 # Don't run the cron in the Gunicorn worker.
288 registry = cls.new(db_name, pooljobs=False)
289 registry.base_registry_signaling_sequence = r
290 # Check if the model caches must be invalidated (e.g. after a write
291 # occured on another process). Don't clear right after a registry
293 elif registry.base_cache_signaling_sequence != c:
294 _logger.info("Invalidating all model caches after database signaling.")
295 registry.base_cache_signaling_sequence = c
296 registry.clear_caches()
297 registry.reset_any_cache_cleared()
298 # One possible reason caches have been invalidated is the
299 # use of decimal_precision.write(), in which case we need
300 # to refresh fields.float columns.
301 for model in registry.models.values():
302 for column in model._columns.values():
303 if hasattr(column, 'digits_change'):
304 column.digits_change(cr)
309 def signal_caches_change(cls, db_name):
310 if openerp.multi_process and db_name in cls.registries:
311 # Check the registries if any cache has been cleared and signal it
312 # through the database to other processes.
313 registry = cls.get(db_name, pooljobs=False)
314 if registry.any_cache_cleared():
315 _logger.info("At least one model cache has been cleared, signaling through the database.")
316 cr = registry.db.cursor()
319 cr.execute("select nextval('base_cache_signaling')")
323 registry.base_cache_signaling_sequence = r
324 registry.reset_any_cache_cleared()
327 def signal_registry_change(cls, db_name):
328 if openerp.multi_process and db_name in cls.registries:
329 registry = cls.get(db_name, pooljobs=False)
330 cr = registry.db.cursor()
333 cr.execute("select nextval('base_registry_signaling')")
337 registry.base_registry_signaling_sequence = r
339 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: