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?
2
Answers
I used to face the same issue as yours. So, we moved the processing code from being executed inline to be executed in a background task by using celery and rabbitMq. RabbitMq was used for queue management. You can use Redis for queue management also.
Celery – https://docs.celeryproject.org/en/stable/getting-started/index.html
RabbitMq – https://www.rabbitmq.com/documentation.html
Asynchronous Tasks Using Flask, Redis, and Celery – https://stackabuse.com/asynchronous-tasks-using-flask-redis-and-celery/
How to Set Up a Task Queue with Celery and RabbitMQ – https://www.linode.com/docs/development/python/task-queue-celery-rabbitmq/
One way to do this entirely w/in Cloud Functions is to set up two functions:
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).