[英]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.我有一个运行在 Google Cloud Function 上的 Flask 应用程序,它在创建订单时从 Shopify 接收 Webhook。 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. Shopify 的文档指出订阅有 5 秒的超时期限和重试期限。 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?在验证 Webhook 之后和开始处理 Webhook 之前,有没有一种方法可以将 200 状态代码发送到 Shopify? Or is there a work-around to this?
或者有解决方法吗?
One way to do this entirely w/in Cloud Functions is to set up two functions:完全使用 Cloud Functions 执行此操作的一种方法是设置两个函数:
In addition to handling the initial request, the first function also invokes the second function via Cloud Pub/Sub.除了处理初始请求外,第一个 function 还通过 Cloud Pub/Sub 调用第二个 function。
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).有关完整示例,请参见https://dev.to/googlecloud/getting-around-api-timeouts-with-cloud-functions-and-cloud-pub-sub-47o3 (这使用了 Slack 的 webhook,但行为应该类似).
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.
因此,我们使用 celery 和 rabbitMq 将处理代码从内联执行转移到后台任务中执行。RabbitMq 用于队列管理。 You can use Redis for queue management also.
您也可以使用 Redis 进行队列管理。
Celery - https://docs.celeryproject.org/en/stable/getting-started/index.html Celery - https://docs.celeryproject.org/en/stable/getting-started/index.html
RabbitMq - https://www.rabbitmq.com/documentation.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/使用 Flask、Redis 和 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/如何使用 Celery 和 RabbitMQ 设置任务队列 - https://www.linode.com/docs/development/python/task-queue-celery-rabbitmq/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.