Refactor:Send Welcome Broadcast Notifications From Sidekiq (#9923)
* Refactor:Send Welcome Broadcast Notifications From Sidekiq * remove old spec and add to new one
This commit is contained in:
parent
e8b14dec83
commit
060ed32c48
5 changed files with 62 additions and 67 deletions
21
app/workers/broadcasts/send_welcome_notifications_worker.rb
Normal file
21
app/workers/broadcasts/send_welcome_notifications_worker.rb
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
module Broadcasts
|
||||
class SendWelcomeNotificationsWorker
|
||||
include Sidekiq::Worker
|
||||
|
||||
sidekiq_options queue: :medium_priority, retry: 15
|
||||
|
||||
def perform
|
||||
# In order to prevent new users from receiving multiple welcome notifications in a day,
|
||||
# a feature_live_date is required. The script will only be effective after feature_live_date
|
||||
# and will ultimately be superseded by 7.days.ago when it's larger than feature_live_date.
|
||||
return unless SiteConfig.welcome_notifications_live_at
|
||||
|
||||
notifications_live_at = SiteConfig.welcome_notifications_live_at
|
||||
week_ago = 7.days.ago
|
||||
latest_date = notifications_live_at > week_ago ? notifications_live_at : week_ago
|
||||
User.select(:id).where("created_at > ?", latest_date).find_each do |user|
|
||||
Broadcasts::WelcomeNotification::Generator.call(user.id)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -68,3 +68,6 @@ resave_supported_tags:
|
|||
expire_old_listings:
|
||||
cron: "30 0 * * *" # daily at 12:30 am UTC
|
||||
class: "Listings::ExpireOldListingsWorker"
|
||||
send_welcome_notifications:
|
||||
cron: "0 16 * * *" # daily at 4 pm UTC
|
||||
class: "Broadcasts::SendWelcomeNotificationsWorker"
|
||||
|
|
|
|||
|
|
@ -1,16 +0,0 @@
|
|||
namespace :broadcasts do
|
||||
desc "Send Welcome Notifications once a day"
|
||||
task send_welcome_notification_flow: :environment do
|
||||
# In order to prevent new users from receiving multiple welcome notifications in a day,
|
||||
# a feature_live_date is required. The script will only be effective after feature_live_date
|
||||
# and will ultimately be superseded by 7.days.ago when it's larger than feature_live_date.
|
||||
next unless SiteConfig.welcome_notifications_live_at
|
||||
|
||||
notifications_live_at = SiteConfig.welcome_notifications_live_at
|
||||
week_ago = 7.days.ago
|
||||
latest_date = notifications_live_at > week_ago ? notifications_live_at : week_ago
|
||||
User.select(:id).where("created_at > ?", latest_date).find_each do |user|
|
||||
Broadcasts::WelcomeNotification::Generator.call(user.id)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -1,51 +0,0 @@
|
|||
require "rails_helper"
|
||||
|
||||
RSpec.describe "Broadcasts tasks", type: :task do
|
||||
let(:service) { Broadcasts::WelcomeNotification::Generator }
|
||||
let(:one_week_from_today) { 1.week.since }
|
||||
|
||||
let(:default_config_date) { SiteConfig.welcome_notifications_live_at }
|
||||
|
||||
before do
|
||||
# Set date to a week from today
|
||||
SiteConfig.welcome_notifications_live_at = one_week_from_today
|
||||
allow(service).to receive(:call)
|
||||
Rake::Task.clear
|
||||
PracticalDeveloper::Application.load_tasks
|
||||
end
|
||||
|
||||
after do
|
||||
SiteConfig.welcome_notifications_live_at = default_config_date
|
||||
end
|
||||
|
||||
describe "#broadcast_welcome_notification_flow" do
|
||||
it "does nothing if SiteConfig.welcome_notifications_live_at is nil" do
|
||||
SiteConfig.welcome_notifications_live_at = nil
|
||||
create(:user, created_at: 1.day.ago)
|
||||
Rake::Task["broadcasts:send_welcome_notification_flow"].invoke
|
||||
expect(service).not_to have_received(:call)
|
||||
end
|
||||
|
||||
it "does not call upon users created before the start_date" do
|
||||
# Travel forward one week, and test that a user created 8 days ago does not receive notifications.
|
||||
Timecop.travel(one_week_from_today) do
|
||||
create(:user, created_at: 1.day.ago)
|
||||
Rake::Task["broadcasts:send_welcome_notification_flow"].invoke
|
||||
expect(service).not_to have_received(:call)
|
||||
end
|
||||
end
|
||||
|
||||
it "call upon users created less than a week ago" do
|
||||
# Travel forward one week (7)
|
||||
# then travel another week further (14 days)
|
||||
# On day 14, pressume user is created 6 days ago (created on day 8)
|
||||
# At this point notifications_live_at < week_ago is true and notifications_live_at is ignored.
|
||||
Timecop.travel(one_week_from_today + 1.week) do
|
||||
create_list(:user, 3, created_at: 6.days.ago)
|
||||
create_list(:user, 1, created_at: 10.days.ago)
|
||||
Rake::Task["broadcasts:send_welcome_notification_flow"].invoke
|
||||
expect(service).to have_received(:call).exactly(3)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
require "rails_helper"
|
||||
|
||||
RSpec.describe Broadcasts::SendWelcomeNotificationsWorker, type: :worker do
|
||||
let(:service) { Broadcasts::WelcomeNotification::Generator }
|
||||
let(:worker) { subject }
|
||||
|
||||
include_examples "#enqueues_on_correct_queue", "medium_priority"
|
||||
|
||||
describe "#perform" do
|
||||
it "does nothing if SiteConfig.welcome_notifications_live_at is nil" do
|
||||
SiteConfig.welcome_notifications_live_at = nil
|
||||
allow(service).to receive(:call)
|
||||
create(:user, created_at: 1.day.ago)
|
||||
worker.perform
|
||||
expect(service).not_to have_received(:call)
|
||||
end
|
||||
|
||||
it "sends welcome notifications to new users" do
|
||||
Timecop.freeze do
|
||||
SiteConfig.welcome_notifications_live_at = 3.days.ago
|
||||
allow(User).to receive(:mascot_account).and_return(create(:user))
|
||||
welcome_broadcast = create(:welcome_broadcast)
|
||||
user = create(:user, created_at: 1.day.ago)
|
||||
old_user = create(:user, created_at: 1.year.ago)
|
||||
|
||||
sidekiq_perform_enqueued_jobs(only: "Notifications::WelcomeNotificationWorker") do
|
||||
worker.perform
|
||||
end
|
||||
|
||||
welcome_notification = user.notifications.find_by(
|
||||
notifiable_id: welcome_broadcast.id, notifiable_type: "Broadcast",
|
||||
)
|
||||
expect(welcome_notification).not_to be_nil
|
||||
expect(old_user.notifications.count).to eq(0)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Reference in a new issue