From c415a9d72f4e32c265058b94f256631e99f5f17d Mon Sep 17 00:00:00 2001 From: "Harry (Open ERP)" Date: Wed, 16 Sep 2009 14:50:36 +0530 Subject: [PATCH] [FIX]base : ir_cron : take minimun next_call from database for job waiting. if database does not has any job, it will take 1 day for job waiting bzr revid: hmo@tinyerp.com-20090916092036-pevdiwq24d42zanl --- bin/addons/base/ir/ir_cron.py | 38 ++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/bin/addons/base/ir/ir_cron.py b/bin/addons/base/ir/ir_cron.py index 7da956c36af..166a5781d44 100644 --- a/bin/addons/base/ir/ir_cron.py +++ b/bin/addons/base/ir/ir_cron.py @@ -27,8 +27,6 @@ import tools import pooler from osv import fields,osv -next_wait = 60 - _intervalTypes = { 'work_days': lambda interval: DateTime.RelativeDateTime(days=interval), 'days': lambda interval: DateTime.RelativeDateTime(days=interval), @@ -74,20 +72,18 @@ class ir_cron(osv.osv, netsvc.Agent): f = getattr(m, func) f(cr, uid, *args) - def _poolJobs(self, db_name, check=False): + def _poolJobs(self, db_name, check=False): try: db, pool = pooler.get_db_and_pool(db_name) except: - return False - + return False if pool._init: # retry in a few minutes - next_call = 600 + next_call = 600 else: - next_call = next_wait - cr = db.cursor() now = DateTime.now() try: + cr = db.cursor() cr.execute('select * from ir_cron where numbercall<>0 and active and nextcall<=now() order by priority') for job in cr.dictfetchall(): nextcall = DateTime.strptime(job['nextcall'], '%Y-%m-%d %H:%M:%S') @@ -115,8 +111,32 @@ class ir_cron(osv.osv, netsvc.Agent): # Can be improved to do at the min(min(nextcalls), time()+next_call) # But is this an improvement ? # + cr = db.cursor() + cr.execute('select min(nextcall) as min_next_call from ir_cron where numbercall<>0 and active and nextcall>=now()') + next_call = cr.dictfetchone()['min_next_call'] + cr.close() + if next_call: + next_call = time.mktime(time.strptime(next_call, '%Y-%m-%d %H:%M:%S')) + else: + next_call = int(time.time()) + 3600 # if do not find active cron job from database, it will run again after 1 day if not check: - self.setAlarm(self._poolJobs, int(time.time()) + next_call, db_name, db_name) + self.setAlarm(self._poolJobs, next_call, db_name, db_name) + + def create(self, cr, uid, vals, context=None): + res = super(ir_cron, self).create(cr, uid, vals, context=context) + cr.commit() + self._poolJobs(cr.dbname) + return res + def write(self, cr, user, ids, vals, context=None): + res = super(ir_cron, self).write(cr, user, ids, vals, context=context) + cr.commit() + self._poolJobs(cr.dbname) + return res + def unlink(self, cr, uid, ids, context=None): + res = super(ir_cron, self).unlink(cr, uid, ids, context=context) + cr.commit() + self._poolJobs(cr.dbname) + return res ir_cron() # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: