A framework for declaring jobs and thereby performing queue operations in different ways on the backend. By using Active Job, it is possible to send emails and execute batch processing in the background.
First in, first out data structure. The reverse of the stack.
Starting with regular cleanups, invoicing, email delivery, and all other processing becomes a job. It is also possible to divide these jobs into smaller work units and execute them in parallel.
--You will be able to install job framework functions and other gems without worrying about the API differences of various job execution functions such as Delayed Job and Resque. --In the back-end queuing work, you don't have to worry about anything other than the operation method. --You don't have to rewrite the job when switching job management frameworks.
Resque --You can deploy code with memory leaks such as RMagick without any worries. --The ecosystem is complete. --Since it forks every time, it is suitable for long-time jobs. --Redis required for queue storage. --Maintenance is not catching up.
Delayed Job --DM needs to create a dedicated table. --If there is a code leaking memory, it needs to be restarted regularly.
Sidekiq --Resque compatible API. --Since it operates in parallel, it is convenient to use for applications where the ratio of I / O waiting is large, such as API calls to external sites. --Since it can be operated by one process, it uses less memory and is economical. --Weak against process bloat. --Handling of connection pools. --Handling other than parallel.
$ brew install redis
gem 'resque'
gem 'resque-scheduler'
Also execute this.
$ bundle install
config/initializers/resque.rb
Resque.redis = Redis.new(host: 'localhost', post: 6379)
Resque.after_fork = Proc.new { ActiveRecord::Base.establish_connection }
lib/tasks/resque.rake
require 'resque/tasks'
require 'resque/scheduler/tasks'
namespace :resque do
task setup: :environment do
ENV['TERM_CHILD'] ||= '1'
ENV['QUEUE'] ||= '*'
require 'resque'
require 'resque-scheduler'
end
end
config/initializers/active_job.rb
ActiveJob::Base.queue_adapter = :resque
--Send emails in the background using Active Job
$ rails g mailer user_mailer
app/mailers/user_mailer.rb
class UserMailer < ActionMailer::Base
default from: "[email protected]"
def registered(user)
mail(to: user.email, subject: 'Welcome to Rdaily!')
end
end
app/views/user_mailer/registered.text.erb
Dear. <%= @user.name %>
Welcome to Rdaily!
$ rails g job user_registered_mailer
app/jobs/user_registered_mailer_job.rb
class UserRegisteredMailerJob < ActiveJob::Base
queue_as :email
def perform(user)
UserMailer.registered(user).deliver_now
end
end
class Account::UsersController < ApplicationController
...
def create
@user = User.new(user_params)
if @user.save
UserRegisteredMailerJob.perform_later(@user)
flash.notice = "User is successfully created."
redirect_to account_path
...
end
gem 'mailcatcher'
Also execute this.
$ bundle install
config/environments/development.rb
Rails.application.configure do
...
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = { address: 'localhost', port: 1025 }
...
end
$ mailcatcher
#Start the Redis server with the following command
$ redis-server
#Start Resque worker with the following command
$ bundle exec rake resque:work
#Launch Resque Scheduler
$ rake environment resque:scheduler
#Start Rails server
$ rails server
Let's use Active Job introduced in Rails 4.2
[Rails, Linux] I tried to summarize "Active Job" and "Job"
Recommended Posts