diff --git a/app/workers/alert_check_worker.rb b/app/workers/alert_check_worker.rb new file mode 100644 index 000000000..83b8056d2 --- /dev/null +++ b/app/workers/alert_check_worker.rb @@ -0,0 +1,24 @@ +class AlertCheckWorker < ApplicationWorker + include SearchAlertList + + def perform(document_type:, display_name:) + content_items = get_alert_content_items(document_type:) + delivered = 0 + content_items.each do |ci| + if any_emails_delivered_for?(ci[:content_id], ci[:valid_from]) + delivered += 1 + else + Rails.logger.warn("Couldn't find any delivered emails for #{display_name}s with content id #{ci[:content_id]} (at #{ci[:url]})") + end + end + + Rails.logger.info("Checking #{display_name}s: #{delivered} out of #{content_items.count} alerts have been delivered to at least one recipient") + + Rails.cache.write("current_#{document_type}s", content_items.count, expires_in: 15.minutes) + Rails.cache.write("delivered_#{document_type}s", delivered, expires_in: 15.minutes) + end + + def any_emails_delivered_for?(content_id, valid_from) + Email.where("notify_status = 'delivered' AND content_id = ? AND created_at > ?", content_id, valid_from).exists? + end +end diff --git a/spec/workers/alert_check_worker_spec.rb b/spec/workers/alert_check_worker_spec.rb new file mode 100644 index 000000000..c0cfe2208 --- /dev/null +++ b/spec/workers/alert_check_worker_spec.rb @@ -0,0 +1,71 @@ +RSpec.describe AlertCheckWorker do + include SearchAlertListHelpers + + describe "#perform", caching: true do + def perform + described_class.new.perform(document_type: "medical_safety_alert", display_name: "Medical Safety Alert") + end + + context "there are no alerts older than an hour" do + before { stub_medical_safety_alert_feed(content_id: SecureRandom.uuid, age: 30.minutes) } + + it "should put 0/0 in the cache" do + expect(Rails.cache).to receive(:write).with("current_medical_safety_alerts", 0, expires_in: 15.minutes) + expect(Rails.cache).to receive(:write).with("delivered_medical_safety_alerts", 0, expires_in: 15.minutes) + perform + end + end + + context "there are no alerts younger than 1 day" do + before { stub_medical_safety_alert_feed(content_id: SecureRandom.uuid, age: 2.days) } + + it "should put 0/0 in the cache" do + expect(Rails.cache).to receive(:write).with("current_medical_safety_alerts", 0, expires_in: 15.minutes) + expect(Rails.cache).to receive(:write).with("delivered_medical_safety_alerts", 0, expires_in: 15.minutes) + perform + end + end + + context "there is a valid alert with delivered emails" do + before do + content_id = SecureRandom.uuid + stub_medical_safety_alert_feed(content_id:, age: 2.hours) + create(:email, content_id:, notify_status: "delivered") + end + + it "should put 1/1 in the cache" do + expect(Rails.cache).to receive(:write).with("current_medical_safety_alerts", 1, expires_in: 15.minutes) + expect(Rails.cache).to receive(:write).with("delivered_medical_safety_alerts", 1, expires_in: 15.minutes) + perform + end + end + + context "there is a valid alerts with undelivered emails" do + before do + content_id = SecureRandom.uuid + stub_medical_safety_alert_feed(content_id:, age: 2.hours) + create(:email, content_id:, notify_status: nil) + end + + it "should put 1/0 in the cache" do + expect(Rails.cache).to receive(:write).with("current_medical_safety_alerts", 1, expires_in: 15.minutes) + expect(Rails.cache).to receive(:write).with("delivered_medical_safety_alerts", 0, expires_in: 15.minutes) + perform + end + end + + context "there is a valid alert with delivered emails that are too old to be valid" do + before do + content_id = SecureRandom.uuid + stub_medical_safety_alert_feed(content_id:, age: 2.hours) + create(:email, content_id:, notify_status: "delivered", created_at: Time.zone.now - 3.hours) + end + + it "should put 1/0 in the cache" do + expect(Rails.cache).to receive(:write).with("current_medical_safety_alerts", 1, expires_in: 15.minutes) + expect(Rails.cache).to receive(:write).with("delivered_medical_safety_alerts", 0, expires_in: 15.minutes) + perform + end + end + end +end