删除和 Zidekiah 作业
Delete a Sidekiq job
当我在购物车中添加产品时,在我的例子中,购物车也是 order/index,如果订单在创建后 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 分钟后自动删除。
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
您可以使用独特的作业,这是 Sidekiq Enterprise 的众多功能之一。
当我在购物车中添加产品时,在我的例子中,购物车也是 order/index,如果订单在创建后 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 分钟后自动删除。
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
您可以使用独特的作业,这是 Sidekiq Enterprise 的众多功能之一。