Skip to content

Commit

Permalink
Add TravelAdviceAlertCheckWorker
Browse files Browse the repository at this point in the history
- Tests
  • Loading branch information
KludgeKML committed Nov 16, 2023
1 parent a041da5 commit 6058ae3
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
24 changes: 24 additions & 0 deletions app/workers/travel_advice_alert_check_worker.rb
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
71 changes: 71 additions & 0 deletions spec/workers/travel_advice_alert_check_worker_spec.rb
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

0 comments on commit 6058ae3

Please sign in to comment.