Skip to main content

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

Comments

Popular posts from this blog

GraphQL With Ruby

Now a day’s most of the web or mobile applications fetch data from server which is stored in a database. REST API provides an interface to stored data that require by the applications. GraphQL is a query language for REST API's not for server databases. It is database agnostic and effectively can be used in any context where an API is used. GraphQL provide platform for declarative data fetching where client need to specify what data needs from API in response. Instead of multiple endpoints that return fixed data structures, a GraphQL server only exposes a single endpoint and responds with precisely the data a client asked for. GraphQL minimizes the amount of data that needs to be transferred over the network and improves applications operating under these conditions. Introduction to GraphQL API on Ruby on Rails Start with adding gem in Gemfile gem ‘graphql’ Run command bundle install Run command rails generate graphql:install Above command will add gr

Best In Place Gem In Ruby On Rails Tutorial

The best_in_place gem is the easiest solution for in place editing in Ruby on Rails. This gem provides functionality of “in place editing” in ruby on rails without writing any extra ajax code. It supports text inputs, textarea, select dropdown, checkboxes, jQuery UI Datepickers, etc. Also Displays server-side validation Installation Steps of “best_in_place” Gem : Installing best_in_place is very easy and straight-forward. Just begin including the gem in your Gemfile: gem ‘best_in_place’ After that, specify the use of the jquery and best in place javascripts in your application.js, and optionally specify jquery-ui if you want to use jQuery UI datepickers: //= require jquery //= require best_in_place //= require jquery-ui //= require best_in_place.jquery-ui Then, just add a binding to prepare all best in place fields when the document is ready: $(document).ready(function() { /* Activating Best In Place */ jQuery(".best_in_place").best_in_place(); });

Data scraping in Ruby on Rails using Nokogiri and Mechanize Gem

What is Data scraping? Website/Data  Scraping  is a technique to operating large amounts of  data  from websites whereby the  data  is extracted and displayed in own sites or it can be stored to a File/Database. Data scraping is basically used where the websites does not provides API. Some Applications do not provide API to collect records. For the same , Data Scraping technique is used. The data can be scraped using Nokogiri Gem. The steps are required: Add the gem “gem ‘nokogiri’, ‘~> 1.8’, ‘>= 1.8.1'” . Then run the bundle install Add the “require ‘nokogiri'” , “require ‘open-uri'” line where you will write the code for the scraping. The controller of the page will look like below: The view of the code of view page will look like : The result in our application will look like: Mechanize Gem in rails The Mechanize library is used for automating interaction with websites. Mechanize automatically stores and sends cookies, follo