【发布时间】:2022-08-14 23:04:20
【问题描述】:
我正在尝试使用 Heya gem 和 GoodJob 在 Rails 应用程序中发送电子邮件活动。 Heya 自述文件和Heya example app 中的示例使用 Sidekiq 作为 Active Job 后端。
我对如何使用GoodJob 实际发送Heya 活动感到困惑。
Heya 的文档显示了启动 Sidekick 的示例:bundle exec sidekiq -q default -q heya
我假设 gem 中某处有一个名为“Heya”的作业队列,但我在源代码中找不到它。我需要创建一个吗?
我需要创建一个运行 Heya 调度程序的作业吗?虽然示例应用程序使用 Sidekiq,但我也没有看到任何 custom jobs in that app。
我为 GoodJob 设置了以下设置,它似乎与 good_job start 运行良好,它应该运行所有作业和队列,但我也尝试过 good_job start --queues=heya,default。
以下是相关代码:
Profile.dev
web: bin/rails server -p 3000
css: bin/rails tailwindcss:watch
worker: bundle exec good_job start
配置/初始化程序/heya.rb
Heya.configure do |config|
config.user_type = \"User\"
config.campaigns.priority = [
\"WelcomeCampaign\",
]
end
应用程序/工作/application_job.rb
class ApplicationJob < ActiveJob::Base
# Automatically retry jobs that encountered a deadlock
# retry_on ActiveRecord::Deadlocked
# Most jobs are safe to ignore if the underlying records are no longer available
# discard_on ActiveJob::DeserializationError
end
app/campaigns/application_campaign.rb
class ApplicationCampaign < Heya::Campaigns::Base
segment :email_subscriber?
default from: \"#{I18n.t(\'settings.site_name\')} <#{I18n.t(\'settings.newsletter_email\')}>\"
end
应用程序/campaigns/welcome_campaign.rb
class WelcomeCampaign < ApplicationCampaign
default wait: 5.minutes,
layout: \"newsletter\"
step :intro, wait: 0.minutes,
subject: \"Welcome to #{I18n.t(\'settings.site_name\')}\"
end
我还有一个类似于the Heya example app 的活动布局和视图,我正在使用Mailcatcher 来查看是否正在发送任何电子邮件。
与 Heya 和 GoodJob 一起发送这些电子邮件时,我缺少什么?
请注意,我正在订阅这样的注册用户:
class User < ApplicationRecord
after_create_commit :add_user_to_newsletters
private
def add_user_to_newsletters
WelcomeCampaign.add(self)
EvergreenCampaign.add(self)
self.update(email_subscriber: true)
end
end
在campaigns/application_campaign.rb 中的默认段是segment :email_subscriber?
如果我在控制台中运行User.last.email_subscriber? 来检查它,它会返回true。
我觉得我错过了关于 Heya 如何连接到 Active Job 的一些东西,这在 Heya 文档中并不明显。
另外,不确定这是否相关,但我将其添加到 config/puma.rb
# https://github.com/bensheldon/good_job#execute-jobs-async--in-process
before_fork do
GoodJob.shutdown
end
on_worker_boot do
GoodJob.restart
end
on_worker_shutdown do
GoodJob.shutdown
end
MAIN_PID = Process.pid
at_exit do
GoodJob.shutdown if Process.pid == MAIN_PID
end
preload_app!
标签: ruby-on-rails ruby actionmailer rails-activejob