Google Cloud PubSub disable retrying or set a minimum

Viewed 907

I am using Cloud Scheduler with PubSub and Cloud Run.

Some times my service is triggered more than one time, even with a successful response (HTTP 204 No Content) of my service running on Cloud Run.

My service tooks around 200 seconds to respond the POST made by PubSub.

My question is: How can I limit the number of tries of PubSub? Or I made some mistake like mutiple subscriptions (I have only one subscription, just checked on console)?

What is strange is when I trigger the Cloud Scheduler, the PubSub call my service several times (see the screenshot bellow)

I am deploying my PubSub and Cloud Run as following:

export PROJECT_ID=...
export PROJECT_NUMBER=$(gcloud projects describe --format 'value(projectNumber)' ${PROJECT_ID})

Setup Cloud Scheduler (need to be done only once per project)

gcloud pubsub topics create supervisor-cron --project ${PROJECT_ID}

Create a Pub/Sub subscription

gcloud pubsub subscriptions create supervisor-subscription \
    --topic supervisor-cron \
    --project ${PROJECT_ID}

Create a Cloud Scheduler at https://console.cloud.google.com/cloudscheduler

Enable Pub/Sub to create authentication tokens in your project

gcloud projects add-iam-policy-binding ${PROJECT_ID} \
    --member=serviceAccount:service-${PROJECT_NUMBER}@gcp-sa-pubsub.iam.gserviceaccount.com \
    --role=roles/iam.serviceAccountTokenCreator

Create or select a service account to represent the Pub/Sub subscription identity

gcloud iam service-accounts create cloud-run-pubsub-invoker \
     --display-name "Cloud Run Pub/Sub Invoker" \
     --project ${PROJECT_ID}

Deploy Cloud Run

gcloud builds submit --tag gcr.io/${PROJECT_ID}/supervisor --project ${PROJECT_ID}
gcloud run deploy supervisor \
    --set-env-vars APP_BASEURL=$(gcloud run services describe anotherservice --format 'value(status.url)' --platform managed --project ${PROJECT_ID}) \
    --set-env-vars APP_HEALTHCHECKS=https://hc-ping.com \
    --platform managed \
    --no-allow-unauthenticated \
    --timeout=900 \
    --image gcr.io/${PROJECT_ID}/supervisor \
    --project ${PROJECT_ID}

Create a Pub/Sub subscription with the service account

gcloud run services add-iam-policy-binding supervisor \
    --member=serviceAccount:cloud-run-pubsub-invoker@${PROJECT_ID}.iam.gserviceaccount.com \
    --role=roles/run.invoker \
    --platform managed \
    --project ${PROJECT_ID}
gcloud pubsub subscriptions create supervisor-subscription \
    --topic supervisor-cron \
    --push-endpoint=$(gcloud run services describe supervisor --format 'value(status.url)' --platform managed --project ${PROJECT_ID}) \
    --push-auth-service-account=cloud-run-pubsub-invoker@${PROJECT_ID}.iam.gserviceaccount.com
1 Answers

You need to extend the ACK deadline in your PubSub subscription.

Add this parameter with you create it. 600s (10 minutes) is the max value.

--ack-deadline=600

You also have other parameters to set a delay between each retry, the max number of retry and so on. Have a look here

Related