【问题标题】:Webhook method is processed faster than controllerWebhook 方法处理速度比控制器快
【发布时间】:2020-01-27 20:53:00
【问题描述】:

在我的应用程序中有 Stripe 付款。

当我点击“支付”按钮时,必须通过控制器进行处理。 Webhook 也在同时运行。

问题是响应 web 钩子的方法比我控制器中的动作快。

如何减慢 webhook 的方法,以便首先处理控制器中的操作?

网络钩子:

ev = Stripe::Webhook.construct_event(...)
case ev.type
when "invoice.created"
  change_invoice(ev)
when "invoice.payment_succeeded"
  invoice_paid(ev)

change_invoice(ev) 比我的控制器快。

【问题讨论】:

  • 我遇到了类似的问题,在我的情况下,我将网络挂钩中收到的数据传递给后台作业。如果“太快”,该作业可能会在几秒钟后将另一个具有相同数据的作业重新排入队列。

标签: ruby-on-rails ruby stripe-payments webhooks


【解决方案1】:

我能想到的最简单的解决方案是使用数据库锁。在这里阅读:https://www.peterdebelak.com/blog/pessimistic-locking-in-rails-by-example/

假设 Event 是你定义的模型:

  # Controller action
  def create
   ev = Event.create(some_params)
   ev.lock!
   # Do the stripe processing here
   # When you are done with your controller work:
   ev.save # will release the lock
  end

  # Then in your webhook processor
  def process
    ev = Event.find(some_id) # This will wait until the lock is released in the controller thread. Just be aware that if the controller doesn't release the lock within the timeout limit, this will raise an exception
    # now you can process the event because the controller is done
  end

【讨论】:

    猜你喜欢
    • 2019-05-31
    • 1970-01-01
    • 2021-09-23
    • 1970-01-01
    • 2012-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-15
    相关资源
    最近更新 更多