如何通过 GoodJob 而不是 Sidekiq 使用 Heya 电子邮件营销活动

发布于 2025-01-10 00:24:29 字数 3317 浏览 0 评论 0原文

我正在尝试使用 Heya gem 和 好工作。 Heya 自述文件中的示例以及 Heya 示例应用 使用 Sidekiq 作为 Active作业后台。

我对如何使用 GoodJob 实际发送 Heya 活动感到困惑。

Heya 的文档显示了启动 Sidekick 的示例: bundle exec sidekiq -q default -q heya

我假设 gem 中的某处有一个名为“Heya”的作业队列,但我找不到这个在源代码中。我需要创建一个吗?

我需要创建一个运行Heya调度程序的作业吗?虽然示例应用程序使用 Sidekiq,但我也没有看到任何 该应用程序中的自定义作业

我对 GoodJob 有以下设置,它似乎与 good_job start 运行良好,它应该运行所有作业和队列,但我也尝试过 good_job start --queues=heya ,默认

以下是相关代码:

Profile.dev

web: bin/rails server -p 3000
css: bin/rails tailwindcss:watch
worker: bundle exec good_job start

config/initializers/heya.rb

Heya.configure do |config|
  config.user_type = "User"

  config.campaigns.priority = [
    "WelcomeCampaign",
  ]
end

app/jobs/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

app/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

我也有类似于 < 的活动布局和视图一个href="https://github.com/honeybadger-io/heya-app/tree/master/app/views/heya/campaign_mailer/demo_campaign" rel="nofollow noreferrer">Heya 示例应用,以及我正在使用 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!

I'm trying to send email campaigns in a rails app with the Heya gem and GoodJob. The example in the Heya readme as well as the Heya example app uses Sidekiq as the Active Job backend.

I'm confused about how to actually send the Heya campaigns with GoodJob.

The docs for Heya show this example of starting Sidekick: bundle exec sidekiq -q default -q heya

I assume that there is a Job queue somewhere in the gem called "Heya", but I can't find this in the source code. Do I need to create one?

Do I need to create a job that runs the Heya scheduler? While the example app uses Sidekiq, I also don't see any custom jobs in that app.

I have the following setup for GoodJob and it appears to be running fine with good_job start which should run all of the jobs and queues, but I've also tried good_job start --queues=heya,default.

Here is the relevant code:

Profile.dev

web: bin/rails server -p 3000
css: bin/rails tailwindcss:watch
worker: bundle exec good_job start

config/initializers/heya.rb

Heya.configure do |config|
  config.user_type = "User"

  config.campaigns.priority = [
    "WelcomeCampaign",
  ]
end

app/jobs/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

app/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

I also have a layout and views for the campaign similar to the Heya example app, and I'm using Mailcatcher to see if any email is being sent.

What am I missing to send these emails with Heya and GoodJob?

Note that I'm subscribing the users on signups like this:

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

And the default segment in campaigns/application_campaign.rb is segment :email_subscriber?

If I run User.last.email_subscriber? in the console to check this it returns true.

I feel like I'm missing something about how Heya connects to Active Job that is not obvious in the Heya docs.

Also, not sure if this is related, but I added this to 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

早乙女 2025-01-17 00:24:29

您是否定期运行 heya 调度程序? $rails heya:scheduler

看起来像 您可以创建自己的背景job 使用 GoodJob Cron 运行,通过执行 Heya::Campaigns::Scheduler.new.run 来运行调度程序并将电子邮件排入队列。

阅读自述文件的“运行调度程序”部分解释了发生的情况:

要开始对电子邮件进行排队,请定期运行调度程序任务:

rails heya:scheduler

Heya 使用 ActiveJob 在后台发送电子邮件。确保您的
ActiveJob 后端配置为处理 heya 队列。默认情况下,GoodJob 从所有队列 "*" 运行。

您可以使用 queue 选项更改 Heya 的默认队列:

# app/campaigns/application_campaign.rb
class ApplicationCampaign < Heya::Campaigns::Base
  default queue: "custom"
end

Are you running the heya scheduler periodically? $ rails heya:scheduler

It looks like you could create your own background job to be run using GoodJob Cron, by executing Heya::Campaigns::Scheduler.new.run to run the scheduler and enqueue the emails.

Reading the "Running the Scheduler" part of the README explains what's happening:

To start queuing emails, run the scheduler task periodically:

rails heya:scheduler

Heya uses ActiveJob to send emails in the background. Make sure your
ActiveJob backend is configured to process the heya queue. By default, GoodJob runs from all queues "*".

You can change Heya's default queue using the queue option:

# app/campaigns/application_campaign.rb
class ApplicationCampaign < Heya::Campaigns::Base
  default queue: "custom"
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文