Repository navigation
Conversation
Consuming leased messages from RECEIVE_<Queue> and acknowledging active leases via batch.ack lacked a runnable Python worker sample.
|
Here is the summary of changes. You are about to add 1 region tag.
This comment is generated by snippet-bot.
|
There was a problem hiding this comment.
Code Review
This pull request introduces functionality to receive and acknowledge messages from a Cloud Spanner queue, specifically querying the RECEIVE_UserTasks table-valued function, validating lease expirations, and acknowledging valid messages. It also includes comprehensive unit and integration tests. The review feedback recommends optimizing the message acknowledgement loop by wrapping it in a single database.batch() context manager to batch all acknowledgements into a single transaction rather than committing them individually.
| for key in valid_keys: | ||
| with database.batch() as batch: | ||
| batch.ack(queue=queue_name, key=key, ignore_not_found=True) | ||
| acked_count += 1 |
There was a problem hiding this comment.
Creating a new database.batch() context manager inside the loop commits each acknowledgement individually, resulting in multiple network roundtrips. Instead, you should wrap the loop inside a single database.batch() block to batch all acknowledgements into a single transaction/commit. Additionally, checking if valid_keys is not empty before entering the batch context prevents an unnecessary empty commit.
| for key in valid_keys: | |
| with database.batch() as batch: | |
| batch.ack(queue=queue_name, key=key, ignore_not_found=True) | |
| acked_count += 1 | |
| if valid_keys: | |
| with database.batch() as batch: | |
| for key in valid_keys: | |
| batch.ack(queue=queue_name, key=key, ignore_not_found=True) | |
| acked_count += 1 |
There was a problem hiding this comment.
Thanks! This comment refers to an earlier revision of this file; the code here has since been reworked in later commits, so this thread no longer applies to the current diff.
…roject_id Commits all valid message acknowledgements in a single batch Commit RPC and omits project identifiers from stdout and stderr.
Problem: reviewers of this sample set asked for stricter and clearer tests and for a single source of truth for dependencies. Mocks were created without a spec, so a renamed client argument would pass silently; the mocked wait timeout was raised from the API call instead of from the operation's result; live tests did not say which environment variables they need; and some tests carried leftover or repeated lines. Package dependencies were pinned in both pyproject.toml and requirements.txt. Solution: where applicable to the files below, patch client classes with autospec=True, raise the mocked TimeoutError from operation.result(), document the required environment variables above each live test and skip when one is missing, remove unused or duplicated test lines, and join string literals that the formatter had left split on one line. For package scaffolding, read dependencies from requirements.txt and update the README install command to match. Files updated: - spanner/queues/tests/queue_receive_messages_test.py
Description
Consuming messages from a Cloud Spanner Queue requires streaming from
RECEIVE_<Queue>under a strong snapshot, verifyingSpannerLeaseExpirationTimestamp, and acknowledging processed keys viabatch.ack.Adds
spanner_queue_receive_messages.Checklist
Testing
Compliance & Style
spanner/, which the default repository owners cover.Post-Approval Actions