Ad

Best Way To Handle Webhook Response Timeouts In Flask?

I have a Flask application running on a Google Cloud Function that receives a Webhook from Shopify when an order is created. The problem is I'm timing out very often, here's what I mean by that:

@app.route('/', methods=['POST'])
def connectToSheets(request):
    print('Webhook received...')

    # Verify request is coming from Shopify
    data = request.data
    hmac_header = request.headers.get('X-Shopify-Hmac-SHA256')
    verify_webhook(data, hmac_header)
    print('Request validated...')

    # Do some stuff...

Shopify's docs states that there is a 5 sec timeout period and a retry period for subscriptions. After I validate the request, there is quite a lot of code so I'm timing out almost every time.

Is there a way I can send a 200 status code to Shopify after I validate the Webhook and before I start processing the Webhook? Or is there a work-around to this?

Ad

Answer

One way to do this entirely w/in Cloud Functions is to set up two functions:

  • one that handles the initial request
  • a second one that does the processing and then follows up with the response

In addition to handling the initial request, the first function also invokes the second function via Cloud Pub/Sub.

See https://dev.to/googlecloud/getting-around-api-timeouts-with-cloud-functions-and-cloud-pub-sub-47o3 for a complete example (this uses Slack's webhook, but the behavior should be similar).

Ad
source: stackoverflow.com
Ad