From 31f626f6367722a614d210fdbf5889ff4839e5e2 Mon Sep 17 00:00:00 2001 From: Keith Lawrence Date: Thu, 9 Nov 2023 16:39:04 +0000 Subject: [PATCH] Add MedicalCheckAlertWorker --- app/workers/medical_alert_check_worker.rb | 33 +++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 app/workers/medical_alert_check_worker.rb diff --git a/app/workers/medical_alert_check_worker.rb b/app/workers/medical_alert_check_worker.rb new file mode 100644 index 000000000..7ae474f88 --- /dev/null +++ b/app/workers/medical_alert_check_worker.rb @@ -0,0 +1,33 @@ +class MedicalAlertCheckWorker < ApplicationWorker + FEED_PATH = "/drug-device-alerts.atom".freeze + NUMBER_OF_ITEMS = 5 + + def perform + content_ids = valid_content_ids + delivered = 0 + content_ids.each { |ci| delivered += 1 if any_emails_delivered_for?(ci) } + + Rails.logger.info("Checking alerts: #{delivered} out of #{content_ids.count} alerts have been delivered to at least one recipient") + + Rails.cache.write("current_medical_alerts", content_ids.count, expires_in: 30.minutes) + Rails.cache.write("delivered_medical_alerts", delivered, expires_in: 30.minutes) + end + + def any_emails_delivered_for?(content_id) + Email.where(notify_status: "delivered", content_id:).exists? + end + + def feed_url + Plek.website_root + FEED_PATH + end + + def valid_content_ids + URI.parse(feed_url).open do |raw_rss| + RSS::Parser + .parse(raw_rss) + .items.first(NUMBER_OF_ITEMS) + .select { |entry| entry.updated.content < Time.zone.now - 3600 } + .map(&:dc_identifier) + end + end +end