-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |