Search code examples
ruby-on-railsemailresqueresque-scheduler

Setting different queue names in Resque mailer


In my rails4 application i need to send email to all users once in a week. Users belongs to different departments. I need to organize the mail sending based on the department wise queue names. But, i don't know how to give the queue name as parameter while sending the mail. All mails using the same queue name 'weekly_user_activity_queue'. Not setting to the department name queue as defined below. How can i achieve that. The code is shown below:

The gemfile:

  gem 'resque',:require => "resque/server" # for background processing
  gem 'resque_mailer' #for background mail sending

app/mailers/user_activit_mailer.rb

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

  @queue = :weekly_user_activity_queue

  def weekly_user_activities
    User.find_each do |user|
      @activities = user.get_user_activities
      email = @user.email

      @queue = user.get_department_name # Assigning department name as the queue name. Users with same department gets the email through the same queue.

      mail(to:email, subject: default_i18n_subject)
    end
  end
end

Please help. Thanks in advance.


Solution

  • Now, the below code will be able to set user defined queue names:

    WeeklyUserUpdates.enqueue(tenant,user.id)
    

    Defined the enqueue method in the workers/weekly_user_updates.rb

    def self.enqueue(user_id)
      Resque::Job.create(select_queue_name(user_id),self,user_id)
    end
    
    def self.select_queue_name(userid)   
      queue_name = User.find_by_id(userid).department_name
    end