mirror of
https://github.com/SrIzan10/hc-harbor.git
synced 2026-05-01 10:45:21 +00:00
Add extra mailing checks and background jobs
This commit is contained in:
12
app/jobs/attempt_to_deliver_physical_mail_job.rb
Normal file
12
app/jobs/attempt_to_deliver_physical_mail_job.rb
Normal 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
|
||||
14
app/jobs/concerns/has_enqueue_control.rb
Normal file
14
app/jobs/concerns/has_enqueue_control.rb
Normal 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
|
||||
@@ -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,
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user