diff --git a/trytond/ir/cron.py b/trytond/ir/cron.py index e2ff0e8da5..c666714f78 100644 --- a/trytond/ir/cron.py +++ b/trytond/ir/cron.py @@ -96,16 +96,55 @@ class Cron(OSV): def check_xml_record(self, cursor, user, ids, values, context=None): return True - def _callback(self, cursor, user, job_id, model, func, args): - args = (args or []) and eval(args) - obj = self.pool.get(model) - if obj and hasattr(obj, func): - fct = getattr(obj, func) - fct(cursor, user, *args) + def _callback(self, cursor, cron): + try: + args = (cron['args'] or []) and eval(cron['args']) + obj = self.pool.get(cron['model']) + if not obj and hasattr(obj, cron['function']): + return False + fct = getattr(obj, cron['function']) + fct(cursor, cron['user'], *args) + except Exception, error: + cursor.rollback() + + tb_s = '' + for line in traceback.format_exception(*sys.exc_info()): + try: + line = line.encode('utf-8', 'ignore') + except: + continue + tb_s += line + try: + tb_s += error.message.decode('utf-8', 'ignore') + except: + pass + + request_obj = self.pool.get('res.request') + try: + rid = request_obj.create( + cursor, cron['user'], + {'name': self.raise_user_error( + cursor, 'request_title', raise_exception=False), + 'priority': '2', + 'act_from': cron['user'], + 'act_to': cron['request_user'], + 'body': self.raise_user_error( + cursor, 'request_body', (cron['name'], tb_s), + raise_exception=False), + 'date_sent': DateTime.now(), + 'references': [ + ('create',{'reference': "ir.cron,%s"%cron['id']})], + 'state': 'waiting', + 'trigger_date': DateTime.now(), + }) + cursor.commit() + except: + cursor.rollback() def pool_jobs(self, db_name): now = DateTime.now() cursor = pooler.get_db(db_name).cursor() + cursor.execute('LOCK TABLE ir_cron') cursor.execute('SELECT * FROM ir_cron ' \ 'WHERE numbercall <> 0 ' \ 'AND active ' \ @@ -115,68 +154,32 @@ class Cron(OSV): crons = cursor.dictfetchall() for cron in crons: - try: - cursor.execute('UPDATE ir_cron SET running = True ' \ - 'WHERE id = %s' % cron['id']) - nextcall = DateTime.strptime(str(cron['nextcall']), - '%Y-%m-%d %H:%M:%S') - numbercall = cron['numbercall'] - done = False - while nextcall < now and numbercall: - if numbercall > 0: - numbercall -= 1 - if not done or cron['doall']: - self._callback(cursor, cron['user'], cron['id'], cron['model'], - cron['function'], cron['args']) - if numbercall: - nextcall += _INTERVALTYPES[cron['interval_type']]( - cron['interval_number']) - done = True - addsql = '' - if not numbercall: - addsql = ', active=False' - cursor.execute("UPDATE ir_cron SET nextcall = %s, " \ - "running = False, numbercall = %s" + addsql + " " \ - "WHERE id = %s", - (nextcall.strftime('%Y-%m-%d %H:%M:%S'), - numbercall, cron['id'])) - cursor.commit() + cursor.execute('UPDATE ir_cron SET running = True ' \ + 'WHERE id = %s' % cron['id']) + nextcall = DateTime.strptime(str(cron['nextcall']), + '%Y-%m-%d %H:%M:%S') + numbercall = cron['numbercall'] + done = False - except Exception, error: - cursor.rollback() + while nextcall < now and numbercall: + if numbercall > 0: + numbercall -= 1 + if not done or cron['doall']: + self._callback(cursor, cron) + if numbercall: + nextcall += _INTERVALTYPES[cron['interval_type']]( + cron['interval_number']) + done = True - tb_s = '' - for line in traceback.format_exception(*sys.exc_info()): - try: - line = line.encode('utf-8', 'ignore') - except: - continue - tb_s += line - try: - tb_s += error.message.decode('utf-8', 'ignore') - except: - continue + addsql = '' + if not numbercall: + addsql = ', active=False' + cursor.execute("UPDATE ir_cron SET nextcall = %s, " \ + "running = False, numbercall = %s" + addsql + " " \ + "WHERE id = %s", + (nextcall.strftime('%Y-%m-%d %H:%M:%S'), + numbercall, cron['id'])) + cursor.commit() - request_obj = self.pool.get('res.request') - try: - request_obj.create( - cursor, cron['user'], - {'name': self.raise_user_error( - cursor, 'request_title', raise_exception=False), - 'priority': '2', - 'act_from': cron['user'], - 'act_to': cron['request_user'], - 'body': self.raise_user_error( - cursor, 'request_body', (cron['name'], tb_s), - raise_exception=False), - 'date_sent': now, - 'references': [ - ('create',{'reference': "ir.cron,%s"%cron['id']})], - 'state': 'waiting', - 'trigger_date': now, - }) - cursor.commit() - except: - cursor.rollback() cursor.close() Cron()