From 3c0a6863d64718d6c942ab9cb3a6562f82413f02 Mon Sep 17 00:00:00 2001 From: Max Wofford Date: Wed, 14 May 2025 16:44:28 -0400 Subject: [PATCH] Add extra mailing checks and background jobs --- app/jobs/attempt_to_deliver_physical_mail_job.rb | 12 ++++++++++++ app/jobs/concerns/has_enqueue_control.rb | 14 ++++++++++++++ app/models/physical_mail.rb | 10 ++++++++++ config/initializers/good_job.rb | 8 ++++++++ 4 files changed, 44 insertions(+) create mode 100644 app/jobs/attempt_to_deliver_physical_mail_job.rb create mode 100644 app/jobs/concerns/has_enqueue_control.rb diff --git a/app/jobs/attempt_to_deliver_physical_mail_job.rb b/app/jobs/attempt_to_deliver_physical_mail_job.rb new file mode 100644 index 0000000..949eefa --- /dev/null +++ b/app/jobs/attempt_to_deliver_physical_mail_job.rb @@ -0,0 +1,12 @@ +class AttemptToDeliverPhysicalMailJob < ApplicationJob + queue_as :literally_whenever + + include HasEnqueueControl + enqueue_limit 1 + + def perform + PhysicalMail.pending_delivery.find_each do |mail| + mail.deliver! + end + end +end diff --git a/app/jobs/concerns/has_enqueue_control.rb b/app/jobs/concerns/has_enqueue_control.rb new file mode 100644 index 0000000..62307dc --- /dev/null +++ b/app/jobs/concerns/has_enqueue_control.rb @@ -0,0 +1,14 @@ +module HasEnqueueControl + extend ActiveSupport::Concern + include GoodJob::ActiveJobExtensions::Concurrency + + class_methods do + def enqueue_limit(limit = 1) + good_job_control_concurrency_with( + total_limit: limit, + key: "enqueue_control_#{self.name.underscore}", + drop: true + ) + end + end +end diff --git a/app/models/physical_mail.rb b/app/models/physical_mail.rb index 84954a2..d9e8304 100644 --- a/app/models/physical_mail.rb +++ b/app/models/physical_mail.rb @@ -14,6 +14,12 @@ class PhysicalMail < ApplicationRecord first_time_7_streak: 1 } + scope :pending_delivery, -> { + where(status: :pending) + .joins(:user) + .joins("INNER JOIN mailing_addresses ON mailing_addresses.user_id = users.id") + } + def link_to_theseus return nil if theseus_id.nil? @@ -27,11 +33,15 @@ class PhysicalMail < ApplicationRecord end def deliver! + return if status == :sent || theseus_id.present? + slug = "hackatime-#{mission_type.to_s.gsub("_", "-")}" flavors = FlavorText.compliment flavors.concat(FlavorText.rare_compliment) if rand(10) == 0 + return nil unless user.mailing_address.present? + # authorization: Bearer response = HTTP.auth("Bearer #{ENV["MAIL_HACKCLUB_TOKEN"]}").post("https://mail.hackclub.com/api/v1/letter_queues/#{slug}", json: { recipient_email: user.email_addresses.first.email, diff --git a/config/initializers/good_job.rb b/config/initializers/good_job.rb index 0d7d93c..e5cc32c 100644 --- a/config/initializers/good_job.rb +++ b/config/initializers/good_job.rb @@ -105,6 +105,14 @@ Rails.application.configure do cron: "* * * * *", class: "Cache::HeartbeatCountsJob", kwargs: { force_reload: true } + }, + check_streak_physical_mail: { + cron: "0 * * * *", # Run before AttemptToDeliverPhysicalMailJob + class: "CheckStreakPhysicalMailJob" + }, + attempt_to_deliver_physical_mail: { + cron: "5 * * * *", # Run after physical mail is created + class: "AttemptToDeliverPhysicalMailJob" } } end