Sidekiq Rails 4.2使用现役或工人?有什么不同 [英] Sidekiq Rails 4.2 Use Active Job or Worker? What's the difference
问题描述
这是我的第一个异步处理作业,我正在为我的应用程序实现Sidekiq进行后台处理.我将其用于提醒电子邮件和应用内通知.对于我应该使用活动作业创建发送电子邮件的作业还是使用Sidekiq Worker发送电子邮件的作业,我感到困惑.他们似乎做同样的事情,而Rails 4.2 Active Job似乎很新……是否取代了对Sidekiq Worker的需要?
This is my first processing jobs asynchronously I am implementing Sidekiq for background processing in my app. I will use it for reminder emails and in-app notifications. I am confused as to whether I should use Active Job to create a job that sends an email or a Sidekiq Worker to send an email. They seem to do the same thing and Rails 4.2 Active Job seems very new…is it replacing the need for a Sidekiq Worker?
以下是使用活动作业"作业和"Sidekiq工作程序"发送邮件程序代码的方法.我正在使用每当每当宝石来安排.
Below is the same sending a mailer code using an Active Job job and a Sidekiq Worker. I am using Whenever gem for scheduling.
my_mailers.rb
my_mailers.rb
class MyMailers < ActionMailer::Base
def some_mailer(r.user_id)
@user = User.find(r.user_id)
mailer_name = "ROUNDUP"
@email = @user.email
@subject ="subject text"
mail(to: @email,
subject: @subject,
template_path: '/notifer_mailers',
template_name: 'hourly_roundup.html',
)
end
end
使用Sidekiq工作者"
some_worker.rb
Using a Sidekiq "Worker"
some_worker.rb
class SomeWorker
include Sidekiq::Worker
def perform()
@user = User.all
@reminders = @user.reminders.select(:user_id).uniq.newmade
@reminders.each do |r|
MyMailers.some_mailer(r.user_id).deliver_later
end
end
end
使用活动作业工作"
some_job.rb
Using an Active Job "Job"
some_job.rb
class SomeJob < ActiveJob::Base
queue_as :mailer
def perform()
@user = User.all
@reminders = @user.reminders.select(:user_id).uniq.newmade
@reminders.each do |r|
MyMailers.some_mailer(r.user_id).deliver_later
end
end
end
无论何时"计划程序中的两个示例 schedule.rb
Both Examples in my Whenever scheduler schedule.rb
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
set :path, Rails.root
set :output, Rails.root.join('log', 'cron.log')
#using a worker
every 1.day, :at => '4:30 am' do
runner SomeWorker.perform_async
end
#using a job
every 1.day, :at => '4:30 am' do
runner SomeJob.perform_async
end
推荐答案
简短的回答是,它们是同一回事. ActiveJob称其为Job,而Sidekiq称其为Worker.我决定保留不同的术语,以便人们可以区分两者.
Short answer is they are the same thing. ActiveJob calls it a Job whereas Sidekiq calls it a Worker. I've decided to keep the terminology different so that people can distinguish the two.
您可以使用任何一个.请注意,ActiveJob不提供对Sidekiq选项全部设置的访问权限,因此,如果您要为工作自定义选项,则可能需要将其设置为Worker.
You can use either one. Note that ActiveJob does not provide access to the full set of Sidekiq options so if you want to customize options for your job you might need to make it a Worker.
这篇关于Sidekiq Rails 4.2使用现役或工人?有什么不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!