查看ActiveJob
以延迟邮件程序任务(使用delayed_jobs,delayed_jobs_active_record),我有2个问题:
deliver_later
方法我有一个Post
模型,如果已更新并删除,则需要向主持人发送电子邮件。
# Post Model
class Post < ActiveRecord::Base
# Table name: posts
#
# id :integer
# title :string
# body :text
# created_at :datetime
# updated_at :datetime
before_save :email_mod_post_updated, :email_mod_post_deleted
private
def email_mod_post_updated
ModeratorMailerJob.updated_post_email(self).deliver_later
end
def email_mod_post_deleted
ModeratorMailerJob.deleted_post_email(self).deliver_later
end
end
# ModeratorMailer
class ModeratorMailer < ApplicationMailer
default from: "Mod <mod@domain.com>"
def updated_post_email(post)
@post = post
mail(to: "mod@domain.com", subject: "Post Updated - #{@post.title}")
end
def deleted_post_email(post)
@post = post
mail(to: "mod@domain.com", subject: "Post Deleted - #{@post.title}")
end
end
当前ActiveJob
设置:
# app/jobs/updated_post_email_job.rb
class UpdatedPostEmailJob < ActiveJob::Base
queue_as :default
def perform(post)
ModeratorMailer.updated_post_email(post)
end
end
# app/jobs/deleted_post_email_job.rb
class DeletedPostEmailJob < ActiveJob::Base
queue_as :default
def perform(post)
ModeratorMailer.deleted_post_email(post)
end
end
问题1:
使用deliver_later
方法,这仍然会设置ActiveJob
,例如。 app/jobs/updated_post_email_job.rb
或者这不是必需的(我在后台运行delayed_job
)?
这与调用ModeratorMailerJob.perform_later(post)
问题2: 人们是否在其活动作业中添加多个执行方法,例如:
class EmailModeratorJob < ActiveJob::Base
queue_as :default
def perform(post)
ModeratorMailer.updated_post(post)
end
def perform(post)
ModeratorMailer.deleted_post(post)
end
end
或者最好是为特定任务分配工作吗?
答案 0 :(得分:0)
ActiveJob和邮寄者:
deliver_later
方法。它将通过配置的适配器在异步作业中发送您的邮件。 你不必为每封邮件定义一份工作,这是无用的。
回答你的问题:
perform
方法来完成这项工作。