-
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.
- Tests
- 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 TravelAdviceAlertCheckWorker < ApplicationWorker | ||
include SearchAlertList | ||
|
||
def perform | ||
content_items = get_alert_content_items(alert_type: "travel_advice", min_age: 1.hour, max_age: 150.minutes) | ||
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 Travel Advice Alert with content id #{ci[:content_id]} (at #{ci[:link]})}") | ||
end | ||
end | ||
|
||
Rails.logger.info("Checking travel advice alerts: #{delivered} out of #{content_items.count} alerts have been delivered to at least one recipient") | ||
|
||
Rails.cache.write("current_travel_advice_alerts", content_items.count, expires_in: 30.minutes) | ||
Rails.cache.write("delivered_travel_advice_alerts", delivered, expires_in: 30.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 TravelAdviceAlertCheckWorker do | ||
include SearchAlertListHelpers | ||
|
||
describe "#perform", caching: true do | ||
def perform | ||
described_class.new.perform | ||
end | ||
|
||
context "there are no alerts older than an hour" do | ||
before { stub_travel_advice_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_travel_advice_alerts", 0, expires_in: 30.minutes) | ||
expect(Rails.cache).to receive(:write).with("delivered_travel_advice_alerts", 0, expires_in: 30.minutes) | ||
perform | ||
end | ||
end | ||
|
||
context "there are no alerts younger than 2 days" do | ||
before { stub_travel_advice_alert_feed(content_id: SecureRandom.uuid, age: 3.days) } | ||
|
||
it "should put 0/0 in the cache" do | ||
expect(Rails.cache).to receive(:write).with("current_travel_advice_alerts", 0, expires_in: 30.minutes) | ||
expect(Rails.cache).to receive(:write).with("delivered_travel_advice_alerts", 0, expires_in: 30.minutes) | ||
perform | ||
end | ||
end | ||
|
||
context "there is a valid alert with delivered emails" do | ||
before do | ||
content_id = SecureRandom.uuid | ||
stub_travel_advice_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_travel_advice_alerts", 1, expires_in: 30.minutes) | ||
expect(Rails.cache).to receive(:write).with("delivered_travel_advice_alerts", 1, expires_in: 30.minutes) | ||
perform | ||
end | ||
end | ||
|
||
context "there is a valid alert with undelivered emails" do | ||
before do | ||
content_id = SecureRandom.uuid | ||
stub_travel_advice_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_travel_advice_alerts", 1, expires_in: 30.minutes) | ||
expect(Rails.cache).to receive(:write).with("delivered_travel_advice_alerts", 0, expires_in: 30.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_travel_advice_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_travel_advice_alerts", 1, expires_in: 30.minutes) | ||
expect(Rails.cache).to receive(:write).with("delivered_travel_advice_alerts", 0, expires_in: 30.minutes) | ||
perform | ||
end | ||
end | ||
end | ||
end |