pubsub Message class does not set timestamp
pubsub.Message timestamps are not set. There are two object properties, neither of them is set. What's even more confusing is that attempting to access an attribute of an instance throws an error.
To reproduce:
from gcloud import pubsub
t = pubsub.Client(project='...').topic('${TOPIC}')
# MAY NEED t.create()
s = t.subscription('${SUBSCRIPTION}')
# MAY NEED s.create()
t.publish('test data') # RETURNS A MSG ID
ack_id, m = s.pull() # WAS t.pull()
Trying to repro right now.
@kir-google-cloud-pubsub I can't reproduce. ack_id, m = s.pull() is essentially invalid, but maybe you meant to say
pairs = s.pull()
ack_id, m = pairs[0]
When I run the slightly modified code, I get
>>> pub_id = t.publish('test data')
>>> pub_id
u'44126525330808'
>>> msgs = s.pull()
>>> assert len(msgs) == 1
>>> ack_id, m = msgs[0]
>>> m.message_id
u'44126525330808'
>>> m.message_id == pub_id
True
>>> m.data
'test data'
Any more details you can provide?
You are right -- about the mistake I meant. In your code, try m.timestamp -- it should be set, but is not.
also, m.service_timestamp
There are two timestamps possible on a message:
message.timestampis in theattributesof the message, and must therefore be set by the topic: setting those is controlled by thetopic.timestamp_messagesattribute, which defaults toFalse.message.service_timestampis set by the back-end, and parsed from thepublishTimefield of the pulled message resource.
To activate the first kind:
from gcloud.pubsub.client import Client
client = Client(project='...')
topic = client.topic('${TOPIC}', timestamp_messages=True)
t.create()
s = t.subscription('${SUBSCRIPTION}')
s.create()
t.publish('test data')
ack_ids = []
for ack_id, m in s.pull():
ack_ids.append(ack_id)
print(m.data, m.timestamp, m.service_timetamp)
sub.acknowledge(ack_ids)
On my box, the previous example yields:
(datetime.datetime(2016, 9, 7, 20, 42, 18, 810563, tzinfo=<UTC>), 'test_data', None)
That tells me that for some reason the back-end is not setting the publishTime field in the message resource it is returning, but that the timestamp attribute set by the publishing application is being set.
It appears that the GAX/gRPC adapter for pubsub is not mediating between the publish_time field in the PublishedMessage proto and the expected publishTime field (as returned from the JSON-over-HTTP API).