diff options
| author | David Pursehouse <david.pursehouse@sonymobile.com> | 2012-10-23 18:05:45 +0900 |
|---|---|---|
| committer | David Pursehouse <david.pursehouse@sonymobile.com> | 2012-10-23 18:05:45 +0900 |
| commit | bff3e4ddf010f1b6e0d0ac52effd9bceb2b21d7d (patch) | |
| tree | a5d05ea29048811e66910798b2127dc36f7ff08a /pygerrit/stream.py | |
| parent | 12a5109267d3a91e311ab92fd0ec2cc3482a13f1 (diff) | |
| download | pygerrit-bff3e4ddf010f1b6e0d0ac52effd9bceb2b21d7d.tar.gz | |
More error handling improvements in stream
Handle IOError and ValueError separately from GerritError when
processing the steam. In this case GerritError is raised if
there is a problem adding an event to the queue; it makes no
sense to then try to add an error event on the same queue.
Add separate handling of GerritError when running the gerrit
stream-events command.
Move the creation of the error event out to a utility method.
Change-Id: Iac5aa8db5ad1a444fd0e6217c32d1800f727276e
Diffstat (limited to 'pygerrit/stream.py')
| -rw-r--r-- | pygerrit/stream.py | 33 |
1 files changed, 22 insertions, 11 deletions
diff --git a/pygerrit/stream.py b/pygerrit/stream.py index 27ce86f..cc1247b 100644 --- a/pygerrit/stream.py +++ b/pygerrit/stream.py @@ -27,6 +27,7 @@ THE SOFTWARE. """ import json +import logging from select import poll, POLLIN from threading import Thread, Event @@ -63,22 +64,32 @@ class GerritStream(Thread): """ Stop the thread. """ self._stop.set() + def _error_event(self, error): + """ Dispatch `error` to the Gerrit client. """ + json_data = json.loads('{"type":"gerrit-stream-error",' + '"error":"%s"}' % str(error)) + self._gerrit.put_event(json_data) + def run(self): """ Listen to the stream and send events to the client. """ try: _stdin, stdout, _stderr = \ self._ssh_client.run_gerrit_command("stream-events") - poller = poll() - poller.register(stdout.channel) - while not self._stop.is_set(): - data = poller.poll() - for (handle, event) in data: - if handle == stdout.channel.fileno(): - if event == POLLIN: + except GerritError, e: + self._error_event(e) + + poller = poll() + poller.register(stdout.channel) + while not self._stop.is_set(): + data = poller.poll() + for (handle, event) in data: + if handle == stdout.channel.fileno(): + if event == POLLIN: + try: line = stdout.readline() json_data = json.loads(line) self._gerrit.put_event(json_data) - except (GerritError, ValueError, IOError), e: - error = json.loads('{"type":"gerrit-stream-error",' - '"error":"%s"}' % str(e)) - self._gerrit.put_event(error) + except (ValueError, IOError), err: + self._error_event(err) + except GerritError, err: + logging.error("Failed to put event: %s", err) |
