From 1ff7ed275845589ea8b8d27c7dbd17e5a282626e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Wirtel?= Date: Thu, 12 Jun 2014 15:08:33 +0200 Subject: [PATCH] [IMP] Optimize the schedulers of the calendar, fetchmail and base_action_rules modules Disable the the crons when they'd have nothing to do, re-enable them when a task is (probably) introduced --- addons/base_action_rule/__openerp__.py | 2 +- addons/base_action_rule/base_action_rule.py | 16 +++++++ .../base_action_rule_data.xml | 1 + addons/calendar/__openerp__.py | 1 + addons/calendar/calendar.py | 45 ++++++++++++++++--- addons/calendar/calendar_cron.xml | 18 ++++++++ addons/calendar/calendar_data.xml | 17 +------ addons/fetchmail/__openerp__.py | 2 +- addons/fetchmail/fetchmail.py | 34 ++++++++------ openerp/addons/base/ir/ir_cron.py | 16 +++++++ 10 files changed, 114 insertions(+), 38 deletions(-) create mode 100644 addons/calendar/calendar_cron.xml diff --git a/addons/base_action_rule/__openerp__.py b/addons/base_action_rule/__openerp__.py index 5dd4b057514..d1bed2f1a27 100644 --- a/addons/base_action_rule/__openerp__.py +++ b/addons/base_action_rule/__openerp__.py @@ -37,9 +37,9 @@ trigger an automatic reminder email. 'website': 'http://www.openerp.com', 'depends': ['base', 'resource', 'mail'], 'data': [ + 'base_action_rule_data.xml', 'base_action_rule_view.xml', 'security/ir.model.access.csv', - 'base_action_rule_data.xml' ], 'demo': [], 'installable': True, diff --git a/addons/base_action_rule/base_action_rule.py b/addons/base_action_rule/base_action_rule.py index 5189bcbc7df..e74e7193b62 100644 --- a/addons/base_action_rule/base_action_rule.py +++ b/addons/base_action_rule/base_action_rule.py @@ -227,10 +227,20 @@ class base_action_rule(osv.osv): updated = True return updated + def _update_cron(self, cr, uid, context=None): + try: + cron = self.pool['ir.model.data'].get_object( + cr, uid, 'base_action_rule', 'ir_cron_crm_action', context=context) + except ValueError: + return False + + return cron.toggle(model=self._name, domain=[('kind', '=', 'on_time')]) + def create(self, cr, uid, vals, context=None): res_id = super(base_action_rule, self).create(cr, uid, vals, context=context) if self._register_hook(cr, [res_id]): openerp.modules.registry.RegistryManager.signal_registry_change(cr.dbname) + self._update_cron(cr, uid, context=context) return res_id def write(self, cr, uid, ids, vals, context=None): @@ -239,8 +249,14 @@ class base_action_rule(osv.osv): super(base_action_rule, self).write(cr, uid, ids, vals, context=context) if self._register_hook(cr, ids): openerp.modules.registry.RegistryManager.signal_registry_change(cr.dbname) + self._update_cron(cr, uid, context=context) return True + def unlink(self, cr, uid, ids, context=None): + res = super(base_action_rule, self).unlink(cr, uid, ids, context=context) + self._update_cron(cr, uid, context=context) + return res + def onchange_model_id(self, cr, uid, ids, model_id, context=None): data = {'model': False, 'filter_pre_id': False, 'filter_id': False} if model_id: diff --git a/addons/base_action_rule/base_action_rule_data.xml b/addons/base_action_rule/base_action_rule_data.xml index d4b12f5d4a3..78988554e86 100644 --- a/addons/base_action_rule/base_action_rule_data.xml +++ b/addons/base_action_rule/base_action_rule_data.xml @@ -11,6 +11,7 @@ + diff --git a/addons/calendar/__openerp__.py b/addons/calendar/__openerp__.py index 2f06a0b5cb9..69ea5908083 100644 --- a/addons/calendar/__openerp__.py +++ b/addons/calendar/__openerp__.py @@ -40,6 +40,7 @@ If you need to manage your meetings, you should install the CRM module. 'website': 'http://www.openerp.com', 'demo': ['calendar_demo.xml'], 'data': [ + 'calendar_cron.xml', 'security/calendar_security.xml', 'security/ir.model.access.csv', 'calendar_view.xml', diff --git a/addons/calendar/calendar.py b/addons/calendar/calendar.py index 5420f67acc0..6903e698e6a 100644 --- a/addons/calendar/calendar.py +++ b/addons/calendar/calendar.py @@ -431,11 +431,12 @@ class calendar_alarm_manager(osv.AbstractModel): return res def get_next_mail(self, cr, uid, context=None): - cron = self.pool.get('ir.cron').search(cr, uid, [('model', 'ilike', self._name)], context=context) - if cron and len(cron) == 1: - cron = self.pool.get('ir.cron').browse(cr, uid, cron[0], context=context) - else: - _logger.exception("Cron for " + self._name + " can not be identified !") + try: + cron = self.pool['ir.model.data'].get_object( + cr, uid, 'calendar', 'ir_cron_scheduler_alarm', context=context) + except ValueError: + _logger.error("Cron for " + self._name + " can not be identified !") + return False if cron.interval_type == "weeks": cron_interval = cron.interval_number * 7 * 24 * 60 * 60 @@ -447,9 +448,12 @@ class calendar_alarm_manager(osv.AbstractModel): cron_interval = cron.interval_number * 60 elif cron.interval_type == "seconds": cron_interval = cron.interval_number + else: + cron_interval = False if not cron_interval: - _logger.exception("Cron delay can not be computed !") + _logger.error("Cron delay can not be computed !") + return False all_events = self.get_next_potential_limit_alarm(cr, uid, cron_interval, notif=False, context=context) @@ -574,6 +578,35 @@ class calendar_alarm(osv.Model): 'interval': 'hours', } + def _update_cron(self, cr, uid, context=None): + try: + cron = self.pool['ir.model.data'].get_object( + cr, uid, 'calendar', 'ir_cron_scheduler_alarm', context=context) + except ValueError: + return False + return cron.toggle(model=self._name, domain=[('type', '=', 'email')]) + + def create(self, cr, uid, values, context=None): + res = super(calendar_alarm, self).create(cr, uid, values, context=context) + + self._update_cron(cr, uid, context=context) + + return res + + def write(self, cr, uid, ids, values, context=None): + res = super(calendar_alarm, self).write(cr, uid, ids, values, context=context) + + self._update_cron(cr, uid, context=context) + + return res + + def unlink(self, cr, uid, ids, context=None): + res = super(calendar_alarm, self).unlink(cr, uid, ids, context=context) + + self._update_cron(cr, uid, context=context) + + return res + class ir_values(osv.Model): _inherit = 'ir.values' diff --git a/addons/calendar/calendar_cron.xml b/addons/calendar/calendar_cron.xml new file mode 100644 index 00000000000..313a0211fa4 --- /dev/null +++ b/addons/calendar/calendar_cron.xml @@ -0,0 +1,18 @@ + + + + + + Run Event Reminder + + + 30 + minutes + -1 + + + + + + + \ No newline at end of file diff --git a/addons/calendar/calendar_data.xml b/addons/calendar/calendar_data.xml index 822236b033e..143ffad0d7f 100644 --- a/addons/calendar/calendar_data.xml +++ b/addons/calendar/calendar_data.xml @@ -71,22 +71,7 @@ days email - - - - - Run Event Reminder - - - 30 - minutes - -1 - - - - - - + Customer Meeting diff --git a/addons/fetchmail/__openerp__.py b/addons/fetchmail/__openerp__.py index 84a26d5ec40..082d53df5e9 100644 --- a/addons/fetchmail/__openerp__.py +++ b/addons/fetchmail/__openerp__.py @@ -56,8 +56,8 @@ For more specific needs, you may also assign custom-defined actions """, 'website': 'http://www.openerp.com', 'data': [ - 'fetchmail_view.xml', 'fetchmail_data.xml', + 'fetchmail_view.xml', 'security/ir.model.access.csv', 'fetchmail_installer_view.xml' ], diff --git a/addons/fetchmail/fetchmail.py b/addons/fetchmail/fetchmail.py index 565996bf961..37a0f165435 100644 --- a/addons/fetchmail/fetchmail.py +++ b/addons/fetchmail/fetchmail.py @@ -251,27 +251,33 @@ openerp_mailgate: "|/path/to/openerp-mailgate.py --host=localhost -u %(uid)d -p server.write({'date': time.strftime(tools.DEFAULT_SERVER_DATETIME_FORMAT)}) return True - def cron_update(self, cr, uid, context=None): - if context is None: - context = {} - if not context.get('fetchmail_cron_running'): - # Enabled/Disable cron based on the number of 'done' server of type pop or imap - ids = self.search(cr, uid, [('state','=','done'),('type','in',['pop','imap'])]) - try: - cron_id = self.pool.get('ir.model.data').get_object_reference(cr, uid, 'fetchmail', 'ir_cron_mail_gateway_action')[1] - self.pool.get('ir.cron').write(cr, 1, [cron_id], {'active': bool(ids)}) - except ValueError: - # Nevermind if default cron cannot be found - pass + def _update_cron(self, cr, uid, context=None): + if context and context.get('fetchmail_cron_running'): + return + + try: + cron = self.pool['ir.model.data'].get_object( + cr, uid, 'fetchmail', 'ir_cron_mail_gateway_action', context=context) + except ValueError: + # Nevermind if default cron cannot be found + return + + # Enabled/Disable cron based on the number of 'done' server of type pop or imap + cron.toggle(model=self._name, domain=[('state','=','done'), ('type','in',['pop','imap'])]) def create(self, cr, uid, values, context=None): res = super(fetchmail_server, self).create(cr, uid, values, context=context) - self.cron_update(cr, uid, context=context) + self._update_cron(cr, uid, context=context) return res def write(self, cr, uid, ids, values, context=None): res = super(fetchmail_server, self).write(cr, uid, ids, values, context=context) - self.cron_update(cr, uid, context=context) + self._update_cron(cr, uid, context=context) + return res + + def unlink(self, cr, uid, ids, context=None): + res = super(fetchmail_server, self).unlink(cr, uid, ids, context=context) + self._update_cron(cr, uid, context=context) return res class mail_mail(osv.osv): diff --git a/openerp/addons/base/ir/ir_cron.py b/openerp/addons/base/ir/ir_cron.py index 8f85beb820c..029c2471f49 100644 --- a/openerp/addons/base/ir/ir_cron.py +++ b/openerp/addons/base/ir/ir_cron.py @@ -294,4 +294,20 @@ class ir_cron(osv.osv): res = super(ir_cron, self).unlink(cr, uid, ids, context=context) return res + def try_write(self, cr, uid, ids, values, context=None): + try: + with cr.savepoint(): + cr.execute("""SELECT id FROM "%s" WHERE id IN %%s FOR UPDATE NOWAIT""" % self._table, + (tuple(ids),), log_exceptions=False) + except psycopg2.OperationalError: + pass + else: + return super(ir_cron, self).write(cr, uid, ids, values, context=context) + return False + + def toggle(self, cr, uid, ids, model, domain, context=None): + active = bool(self.pool[model].search_count(cr, uid, domain, context=context)) + + return self.try_write(cr, uid, ids, {'active': active}, context=context) + # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: