Search code examples
google-cloud-functionsgoogle-cloud-pubsubgoogle-cloud-scheduler

How to pass attributes from Cloud Scheduler to Pub/Sub?


I am trying to trigger a Cloud Function which runs on a schedule as such:

Cloud Scheduler -> Cloud Pub/Sub -> Cloud Functions

My Cloud Function uses attributes from the Pub/Sub message:

    let messageTitle = null;
        try {
            messageTitle = message.attributes.messageTitle;
        } catch (e) {
            console.error('no title in pub/sub message', e);
        }

How can I create a scheduler which has attributes?

I have tried entering this in the Scheduler 'Payload' field:

{
  "data": "string",
  "attributes": {
    messageTitle: "TEST 3 title",
    messageBody: "TEST 3 body"
  },
  "messageId": "string",
  "publishTime": "string"
}

But it does not create the attributes in Pub/Sub.


Solution

  • Today (july 2020), an important thing to know on Cloud Scheduler: Few parameters are accessible through the console.

    If you want to access to advance parameter, use the gcloud command or the rest API

    Now you can access to the retry policies and to the attributes on PubSub message.

    gcloud scheduler jobs create pubsub --message-body=my_body \
       --attributes=att1=val1,att2=val2 --topic=topicName \
       --schedule="0 0 * * *" --time-zone=UTC mySchedulerName
    

    Note, in the console, you can only define the payload, I mean the data field in the PubSub message, equal to the --message-body= in the command line