删除Sidekiq作业

时间:2019-01-18 15:06:10

标签: ruby-on-rails ruby sidekiq

当我在购物车中添加产品时(以我的情况来说,购物车也是订单/索引),如果订单在创建后10分钟内仍未付款,它将启动一项工作以删除订单并恢复其状态产品从不可用到可用。

此刻,每次我基本上将新产品添加到购物车中时,它都会创建工作

class OrdersController < ApplicationController
  before_action :set_product, only: [:create, :update]
  before_action :set_order, only: [:payorder, :index, :destroy, :update, :product_ordinable, :show]
  before_action :product_ordinable, only: [:destroy]

  def payorder
    customer = Stripe::Customer.create(
      source: params[:stripeToken],
      email:  params[:stripeEmail]
      )

    charge = Stripe::Charge.create(
      customer:     customer.id,
      amount:       @order.amount_cents,
      description:  "Payment for your products for order #{@order.id}",
      currency:     @order.amount.currency
      )

    @order.update(payment: charge.to_json, paid: true)
    respond_to do |format|
      format.html { redirect_to orders_path, notice: 'Payment Completed!' }
    end

  rescue Stripe::CardError => e
    flash[:alert] = e.message
    render :index
  end

  def index
  end

  def show
  end

  def new
  end

  def edit
  end

  delegate *%w(
  unpaid_orders?
  orders
  ), to: :current_user

  def create
    if @product.ordinable?
      order = unpaid_orders? ? orders.last : orders.create!
      @product.update(ordinable: false)
      if order.products << @product
        order.update(amount: order.products.sum(&:price))
        @notice = 'Product added to the Cart!'
        OrderPaidCheckJob.set(wait: 10.minutes).perform_later(order.id)
        # unless unpaid_orders
      else
        @notice = 'There was a problem while adding the product to the cart!'
      end
    else
      @notice = 'There was a problem while adding the product to the cart!'
    end
    redirect_to products_path, notice: @notice
  end

  def update
    @order.products.delete(Product.find(@product.id))
    @product.update(ordinable: true)
    if @order.products.empty?
      @order.destroy
      @notice = 'Order deleted!'
    else
      @order.update(amount: @order.products.sum(&:price))
      @notice = 'Order Updated!'
    end
    redirect_to orders_path, notice: @notice
  end

  def destroy
    @order.destroy
    redirect_to orders_url, notice: 'The order was successfully destroyed.'
  end

  private

  def set_product
    @product = Product.find(params[:id])
  end

  def set_order
    @order = current_user.orders.last
  end

  def product_ordinable
    @order.products.each do |x|
      x.ordinable = true
      x.save
    end
  end

  def previous_orders
  end
end
class OrderPaidCheckJob < ApplicationJob
  queue_as :default

  def perform(order_id)
    order = Order.find(order_id)
    if order.paid == false
      order.products.each do |x|
        x.ordinable = true
        x.save
      end
      order.destroy
    end
  end
end

我想删除该作业,或者当我在购物车中添加另一种产品时重新开始,以便在购物10分钟后看不到自动删除的订单。

2 个答案:

答案 0 :(得分:0)

class OrderPaidCheckJob < ApplicationJob
  queue_as :default

  def perform(order_id)
    order = Order.find(order_id)
    return if order.nil? || order.paid
    if order.updated_at > 10.minutes.ago # order recently changed
      OrderPaidCheckJob.set(wait: 10.minutes).perform_later(order.id)
      return # end this job. check again 10min later
    end
    order.products.each do |x|
      x.ordinable = true
      x.save
    end
    order.destroy
  end
end

答案 1 :(得分:0)

您可以使用一个独特的作业,这是Sidekiq Enterprise拥有的众多功能之一。

https://github.com/mperham/sidekiq/wiki/Ent-Unique-Jobs