3
votes

I want to deploy a Pubsub-triggered Cloud Function with message ordering: https://cloud.google.com/pubsub/docs/ordering

gcloud functions deploy doesn't have an option to set an --enable-message-ordering option: https://cloud.google.com/sdk/gcloud/reference/functions/deploy

Should I pre-create the subscription before deploying the function? If so, does Cloud Functions have a well-known format for how it matches to a subscription name? It seems maybe the format is: gcf-{function-name}-{region}-{topic-name}, but it also looks like the name format has changed over time, e.g. older deployed functions don't have the region name in the subscription. Is there a stable way to do this?

2

2 Answers

3
votes

You must create message ordering pub/sub and Cloud function manually.

Fisrt, Create a pub/sub topic, and then create a subscription that subscribes pub/sub topic with --enable-message-ordering

Second, Create a Cloud function that will serve ordered pub/sub messages.

Last, back to the pub/sub subscription, Edit delivery type to push and specify your cloud function endpoint.

enter image description here

So final diagram is like below.

Publisher -> Pub/sub topic -> Pub/sub subscriber -> Cloud function

You tried to make a connection Pub/sub topic with Cloud function directly.

But for message ordering, Pub/sub needs topic -> subscriber connection.

So only pub/sub topic -> pub/sub subscriber -> Cloud function connection can delivers ordered messages to your function.

0
votes

When declaring a Pub/Sub topic with a Cloud Function alike:

exports.pubsub = functions.pubsub.topic('some-topic').onPublish((message, context) => {}

The problem is that message ordering is only available for the subscription, but not for the topic.
On deploy a push subscription is automatically being created at: cloudpubsub/subscription/list (the one which starts with gcf-*). This only appears to work when manually subscribing to a topic: Enabling message ordering. I haven't yet tried if it would pick up a subscription with the same name; if everything fails, one still could record the messages and then order by timestamp or a "sequence token": https://www.youtube.com/watch?v=nQ9_Xur2aM4.