[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
This commit is contained in:
Olivier Dony 2012-12-21 14:37:51 +01:00
parent a873a7de80
commit 4fd1bc7b3c
4 changed files with 16 additions and 36 deletions

View File

@ -95,10 +95,7 @@ def preload_registry(dbname):
""" Preload a registry, and start the cron.""" """ Preload a registry, and start the cron."""
try: try:
update_module = True if openerp.tools.config['init'] or openerp.tools.config['update'] else False 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) db, registry = openerp.pooler.get_db_and_pool(dbname,update_module=update_module)
# 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: except Exception:
_logger.exception('Failed to initialize database `%s`.', dbname) _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.""" """ Preload a registry, possibly run a test file, and start the cron."""
try: try:
config = openerp.tools.config 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() cr = db.cursor()
_logger.info('loading test file %s', test_file) _logger.info('loading test file %s', test_file)
openerp.tools.convert_yaml_import(cr, 'base', file(test_file), 'test', {}, 'test', True) openerp.tools.convert_yaml_import(cr, 'base', file(test_file), 'test', {}, 'test', True)

View File

@ -58,8 +58,8 @@ class Registry(object):
self.db_name = db_name self.db_name = db_name
self.db = openerp.sql_db.db_connect(db_name) self.db = openerp.sql_db.db_connect(db_name)
# In monoprocess cron jobs flag (pooljobs) # Indicates that the registry is
self.cron = False self.ready = False
# Inter-process signaling (used only when openerp.multi_process is True): # Inter-process signaling (used only when openerp.multi_process is True):
# The `base_registry_signaling` sequence indicates the whole registry # The `base_registry_signaling` sequence indicates the whole registry
@ -121,15 +121,6 @@ class Registry(object):
models_to_load.append(model._name) models_to_load.append(model._name)
return [self.models[m] for m in models_to_load] 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): def clear_caches(self):
""" Clear the caches """ Clear the caches
This clears the caches associated to methods decorated with This clears the caches associated to methods decorated with
@ -192,18 +183,17 @@ class RegistryManager(object):
registries_lock = threading.RLock() registries_lock = threading.RLock()
@classmethod @classmethod
def get(cls, db_name, force_demo=False, status=None, update_module=False, def get(cls, db_name, force_demo=False, status=None, update_module=False):
pooljobs=True):
""" Return a registry for a given database name.""" """ Return a registry for a given database name."""
try: try:
return cls.registries[db_name] return cls.registries[db_name]
except KeyError: except KeyError:
return cls.new(db_name, force_demo, status, return cls.new(db_name, force_demo, status,
update_module, pooljobs) update_module)
@classmethod @classmethod
def new(cls, db_name, force_demo=False, status=None, 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. """ Create and return a new registry for a given database name.
The (possibly) previous registry for that database name is discarded. The (possibly) previous registry for that database name is discarded.
@ -235,8 +225,7 @@ class RegistryManager(object):
finally: finally:
cr.close() cr.close()
if pooljobs: registry.ready = True
registry.schedule_cron_jobs()
return registry return registry
@ -273,7 +262,7 @@ class RegistryManager(object):
@classmethod @classmethod
def check_registry_signaling(cls, db_name): def check_registry_signaling(cls, db_name):
if openerp.multi_process and db_name in cls.registries: 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() cr = registry.db.cursor()
try: try:
cr.execute(""" cr.execute("""
@ -285,8 +274,7 @@ class RegistryManager(object):
# database has been updated by another process). # database has been updated by another process).
if registry.base_registry_signaling_sequence != r: if registry.base_registry_signaling_sequence != r:
_logger.info("Reloading the model registry after database signaling.") _logger.info("Reloading the model registry after database signaling.")
# Don't run the cron in the Gunicorn worker. registry = cls.new(db_name)
registry = cls.new(db_name, pooljobs=False)
registry.base_registry_signaling_sequence = r registry.base_registry_signaling_sequence = r
# Check if the model caches must be invalidated (e.g. after a write # Check if the model caches must be invalidated (e.g. after a write
# occured on another process). Don't clear right after a registry # 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: if openerp.multi_process and db_name in cls.registries:
# Check the registries if any cache has been cleared and signal it # Check the registries if any cache has been cleared and signal it
# through the database to other processes. # through the database to other processes.
registry = cls.get(db_name, pooljobs=False) registry = cls.get(db_name)
if registry.any_cache_cleared(): if registry.any_cache_cleared():
_logger.info("At least one model cache has been cleared, signaling through the database.") _logger.info("At least one model cache has been cleared, signaling through the database.")
cr = registry.db.cursor() cr = registry.db.cursor()
@ -327,7 +315,7 @@ class RegistryManager(object):
@classmethod @classmethod
def signal_registry_change(cls, db_name): def signal_registry_change(cls, db_name):
if openerp.multi_process and db_name in cls.registries: 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() cr = registry.db.cursor()
r = 1 r = 1
try: try:

View File

@ -28,22 +28,17 @@
from openerp.modules.registry import RegistryManager 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.""" """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 return registry.db, registry
def restart_pool(db_name, force_demo=False, status=None, update_module=False): 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.""" """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 registry = RegistryManager.new(db_name, force_demo, status, update_module)
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)
return registry.db, registry return registry.db, registry
def get_db(db_name): def get_db(db_name):
"""Return a database connection. The corresponding registry is initialized.""" """Return a database connection. The corresponding registry is initialized."""
return get_db_and_pool(db_name)[0] return get_db_and_pool(db_name)[0]

View File

@ -43,7 +43,7 @@ def cron_runner(number):
registries = openerp.modules.registry.RegistryManager.registries registries = openerp.modules.registry.RegistryManager.registries
_logger.debug('cron%d polling for jobs', number) _logger.debug('cron%d polling for jobs', number)
for db_name, registry in registries.items(): 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) # acquired = openerp.addons.base.ir.ir_cron.ir_cron._acquire_job(db_name)
# TODO why isnt openerp.addons.base defined ? # TODO why isnt openerp.addons.base defined ?
import sys import sys