# Terminal
bundle exec sidekiq -C config/sidekiq.yml
# app/events/application_publisher.rb
class ApplicationPublisher
include Wisper::Publisher
def self.call(*args)
new(*args).call
end
end
# app/events/publishers/user_created.rb
class Publishers::UserCreated < ApplicationPublisher
attr_accessor :user_id
def initialize(user_id)
@user_id = user_id
subscribe(Subscribers::UserCreated, async: true)
end
def call
user = User.find_by(id: user_id)
if user.email?
publish(:welcome_user_email, user.id)
# broadcast(:welcome_user_email, user.id)
end
end
end
# app/events/subscribers/user_created.rb
class Subscribers::UserCreated
def self.sidekiq_options
{ queue: 'first_service_app' }
end
# If you're not running the event in a background task
# then the method would be an instance method.
# def welcome_user_email(user_id)
# end
def self.welcome_user_email(user_id)
# send email
end
end
# users_controller.rb
def create
@user = User.new(user_params)
respond_to do |format|
if @user.save
Publishers::UserCreated.call(@user.id)
format.html { redirect_to @user, notice: 'User was successfully created.' }
format.json { render :show, status: :created, location: @user }
else
format.html { render :new }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
# Gemfile
gem 'wisper'
gem 'sidekiq'
gem 'wisper-sidekiq'
# config/routes.rb
require 'sidekiq/web'
Rails.application.routes.draw do
resources :users
root to: 'users#index'
mount Sidekiq::Web => '/sidekiq'
end
# config/application.rb
config.active_job.queue_adapter = :sidekiq
# config/sidekiq.yml
:queues:
- first_service_app