Skip to content

Commit

Permalink
Add MedicalCheckAlertWorker
Browse files Browse the repository at this point in the history
  • Loading branch information
KludgeKML committed Nov 9, 2023
1 parent 0dd2a55 commit 31f626f
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions app/workers/medical_alert_check_worker.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 31f626f

Please sign in to comment.