[FIX] cron: avoid multiple cron
authorChristophe Simonis <chs@openerp.com>
Mon, 6 Sep 2010 15:28:25 +0000 (17:28 +0200)
committerChristophe Simonis <chs@openerp.com>
Mon, 6 Sep 2010 15:28:25 +0000 (17:28 +0200)
bzr revid: chs@openerp.com-20100906152825-l24ehbwx2c9y5xz9

bin/addons/base/ir/ir_cron.py
bin/pooler.py

index 6b553ff..712cce5 100644 (file)
@@ -1,7 +1,7 @@
 # -*- encoding: utf-8 -*-
 ##############################################################################
 #
-#    OpenERP, Open Source Management Solution  
+#    OpenERP, Open Source Management Solution
 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
 #    $Id$
 #
@@ -49,7 +49,7 @@ class ir_cron(osv.osv, netsvc.Agent):
         'interval_number': fields.integer('Interval Number'),
         'interval_type': fields.selection( [('minutes', 'Minutes'),
             ('hours', 'Hours'), ('work_days','Work Days'), ('days', 'Days'),('weeks', 'Weeks'), ('months', 'Months')], 'Interval Unit'),
-        'numbercall': fields.integer('Number of Calls', help='Number of time the function is called,\na negative number indicates that the function will always be called'),        
+        'numbercall': fields.integer('Number of Calls', help='Number of time the function is called,\na negative number indicates that the function will always be called'),
         'doall' : fields.boolean('Repeat Missed'),
         'nextcall' : fields.datetime('Next Call Date', required=True),
         'model': fields.char('Object', size=64),
@@ -76,7 +76,7 @@ class ir_cron(osv.osv, netsvc.Agent):
         except:
             return False
         return True
-    
+
     _constraints= [
         (_check_args, 'Invalid arguments', ['args']),
     ]
@@ -93,11 +93,11 @@ class ir_cron(osv.osv, netsvc.Agent):
                 self._logger.notifyChannel('timers', netsvc.LOG_ERROR, tools.exception_to_unicode(e))
 
 
-    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
         cr = db.cursor()
         try:
             if not pool._init:
@@ -106,7 +106,7 @@ class ir_cron(osv.osv, netsvc.Agent):
                 for job in cr.dictfetchall():
                     nextcall = DateTime.strptime(job['nextcall'], '%Y-%m-%d %H:%M:%S')
                     numbercall = job['numbercall']
-                
+
                     ok = False
                     while nextcall < now and numbercall:
                         if numbercall > 0:
@@ -124,37 +124,39 @@ class ir_cron(osv.osv, netsvc.Agent):
 
 
             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']  
-            if next_call:                
+            next_call = cr.dictfetchone()['min_next_call']
+            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, next_call, db_name, db_name)
-        
+
         finally:
             cr.commit()
             cr.close()
 
-            
+    def restart(self, dbname):
+        self.cancel(dbname)
+        self._poolJobs(dbname)
+
     def create(self, cr, uid, vals, context=None):
-        res = super(ir_cron, self).create(cr, uid, vals, context=context)        
+        res = super(ir_cron, self).create(cr, uid, vals, context=context)
         cr.commit()
-        self.cancel(cr.dbname)
-        self._poolJobs(cr.dbname)
+        self.restart(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.cancel(cr.dbname)
-        self._poolJobs(cr.dbname)
+        self.restart(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.cancel(cr.dbname)
-        self._poolJobs(cr.dbname)
+        self.restart(cr.dbname)
         return res
 ir_cron()
 
index 32b45db..75625f1 100644 (file)
@@ -1,7 +1,7 @@
 # -*- encoding: utf-8 -*-
 ##############################################################################
 #
-#    OpenERP, Open Source Management Solution  
+#    OpenERP, Open Source Management Solution
 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
 #    $Id$
 #
@@ -25,7 +25,7 @@ pool_dic = {}
 def get_db_and_pool(db_name, force_demo=False, status=None, update_module=False):
     if not status:
         status={}
+
     db = get_db_only(db_name)
 
     if db_name in pool_dic:
@@ -35,10 +35,10 @@ def get_db_and_pool(db_name, force_demo=False, status=None, update_module=False)
         import osv.osv
         pool = osv.osv.osv_pool()
         pool_dic[db_name] = pool
-        
+
         try:
             addons.load_modules(db, force_demo, status, update_module)
-        except Exception, e:
+        except Exception:
             del pool_dic[db_name]
             raise
 
@@ -51,13 +51,12 @@ def get_db_and_pool(db_name, force_demo=False, status=None, update_module=False)
 
         import report
         report.interface.register_all(db)
-        pool.get('ir.cron')._poolJobs(db.dbname)
+        pool.get('ir.cron').restart(db.dbname)
     return db, pool
 
 
 def restart_pool(db_name, force_demo=False, status=None, update_module=False):
     if db_name in pool_dic:
-        pool_dic[db_name].get('ir.cron').cancel(db_name)
         del pool_dic[db_name]
     return get_db_and_pool(db_name, force_demo, status, update_module=update_module)