Webhook方法处理速度比控制器快

时间:2019-09-27 13:59:39

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

在我的申请中,Stripe付款。

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

问题在于,响应Web挂钩的方法比我的控制器中的操作要快。

如何放慢Webhook的方法,以便首先处理控制器中的动作?

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)比我的控制器快。

1 个答案:

答案 0 :(得分:0)

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

假设事件是您定义的模型:

  # 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