[FIX] cron: avoid multiple cron
bzr revid: chs@openerp.com-20100906152927-7xhtr8q4vqjqc26h
This commit is contained in:
parent
d429b331e0
commit
0f8e08d617
|
@ -1,7 +1,7 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
##############################################################################
|
##############################################################################
|
||||||
#
|
#
|
||||||
# OpenERP, Open Source Management Solution
|
# OpenERP, Open Source Management Solution
|
||||||
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
|
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>). All Rights Reserved
|
||||||
# $Id$
|
# $Id$
|
||||||
#
|
#
|
||||||
|
@ -50,7 +50,7 @@ class ir_cron(osv.osv, netsvc.Agent):
|
||||||
'interval_number': fields.integer('Interval Number'),
|
'interval_number': fields.integer('Interval Number'),
|
||||||
'interval_type': fields.selection( [('minutes', 'Minutes'),
|
'interval_type': fields.selection( [('minutes', 'Minutes'),
|
||||||
('hours', 'Hours'), ('work_days','Work Days'), ('days', 'Days'),('weeks', 'Weeks'), ('months', 'Months')], 'Interval Unit'),
|
('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'),
|
'doall' : fields.boolean('Repeat Missed'),
|
||||||
'nextcall' : fields.datetime('Next Call Date', required=True),
|
'nextcall' : fields.datetime('Next Call Date', required=True),
|
||||||
'model': fields.char('Object', size=64),
|
'model': fields.char('Object', size=64),
|
||||||
|
@ -77,7 +77,7 @@ class ir_cron(osv.osv, netsvc.Agent):
|
||||||
except:
|
except:
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
_constraints = [
|
_constraints = [
|
||||||
(_check_args, 'Invalid arguments', ['args']),
|
(_check_args, 'Invalid arguments', ['args']),
|
||||||
]
|
]
|
||||||
|
@ -98,7 +98,7 @@ class ir_cron(osv.osv, netsvc.Agent):
|
||||||
try:
|
try:
|
||||||
db, pool = pooler.get_db_and_pool(db_name)
|
db, pool = pooler.get_db_and_pool(db_name)
|
||||||
except:
|
except:
|
||||||
return False
|
return False
|
||||||
cr = db.cursor()
|
cr = db.cursor()
|
||||||
try:
|
try:
|
||||||
if not pool._init:
|
if not pool._init:
|
||||||
|
@ -107,7 +107,7 @@ class ir_cron(osv.osv, netsvc.Agent):
|
||||||
for job in cr.dictfetchall():
|
for job in cr.dictfetchall():
|
||||||
nextcall = datetime.strptime(job['nextcall'], '%Y-%m-%d %H:%M:%S')
|
nextcall = datetime.strptime(job['nextcall'], '%Y-%m-%d %H:%M:%S')
|
||||||
numbercall = job['numbercall']
|
numbercall = job['numbercall']
|
||||||
|
|
||||||
ok = False
|
ok = False
|
||||||
while nextcall < now and numbercall:
|
while nextcall < now and numbercall:
|
||||||
if numbercall > 0:
|
if numbercall > 0:
|
||||||
|
@ -125,12 +125,12 @@ 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()')
|
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']
|
next_call = cr.dictfetchone()['min_next_call']
|
||||||
if next_call:
|
if next_call:
|
||||||
next_call = time.mktime(time.strptime(next_call, '%Y-%m-%d %H:%M:%S'))
|
next_call = time.mktime(time.strptime(next_call, '%Y-%m-%d %H:%M:%S'))
|
||||||
else:
|
else:
|
||||||
next_call = int(time.time()) + 3600 # if do not find active cron job from database, it will run again after 1 day
|
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:
|
if not check:
|
||||||
self.setAlarm(self._poolJobs, next_call, db_name, db_name)
|
self.setAlarm(self._poolJobs, next_call, db_name, db_name)
|
||||||
|
|
||||||
|
@ -138,29 +138,31 @@ class ir_cron(osv.osv, netsvc.Agent):
|
||||||
logger = netsvc.Logger()
|
logger = netsvc.Logger()
|
||||||
logger.notifyChannel('cron', netsvc.LOG_WARNING,
|
logger.notifyChannel('cron', netsvc.LOG_WARNING,
|
||||||
'Exception in cron:'+str(ex))
|
'Exception in cron:'+str(ex))
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
cr.commit()
|
cr.commit()
|
||||||
cr.close()
|
cr.close()
|
||||||
|
|
||||||
|
def restart(self, dbname):
|
||||||
|
self.cancel(dbname)
|
||||||
|
self._poolJobs(dbname)
|
||||||
|
|
||||||
def create(self, cr, uid, vals, context=None):
|
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()
|
cr.commit()
|
||||||
self.cancel(cr.dbname)
|
self.restart(cr.dbname)
|
||||||
self._poolJobs(cr.dbname)
|
|
||||||
return res
|
return res
|
||||||
|
|
||||||
def write(self, cr, user, ids, vals, context=None):
|
def write(self, cr, user, ids, vals, context=None):
|
||||||
res = super(ir_cron, self).write(cr, user, ids, vals, context=context)
|
res = super(ir_cron, self).write(cr, user, ids, vals, context=context)
|
||||||
cr.commit()
|
cr.commit()
|
||||||
self.cancel(cr.dbname)
|
self.restart(cr.dbname)
|
||||||
self._poolJobs(cr.dbname)
|
|
||||||
return res
|
return res
|
||||||
|
|
||||||
def unlink(self, cr, uid, ids, context=None):
|
def unlink(self, cr, uid, ids, context=None):
|
||||||
res = super(ir_cron, self).unlink(cr, uid, ids, context=context)
|
res = super(ir_cron, self).unlink(cr, uid, ids, context=context)
|
||||||
cr.commit()
|
cr.commit()
|
||||||
self.cancel(cr.dbname)
|
self.restart(cr.dbname)
|
||||||
self._poolJobs(cr.dbname)
|
|
||||||
return res
|
return res
|
||||||
ir_cron()
|
ir_cron()
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
##############################################################################
|
##############################################################################
|
||||||
#
|
#
|
||||||
# OpenERP, Open Source Management Solution
|
# OpenERP, Open Source Management Solution
|
||||||
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
|
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
|
||||||
#
|
#
|
||||||
|
@ -15,7 +15,7 @@
|
||||||
# GNU Affero General Public License for more details.
|
# GNU Affero General Public License for more details.
|
||||||
#
|
#
|
||||||
# You should have received a copy of the GNU Affero General Public License
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
#
|
#
|
||||||
##############################################################################
|
##############################################################################
|
||||||
|
|
||||||
|
@ -34,10 +34,10 @@ def get_db_and_pool(db_name, force_demo=False, status=None, update_module=False,
|
||||||
import osv.osv
|
import osv.osv
|
||||||
pool = osv.osv.osv_pool()
|
pool = osv.osv.osv_pool()
|
||||||
pool_dic[db_name] = pool
|
pool_dic[db_name] = pool
|
||||||
|
|
||||||
try:
|
try:
|
||||||
addons.load_modules(db, force_demo, status, update_module)
|
addons.load_modules(db, force_demo, status, update_module)
|
||||||
except Exception, e:
|
except Exception:
|
||||||
del pool_dic[db_name]
|
del pool_dic[db_name]
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
@ -51,13 +51,12 @@ def get_db_and_pool(db_name, force_demo=False, status=None, update_module=False,
|
||||||
import report
|
import report
|
||||||
report.interface.register_all(db)
|
report.interface.register_all(db)
|
||||||
if pooljobs:
|
if pooljobs:
|
||||||
pool.get('ir.cron')._poolJobs(db.dbname)
|
pool.get('ir.cron').restart(db.dbname)
|
||||||
return db, pool
|
return db, pool
|
||||||
|
|
||||||
|
|
||||||
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):
|
||||||
if db_name in pool_dic:
|
if db_name in pool_dic:
|
||||||
pool_dic[db_name].get('ir.cron').cancel(db_name)
|
|
||||||
del pool_dic[db_name]
|
del pool_dic[db_name]
|
||||||
return get_db_and_pool(db_name, force_demo, status, update_module=update_module)
|
return get_db_and_pool(db_name, force_demo, status, update_module=update_module)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue