4

示例场景:

付款处理和电子产品交付交易。

要求

  1. 每天大约有几千笔支付交易需要执行。每个大约需要 1 秒。(所以整个过程大约需要一个小时)
  2. 事务必须在单个线程中线性处理(下一个事务必须在最后一个事务完成之前才开始,强 FIFO 顺序是必要的)
  3. 每个支付交易都包装在一个数据库交易中,任何回滚交易的东西都会被中止并放入另一个队列以进行手动错误处理。之后,它应该继续处理其余的交易。

重要性顺序

  1. 单次执行(如果失败,放入错误队列进行人工处理)
  2. 单线程
  3. 先进先出

Sidekiq 是否适合此类关键任务流程?sidekiq 能否满足所有这些要求?或者你会推荐其他替代品吗?您能否指出一些有关 Rails 支付处理的最佳实践?

Note: The question is not regarding whether to use stripe or ActiveMerchant for payment handling. It is more about the safest way to programmatically execute those processes in the background.

4

2 回答 2

4

Yes, Sidekiq can fulfill all of these requirements.

To process your transactions one at a time in serial, you can launch a single Sidekiq process with a concurrency of 1 that only works on that queue. The process will work jobs off the queue one at a time, in order.

For a failed task to go into a failure queue, you'll need to use the Sidekiq Failures gem and ensure retries are turned off for that task.

To guarantee that each task is executed at least once, you can purchase Sidekiq Pro and use Reliable Fetch. If Sidekiq crashes, it will execute the task when it starts back up. This assumes you will set up monitoring to ensure the Sidekiq process stays running. You might also want to make your task idempotent, so it doesn't write the same transaction twice. (In theory, the process could crash after your database transaction commits, but before Sidekiq reports to Redis that the task completed.)

If using Ruby is a constraint, Sidekiq is probably your best bet. I've used several different queuing systems in Ruby and none of them have the reliability guarantee that Sidekiq does.

于 2013-11-22T07:03:19.167 回答
0

直到最后一个事务完成后才能开始下一个事务

在这种情况下,我认为后台处理是合适的,只要您在完成上一个作业时创建下一个作业。

class Worker
  include Sidekiq::Worker

  def perform(*params)
    # do work, raising exception if necessary

    NextWorker.perform_async(params, here)
  end
end
于 2013-11-21T04:37:18.193 回答