Add ignore events filter

This change enables GerritWatcher user to ignore noisy events
such as replicated-ref event.

Change-Id: If95d560a3dbb20e530e9ea1e94c1250aa083c9ea
This commit is contained in:
Tristan Cacqueray 2021-04-07 19:50:49 +00:00
parent 874d4776ee
commit d30b133a89
1 changed files with 13 additions and 6 deletions

View File

@ -114,7 +114,8 @@ class GerritWatcher(threading.Thread):
def __init__(
self, gerrit, username=None, hostname=None, port=None,
keyfile=None, keep_alive=0, connection_attempts=-1, retry_delay=5):
keyfile=None, keep_alive=0, connection_attempts=-1, retry_delay=5,
ignore_events=None):
"""Create a GerritWatcher.
:param gerrit: A GerritConnection instance to pass events to.
@ -134,6 +135,7 @@ class GerritWatcher(threading.Thread):
)
self.gerrit = gerrit
self.retry_delay = float(retry_delay)
self.ignore_events = ignore_events or set()
self.state = IDLE
def _read(self, fd):
@ -141,9 +143,12 @@ class GerritWatcher(threading.Thread):
if line:
try:
data = json.loads(line)
self.log.debug("Received data from Gerrit event stream: \n%s" %
pprint.pformat(data))
self.gerrit.addEvent(data)
if isinstance(data, dict) and \
data.get('type') not in self.ignore_events:
self.log.debug(
"Received data from Gerrit event stream: \n%s" %
pprint.pformat(data))
self.gerrit.addEvent(data)
except json.decoder.JSONDecodeError:
self.log.debug("Can not parse data from Gerrit event stream: \n%s" %
pprint.pformat(line))
@ -229,11 +234,13 @@ class Gerrit(object):
self.event_queue = None
self.installed_plugins = None
def startWatching(self, connection_attempts=-1, retry_delay=5):
def startWatching(
self, connection_attempts=-1, retry_delay=5, ignore_events=None):
self.event_queue = six.moves.queue.Queue()
watcher = GerritWatcher(self,
connection_attempts=connection_attempts,
retry_delay=retry_delay)
retry_delay=retry_delay,
ignore_events=ignore_events)
self.watcher_thread = watcher
self.watcher_thread.daemon = True
self.watcher_thread.start()