[FIX] netsvc.Agent, ir.cron: pythonic logging instead of legacy netsvc logging

bzr revid: odo@openerp.com-20110118180154-1ek9mmf8yarh4se5
This commit is contained in:
P. Christeas 2011-01-18 19:01:54 +01:00 committed by Olivier Dony
commit 0d691b2230
2 changed files with 10 additions and 10 deletions

View File

@ -41,6 +41,9 @@ _intervalTypes = {
} }
class ir_cron(osv.osv, netsvc.Agent): class ir_cron(osv.osv, netsvc.Agent):
""" This is the ORM object that periodically executes actions.
Note that we use the netsvc.Agent()._logger member.
"""
_name = "ir.cron" _name = "ir.cron"
_order = 'name' _order = 'name'
_columns = { _columns = {
@ -74,7 +77,7 @@ class ir_cron(osv.osv, netsvc.Agent):
try: try:
for this in self.browse(cr, uid, ids, context): for this in self.browse(cr, uid, ids, context):
str2tuple(this.args) str2tuple(this.args)
except: except Exception:
return False return False
return True return True
@ -90,8 +93,7 @@ class ir_cron(osv.osv, netsvc.Agent):
try: try:
f(cr, uid, *args) f(cr, uid, *args)
except Exception, e: except Exception, e:
self._logger.notifyChannel('timers', netsvc.LOG_ERROR, "Job call of self.pool.get('%s').%s(cr, uid, *%r) failed" % (model, func, args)) self._logger.exception("Job call of self.pool.get('%s').%s(cr, uid, *%r) failed" % (model, func, args))
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):
@ -135,9 +137,7 @@ class ir_cron(osv.osv, netsvc.Agent):
self.setAlarm(self._poolJobs, next_call, db_name, db_name) self.setAlarm(self._poolJobs, next_call, db_name, db_name)
except Exception, ex: except Exception, ex:
logger = netsvc.Logger() self._logger.warning('Exception in cron:', exc_info=True)
logger.notifyChannel('cron', netsvc.LOG_WARNING,
'Exception in cron:'+str(ex))
finally: finally:
cr.commit() cr.commit()

View File

@ -309,7 +309,7 @@ class Agent(object):
""" """
__tasks = [] __tasks = []
__tasks_by_db = {} __tasks_by_db = {}
_logger = Logger() _logger = logging.getLogger('netsvc.agent')
@classmethod @classmethod
def setAlarm(cls, function, timestamp, db_name, *args, **kwargs): def setAlarm(cls, function, timestamp, db_name, *args, **kwargs):
@ -320,6 +320,7 @@ class Agent(object):
@classmethod @classmethod
def cancel(cls, db_name): def cancel(cls, db_name):
"""Cancel all tasks for a given database. If None is passed, all tasks are cancelled""" """Cancel all tasks for a given database. If None is passed, all tasks are cancelled"""
cls._logger.debug("Cancel timers for %s db", db_name or 'all')
if db_name is None: if db_name is None:
cls.__tasks, cls.__tasks_by_db = [], {} cls.__tasks, cls.__tasks_by_db = [], {}
else: else:
@ -334,7 +335,7 @@ class Agent(object):
@classmethod @classmethod
def runner(cls): def runner(cls):
"""Neverending function (intended to be ran in a dedicated thread) that """Neverending function (intended to be ran in a dedicated thread) that
checks every 60 seconds tasks to run. checks every 60 seconds tasks to run. TODO: make configurable
""" """
current_thread = threading.currentThread() current_thread = threading.currentThread()
while True: while True:
@ -346,7 +347,7 @@ class Agent(object):
# null timestamp -> cancelled task # null timestamp -> cancelled task
continue continue
current_thread.dbname = dbname # hack hack current_thread.dbname = dbname # hack hack
cls._logger.notifyChannel('timers', LOG_DEBUG, "Run %s.%s(*%r, **%r)" % (function.im_class.__name__, function.func_name, args, kwargs)) cls._logger.debug("Run %s.%s(*%s, **%s)", function.im_class.__name__, function.func_name, args, kwargs)
delattr(current_thread, 'dbname') delattr(current_thread, 'dbname')
task_thread = threading.Thread(target=function, name='netsvc.Agent.task', args=args, kwargs=kwargs) task_thread = threading.Thread(target=function, name='netsvc.Agent.task', args=args, kwargs=kwargs)
# force non-daemon task threads (the runner thread must be daemon, and this property is inherited by default) # force non-daemon task threads (the runner thread must be daemon, and this property is inherited by default)
@ -362,7 +363,6 @@ agent_runner = threading.Thread(target=Agent.runner, name="netsvc.Agent.runner")
agent_runner.setDaemon(True) agent_runner.setDaemon(True)
agent_runner.start() agent_runner.start()
import traceback import traceback
class Server: class Server: