From 4fd1bc7b3c67ab531aca854486c6793340cba502 Mon Sep 17 00:00:00 2001 From: Olivier Dony Date: Fri, 21 Dec 2012 14:37:51 +0100 Subject: [PATCH] [IMP] cron: remove useless pooljobs and schedule_cron_jobs methods The pooljobs and scheduled_cron_jobs stuff was only used to delay the processing of cron jobs until after the registry was fully loaded. However this is already the case because RegistryManager.new() only sets the flag at the end of the init step. The flag was named `registry.cron` but simply meant that the registry was fully loaded and ready, so it is simpler to rename it to `registry.ready`. In multiprocess mode this flag is enterily irrelevant so there is no need to selectively set it to True or False. `registry.ready` is simpler. bzr revid: odo@openerp.com-20121221133751-h4x670vblfr3d09e --- openerp/cli/server.py | 7 ++----- openerp/modules/registry.py | 32 ++++++++++---------------------- openerp/pooler.py | 11 +++-------- openerp/service/cron.py | 2 +- 4 files changed, 16 insertions(+), 36 deletions(-) diff --git a/openerp/cli/server.py b/openerp/cli/server.py index 7c768206354..7fb252f905f 100644 --- a/openerp/cli/server.py +++ b/openerp/cli/server.py @@ -95,10 +95,7 @@ def preload_registry(dbname): """ Preload a registry, and start the cron.""" try: update_module = True if openerp.tools.config['init'] or openerp.tools.config['update'] else False - db, registry = openerp.pooler.get_db_and_pool(dbname, update_module=update_module, 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() + db, registry = openerp.pooler.get_db_and_pool(dbname,update_module=update_module) except Exception: _logger.exception('Failed to initialize database `%s`.', dbname) @@ -106,7 +103,7 @@ def run_test_file(dbname, test_file): """ Preload a registry, possibly run a test file, and start the cron.""" try: config = openerp.tools.config - db, registry = openerp.pooler.get_db_and_pool(dbname, update_module=config['init'] or config['update'], pooljobs=False) + db, registry = openerp.pooler.get_db_and_pool(dbname, update_module=config['init'] or config['update']) cr = db.cursor() _logger.info('loading test file %s', test_file) openerp.tools.convert_yaml_import(cr, 'base', file(test_file), 'test', {}, 'test', True) diff --git a/openerp/modules/registry.py b/openerp/modules/registry.py index 23dbc280803..4aa20f3349d 100644 --- a/openerp/modules/registry.py +++ b/openerp/modules/registry.py @@ -58,8 +58,8 @@ class Registry(object): self.db_name = db_name self.db = openerp.sql_db.db_connect(db_name) - # In monoprocess cron jobs flag (pooljobs) - self.cron = False + # Indicates that the registry is + self.ready = False # Inter-process signaling (used only when openerp.multi_process is True): # The `base_registry_signaling` sequence indicates the whole registry @@ -121,15 +121,6 @@ class Registry(object): 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 @@ -192,18 +183,17 @@ class RegistryManager(object): registries_lock = threading.RLock() @classmethod - def get(cls, db_name, force_demo=False, status=None, update_module=False, - pooljobs=True): + def get(cls, db_name, force_demo=False, status=None, update_module=False): """ Return a registry for a given database name.""" try: return cls.registries[db_name] except KeyError: return cls.new(db_name, force_demo, status, - update_module, pooljobs) + update_module) @classmethod def new(cls, db_name, force_demo=False, status=None, - update_module=False, pooljobs=True): + update_module=False): """ Create and return a new registry for a given database name. The (possibly) previous registry for that database name is discarded. @@ -235,8 +225,7 @@ class RegistryManager(object): finally: cr.close() - if pooljobs: - registry.schedule_cron_jobs() + registry.ready = True return registry @@ -273,7 +262,7 @@ class RegistryManager(object): @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) + registry = cls.get(db_name) cr = registry.db.cursor() try: cr.execute(""" @@ -285,8 +274,7 @@ class RegistryManager(object): # 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 = cls.new(db_name) 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 @@ -311,7 +299,7 @@ class RegistryManager(object): 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) + registry = cls.get(db_name) if registry.any_cache_cleared(): _logger.info("At least one model cache has been cleared, signaling through the database.") cr = registry.db.cursor() @@ -327,7 +315,7 @@ class RegistryManager(object): @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) + registry = cls.get(db_name) cr = registry.db.cursor() r = 1 try: diff --git a/openerp/pooler.py b/openerp/pooler.py index d5588acf6ff..58cf4936b27 100644 --- a/openerp/pooler.py +++ b/openerp/pooler.py @@ -28,22 +28,17 @@ from openerp.modules.registry import RegistryManager -def get_db_and_pool(db_name, force_demo=False, status=None, update_module=False, pooljobs=True): +def get_db_and_pool(db_name, force_demo=False, status=None, update_module=False): """Create and return a database connection and a newly initialized registry.""" - registry = RegistryManager.get(db_name, force_demo, status, update_module, pooljobs) + registry = RegistryManager.get(db_name, force_demo, status, update_module) return registry.db, registry def restart_pool(db_name, force_demo=False, status=None, update_module=False): """Delete an existing registry and return a database connection and a newly initialized registry.""" - # preserve previous `cron-active` status of registry if it existed already - previous_registry = RegistryManager.registries.get(db_name) - cron_active = previous_registry and previous_registry.cron or False - - registry = RegistryManager.new(db_name, force_demo, status, update_module, pooljobs=cron_active) + registry = RegistryManager.new(db_name, force_demo, status, update_module) return registry.db, registry - def get_db(db_name): """Return a database connection. The corresponding registry is initialized.""" return get_db_and_pool(db_name)[0] diff --git a/openerp/service/cron.py b/openerp/service/cron.py index 337583b65e4..f3c81fe785f 100644 --- a/openerp/service/cron.py +++ b/openerp/service/cron.py @@ -43,7 +43,7 @@ def cron_runner(number): registries = openerp.modules.registry.RegistryManager.registries _logger.debug('cron%d polling for jobs', number) for db_name, registry in registries.items(): - while True and registry.cron: + while True and registry.ready: # acquired = openerp.addons.base.ir.ir_cron.ir_cron._acquire_job(db_name) # TODO why isnt openerp.addons.base defined ? import sys