2015-08-31 18 views
12

Tôi đang dùng Heroku với miền tùy chỉnh và tôi có tiện ích bổ sung Redis. Tôi cần trợ giúp để hiểu cách tạo nhân viên nền cho thông báo qua email. Người dùng có thể hộp thư đến với nhau và tôi muốn gửi thông báo qua email cho người dùng cho mỗi thư mới nhận được. Tôi có các thông báo làm việc trong phát triển, nhưng tôi không tốt với việc tạo ra các công việc nền được yêu cầu cho Heroku, nếu không thì máy chủ sẽ hết thời gian chờ.Hết giờ Heroku khi gửi email

Messages Bộ điều khiển: mô hình

def create 
    @recipient = User.find(params[:user]) 
    current_user.send_message(@recipient, params[:body], params[:subject]) 
    flash[:notice] = "Message has been sent!" 
    if request.xhr? 
     render :json => {:notice => flash[:notice]} 
    else 
     redirect_to :conversations 
    end 
    end 

User:

def mailboxer_email(object) 
    if self.no_email 
     email 
    else 
     nil 
    end 
end 

Mailboxer.rb:

Mailboxer.setup do |config| 

    #Configures if you applications uses or no the email sending for Notifications and Messages 
    config.uses_emails = false 

    #Configures the default from for the email sent for Messages and Notifications of Mailboxer 
    config.default_from = "[email protected]" 

    #Configures the methods needed by mailboxer 
    config.email_method = :mailboxer_email 
    config.name_method = :name 

    #Configures if you use or not a search engine and wich one are you using 
    #Supported enignes: [:solr,:sphinx] 
    config.search_enabled = false 
    config.search_engine = :sphinx 
end 

Trả lời

7

Sidekiq chắc chắn là cách để đi với Heroku. Tôi không nghĩ rằng hộp thư hỗ trợ cấu hình nền ra khỏi hộp. Rất may, nó vẫn thực sự dễ dàng với quá trình xếp hàng của sidekiq.

  1. Thêm gem 'sidekiq' vào gemfile và chạy bundle.
  2. Tạo tệp công nhân app/workers/message_worker.rb.
class MessageWorker 
    include Sidekiq::Worker 

    def perform(sender_id, recipient_id, body, subject) 
    sender = User.find(sender_id) 
    recipient = User.find(recipient_id) 
    sender.send_message(recipient, body, subject) 
    end 
end 
  1. Cập nhật điều khiển của bạn để xếp hàng Worker

Remove: current_user.send_message(@recipient, params[:body], params[:subject])

Add: MessageWorker.perform_async(current_user.id, @recipient.id, params[:body], params[:subject])

Lưu ý: Bạn nên không bao giờ vượt qua đối tượng ActiveRecord. Đó là lý do tại sao tôi thiết lập phương pháp này để chuyển Id người dùng và tìm chúng theo phương thức perform của công nhân, thay vì toàn bộ đối tượng.

  1. Cuối cùng, khởi động lại máy chủ của bạn và chạy bundle exec sidekiq. Bây giờ ứng dụng của bạn sẽ gửi nền email.

  2. Khi bạn triển khai, bạn sẽ cần một dyno riêng cho công nhân trông giống như sau: worker: bundle exec sidekiq. Bạn cũng sẽ cần add-on Redis của Heroku.

5

Âm thanh như một H21 Request Timeout:

Yêu cầu HTTP mất hơn 30 giây để hoàn tất.

Để tạo công nhân nền cho điều này trong RoR, bạn nên lấy Resque, thư viện xếp hàng nền được Redis hỗ trợ cho RoR. Here is a demo. Another demo. And another demo.

Để tìm hiểu thêm về cách sử dụng Resque trong Heroku, you can also read the herokue article up here. Or this tutorial (it's an old one though). Another great tutorial.

Ngoài ra còn có resque_mailer gem để tăng tốc độ cho bạn.

gem install resque_mailer #or add it to your Gemfile & use bundler 

Điều này khá đơn giản. Dưới đây là một đoạn trích từ a working demo by the author:

class Notifier < ActionMailer::Base 
    include Resque::Mailer 

    default :from => "[email protected]" 

    def test(data={}) 
    data.symbolize_keys! 

    Rails.logger.info "sending test mail" 
    Rails.logger.info "params: #{data.keys.join(',')}" 
    Rails.logger.info "" 

    @subject = data[:subject] || "Testing mail" 
    mail(:to => "[email protected]", 
     :subject => @subject) 
    end 
end 

làm Notifier.test.deliver sẽ cung cấp những đường bưu điện.

Bạn cũng có thể xem xét sử dụng dịch vụ chuyển phát thư như SES.

1

Sidekiq là một tùy chọn mà bạn có thể xem xét. Để làm cho nó hoạt động, bạn có thể thêm một cái gì đó như RedisToGo, sau đó cấu hình một initializer cho Redis. Sau đó, trên Heroku bạn có thể thêm một cái gì đó như worker: bundle exec sidekiq ... vào Procfile của bạn.

https://github.com/mperham/sidekiq/wiki/Getting-Started

Nó cũng có bảng điều khiển để theo dõi.

https://github.com/mperham/sidekiq/wiki/Monitoring