Add extra mailing checks and background jobs

This commit is contained in:
Max Wofford
2025-05-14 16:44:28 -04:00
parent e7ce5b9312
commit 3c0a6863d6
4 changed files with 44 additions and 0 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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 <token>
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,

View File

@@ -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