From 060ed32c48f0c1f41c607d711790c111e19deb62 Mon Sep 17 00:00:00 2001 From: Molly Struve Date: Fri, 21 Aug 2020 11:57:08 -0500 Subject: [PATCH] Refactor:Send Welcome Broadcast Notifications From Sidekiq (#9923) * Refactor:Send Welcome Broadcast Notifications From Sidekiq * remove old spec and add to new one --- .../send_welcome_notifications_worker.rb | 21 ++++++++ config/schedule.yml | 3 ++ lib/tasks/broadcasts.rake | 16 ------ spec/tasks/broadcasts_spec.rb | 51 ------------------- .../send_welcome_notifications_worker_spec.rb | 38 ++++++++++++++ 5 files changed, 62 insertions(+), 67 deletions(-) create mode 100644 app/workers/broadcasts/send_welcome_notifications_worker.rb delete mode 100644 lib/tasks/broadcasts.rake delete mode 100644 spec/tasks/broadcasts_spec.rb create mode 100644 spec/workers/broadcasts/send_welcome_notifications_worker_spec.rb diff --git a/app/workers/broadcasts/send_welcome_notifications_worker.rb b/app/workers/broadcasts/send_welcome_notifications_worker.rb new file mode 100644 index 000000000..40bb1f6b9 --- /dev/null +++ b/app/workers/broadcasts/send_welcome_notifications_worker.rb @@ -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 diff --git a/config/schedule.yml b/config/schedule.yml index 468ec7901..8690845f3 100644 --- a/config/schedule.yml +++ b/config/schedule.yml @@ -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" diff --git a/lib/tasks/broadcasts.rake b/lib/tasks/broadcasts.rake deleted file mode 100644 index 2e6455ff4..000000000 --- a/lib/tasks/broadcasts.rake +++ /dev/null @@ -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 diff --git a/spec/tasks/broadcasts_spec.rb b/spec/tasks/broadcasts_spec.rb deleted file mode 100644 index 3a4c0f413..000000000 --- a/spec/tasks/broadcasts_spec.rb +++ /dev/null @@ -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 diff --git a/spec/workers/broadcasts/send_welcome_notifications_worker_spec.rb b/spec/workers/broadcasts/send_welcome_notifications_worker_spec.rb new file mode 100644 index 000000000..7ef6163d1 --- /dev/null +++ b/spec/workers/broadcasts/send_welcome_notifications_worker_spec.rb @@ -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