Question

I am referring Agile web developments with Rails 4th edition book. I have created two Email notification first for confirmation of order, second for shipped order. Now I want to add a delay of 5 minutes after sending a 'confirmation of order' mail to the user and then send the second 'shipped order' email. currently I have this two files, tell me what changes should I make to add required delay.

Thanks in advance.

orders_controller.rb

def create

    @order = Order.new(params[:order])
    @order.add_line_items_from_cart(current_cart)

    respond_to do |format|
      if @order.save
        Cart.destroy(session[:cart_id])
        session[:cart_id] = nil
        OrderNotifier.received(@order).deliver
        #Mail after 5 miutes to inform order is Shipped
        OrderNotifier.delay.shipped(@order)
        format.html { redirect_to store_url, notice: I18n.t('.thanks') }
        format.json { render json: @order, status: :created, location: @order }
      else
        @cart = current_cart
        format.html { render action: "new" }
        format.json { render json: @order.errors, status: :unprocessable_entity }
      end
    end
  end

order_notifier.rb

class OrderNotifier < ActionMailer::Base

  default from: 'sam ruby <depot@example.com>'


  def received(order)
    @order = order
    mail to: order.email, subject: 'Pragmatic Store Order Confirmation'
  end


  def shipped(order)
    @order = order
    mail to: order.email, subject: 'Pragmatic Store Order Shipped'
  end
  handle_asynchronously :shipped, :run_at => Proc.new { 5.minutes.from_now }
end

I did the above changes to my code,

It raise error with rake jobs:work

[Worker(pid:8300)] Starting job worker [Worker(pid:8300)] Job OrderNotifier#shipped_without_delay (id=31) RUNNING [Worker(pid:8300)] Job OrderNotifier#shipped_without_delay (id=31) FAILED (0 prior attempts) with NoMethodError: undefined method []' for nil:NilClass [Worker(id:8300)] 1 jobs processed at 1.5796 j/s, 1 failed [Worker(pid:8300)] Job OrderNotifier#shipped_without_delay (id=31) RUNNING [Worker(pid:8300)] Job OrderNotifier#shipped_without_delay (id=31) FAILED (1 prior attempts) with NoMethodError: undefined method []' for nil:NilClass [Worker(pid:8300)] 1 jobs processed at 6.3007 j/s, 1 failed

Was it helpful?

Solution

You can't handle that in your controller, otherwise the ruby process will be blocked for 5 minutes :-). You should use something like the delayed_job gem, available on github: https://github.com/collectiveidea/delayed_job - this gem is awesome and perfect for such situations. Simple check out the readme on the github page.

OTHER TIPS

Mattherick is correct - this must be handled outside of your controller. But I would highly recommend Sidekiq over DelayedJob. It can handle a much higher volume of jobs, and I've found it to be more stable.

https://github.com/mperham/sidekiq

And for sending specifically, see https://github.com/mperham/sidekiq/wiki/Delayed-Extensions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top