Send mail using worker in your Rails project

Send mail using worker in your Rails project

Intro about What is worker?

A worker is typically a process that runs outside of your application request cycle. Ruby libraries like resque and delayed_job are typically used to manage worker queues.

 

On the other hand, if we’re talking about web servers the word “worker” is sometimes used interchangeably with threads or processes (ie: nginx or apache “workers”).

Let’s come…. write the worker

Follow these steps one by one

1. add gem ‘sidekiq‘ to gemfile

 

2. Now do bundle install from your terminal

 

3. Add workers directory into your project : app/workers

 

4 Add a class in app/workers/myworkerclass.rb

 

5. include Sidekiq::Worker in this class

 

6. Now define a perform() method in this worker class like

 

 

class Myworkerclass

 

include Sidekiq::Worker

def perform(receiver_emails,offer)

UserMailer.send_offer_to_users(receiver_emails,offer).deliver_now

end

end

 

7. include Sidekiq::Worker to controller from where you want to call the worker

 

8. call the worker from controller like :

 

Myworkerclass.perform_async(reciever_emails,offer)

 

9. if redis server is installed then run this command on your terminal

 

$ bundle exec sidekiq

 

otherwise install redis server

go through Click here for Redis installation