Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2880fef

Browse files
author
Jon Wayne Parrott
authored
Surface publish RPC errors back to the publish futures (googleapis#5124)
1 parent 3582164 commit 2880fef

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

pubsub/google/cloud/pubsub_v1/publisher/batch/thread.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import six
2222

23+
import google.api_core.exceptions
2324
from google.cloud.pubsub_v1 import types
2425
from google.cloud.pubsub_v1.publisher import exceptions
2526
from google.cloud.pubsub_v1.publisher import futures
@@ -199,10 +200,24 @@ def _commit(self):
199200
# Begin the request to publish these messages.
200201
# Log how long the underlying request takes.
201202
start = time.time()
202-
response = self._client.api.publish(
203-
self._topic,
204-
self._messages,
205-
)
203+
204+
try:
205+
response = self._client.api.publish(
206+
self._topic,
207+
self._messages,
208+
)
209+
except google.api_core.exceptions.GoogleAPICallError as exc:
210+
# We failed to publish, set the exception on all futures and
211+
# exit.
212+
self._status = base.BatchStatus.ERROR
213+
214+
for future in self._futures:
215+
future.set_exception(exc)
216+
217+
_LOGGER.exception(
218+
'Failed to publish %s messages.', len(self._futures))
219+
return
220+
206221
end = time.time()
207222
_LOGGER.debug('gRPC Publish took %s seconds.', end - start)
208223

@@ -220,9 +235,14 @@ def _commit(self):
220235
self._status = base.BatchStatus.ERROR
221236
exception = exceptions.PublishError(
222237
'Some messages were not successfully published.')
238+
223239
for future in self._futures:
224240
future.set_exception(exception)
225241

242+
_LOGGER.error(
243+
'Only %s of %s messages were published.',
244+
len(response.message_ids), len(self._futures))
245+
226246
def monitor(self):
227247
"""Commit this batch after sufficient time has elapsed.
228248

pubsub/tests/unit/pubsub_v1/publisher/batch/test_thread.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import mock
1919

20+
import google.api_core.exceptions
2021
from google.auth import credentials
2122
from google.cloud.pubsub_v1 import publisher
2223
from google.cloud.pubsub_v1 import types
@@ -201,6 +202,26 @@ def test_blocking__commit_wrong_messageid_length():
201202
assert isinstance(future.exception(), exceptions.PublishError)
202203

203204

205+
def test_block__commmit_api_error():
206+
batch = create_batch()
207+
futures = (
208+
batch.publish({'data': b'blah blah blah'}),
209+
batch.publish({'data': b'blah blah blah blah'}),
210+
)
211+
212+
# Make the API throw an error when publishing.
213+
error = google.api_core.exceptions.InternalServerError('uh oh')
214+
patch = mock.patch.object(
215+
type(batch.client.api), 'publish', side_effect=error)
216+
217+
with patch:
218+
batch._commit()
219+
220+
for future in futures:
221+
assert future.done()
222+
assert future.exception() == error
223+
224+
204225
def test_monitor():
205226
batch = create_batch(max_latency=5.0)
206227
with mock.patch.object(time, 'sleep') as sleep:

0 commit comments

Comments
 (0)