简体   繁体   English

在 Flask 中处理 webhook 响应超时的最佳方法?

[英]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 执行此操作的一种方法是设置两个函数:

  • 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.除了处理初始请求外,第一个 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.

相关问题 在 App Engine 中处理 session 亲和力的最佳方法 - Best way to handle session affinity in app engine 处理 EC2 实例强制终止的最佳方法 - Best way to handle EC2 instance forced termination 处理将 Uint8List 上传和显示到 FirebaseStorage 的最佳方法 - Best way to handle uploading and displaying Uint8List to FirebaseStorage 处理 TypeScript 中未初始化的 class 值的最佳方法 - Object 可能为“空” - Best way to handle uninitialised class values in TypeScript - Object is possibly 'null' 在 Spring Boot 中处理 @SqsListener 处理失败的最佳方法是什么? - What is the best way to handle @SqsListener processing failure in Spring Boot? 如何处理失败或严重延迟的 webhook? - How to handle a failed or significantly delayed webhook? 禁用 Twilio webhook 响应内容类型错误 - Disable Twilio webhook response content type errors DialogFlow Cx webhook 响应导致“ErrorCode”:“INVALID_ARGUMENT”、“ErrorMessage”:无法解析 webhook 响应: - DialogFlow Cx webhook response results in "ErrorCode": "INVALID_ARGUMENT", "ErrorMessage": Failed to parse webhook response: 有没有办法在不使用 Webhook 的情况下向 slack 发送 POST 请求? - Is there a way to send a POST request to slack without using Webhook? 如果您不想发送响应,在云功能中获取数据的最佳方式是什么? - what is the best way to get data inside a cloud-function in case you don't want to send a response?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM