db: logging option for cursor
In some cases we expect query to fail (LOCK NOWAIT) but it still clutters the logs. Option for not logging cursor-level errors. Fixes: https://pagure.io/koji/issue/2837
This commit is contained in:
parent
bc04708990
commit
e7db7d8b53
3 changed files with 10 additions and 6 deletions
|
|
@ -5421,10 +5421,13 @@ def _singleValue(query, values=None, strict=True):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def _dml(operation, values):
|
def _dml(operation, values, log=True):
|
||||||
"""Run an insert, update, or delete. Return number of rows affected"""
|
"""Run an insert, update, or delete. Return number of rows affected
|
||||||
|
If log is False, errors will not be logged. It makes sense only for
|
||||||
|
queries which are expected to fail (LOCK NOWAIT)
|
||||||
|
"""
|
||||||
c = context.cnx.cursor()
|
c = context.cnx.cursor()
|
||||||
c.execute(operation, values)
|
c.execute(operation, values, log=log)
|
||||||
ret = c.rowcount
|
ret = c.rowcount
|
||||||
logger.debug("Operation affected %s row(s)", ret)
|
logger.debug("Operation affected %s row(s)", ret)
|
||||||
c.close()
|
c.close()
|
||||||
|
|
|
||||||
|
|
@ -132,7 +132,7 @@ class CursorWrapper:
|
||||||
params[i] = tuple(item)
|
params[i] = tuple(item)
|
||||||
return sql, params
|
return sql, params
|
||||||
|
|
||||||
def execute(self, operation, parameters=()):
|
def execute(self, operation, parameters=(), log=True):
|
||||||
debug = self.logger.isEnabledFor(logging.DEBUG)
|
debug = self.logger.isEnabledFor(logging.DEBUG)
|
||||||
operation, parameters = self.preformat(operation, parameters)
|
operation, parameters = self.preformat(operation, parameters)
|
||||||
if debug:
|
if debug:
|
||||||
|
|
@ -141,7 +141,8 @@ class CursorWrapper:
|
||||||
try:
|
try:
|
||||||
ret = self.cursor.execute(operation, parameters)
|
ret = self.cursor.execute(operation, parameters)
|
||||||
except Exception:
|
except Exception:
|
||||||
self.logger.error('Query failed. Query was: %s', self.quote(operation, parameters))
|
if log:
|
||||||
|
self.logger.error('Query failed. Query was: %s', self.quote(operation, parameters))
|
||||||
raise
|
raise
|
||||||
if debug:
|
if debug:
|
||||||
self.logger.debug("Execute operation completed in %.4f seconds", time.time() - start)
|
self.logger.debug("Execute operation completed in %.4f seconds", time.time() - start)
|
||||||
|
|
|
||||||
|
|
@ -356,7 +356,7 @@ def handle_db_msgs(urls, CONFIG):
|
||||||
# we're running in postCommit, so we need to handle new transaction
|
# we're running in postCommit, so we need to handle new transaction
|
||||||
c.execute('BEGIN')
|
c.execute('BEGIN')
|
||||||
try:
|
try:
|
||||||
c.execute('LOCK TABLE proton_queue IN ACCESS EXCLUSIVE MODE NOWAIT')
|
c.execute('LOCK TABLE proton_queue IN ACCESS EXCLUSIVE MODE NOWAIT', log=False)
|
||||||
except psycopg2.OperationalError:
|
except psycopg2.OperationalError:
|
||||||
LOG.debug('skipping db queue due to lock')
|
LOG.debug('skipping db queue due to lock')
|
||||||
return
|
return
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue