ROR Application Development Tips

I'm Ruby on Rails developer. I love to share my experience, technical knowledge. I work at Crypex Technologies which is developing applications in ROR for more than a decade.

Bulk Mailing Using Sidekiq

Introduction:

 

Sometimes the rails application requires sending the bulk mails using action mailers on the single attempt. The user can be able to send them seamlessly. But, this task is not possible in synchronous way. This will affect whole application performance and will take a lot of time to load the respective pages.

To overcome this problem rails community have enormous kinds of solutions. Among them  sidekiq is the best choice to overcome this kind of problems.

The sidekiq is a full-featured background processing framework for Ruby. It aims to be simple to integrate with any modern Rails application and much higher performance than other existing solutions.

The Redisis an open source (BSD licensed), in-memory data structure store, used as a database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperlog logs and geospatial indexes with radius queries.

The Action Mailer is a service provided by rails to send the mails on respective emails ids. To send an email we must have to invoke deliver method of actionmailer.

To send mails asynchronously we have to use the sidekiq.

Integration Setup:

Add sidekiq into your rails application by including it into your Gemfile and run the bundle command.

#Gemfile

gem ‘sidekiq’

To run sidekiq, run the following command:

$ bundle exec sidekiq -d -L log/sidekiq.log -C config/sidekiq.yml(default development mode)

$ bundle exec sidekiq -d -L log/sidekiq.log -C config/sidekiq.yml -e production (production mode)

Sending Delayed Emails:

                Sidekiq provides asynchronous emails sending provision with Action Mailer. That will be performed by adding below three methods to the ActionMailer module.

.dealy

The .delay method will call the action mailer method and add this mail into to DelayedMailer queue for processing.

                                For ex.,   NewsMailer.delay.newsmailer(@email)

.delay_for(interval)

Here, user can be proviode inerval time before sending emails asynchronously.

                                For ex.,   NewsMailer.delay_for(1.day).newsmailer(@email)

.delay_until(timestamp)

Here, user can set the timestamp for sidekiq. Sidekiq will wait until the provided time to send the email.

For ex.,   NewsMailer.delay_until(3.day.from_now).newsmailer(@email)

Sending Bulk delayed Emails:

The below example illustrated the way to send bulk emails.

ApplicationMailer.delay.new_article_mail(@article)

Here., ApplicationMailer is a class have new_article_mail(article) method. We used .delay method to                           set the delay for sending mail asynchronously.

# Send an email to all the users if new project has been posted in website.

		def new_article_mail(article)
			@article = article
			all_users("New article added in GXP")
		end
	
		private
		def all_users(subject)
		     User.all.map{ |user|
		       @user = user
		       send_mail(user.email, subject).deliver
		     }
		end

		def send_mail(email, subject)
		    mail to: email, subject: subject
		end

Here, new_article_mail method has one more method all_users in which we are calling send_mail method for sending an email to the individual user.

Source: Bulk Mailing Using Sidekiq