2024-04-03 23:20:43 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Jobs
|
2024-05-07 04:12:55 +08:00
|
|
|
class DiscourseAutomation::Tracker < ::Jobs::Scheduled
|
2024-04-03 23:20:43 +08:00
|
|
|
every 1.minute
|
|
|
|
|
2024-11-06 06:27:49 +08:00
|
|
|
BATCH_LIMIT = 300
|
2024-04-03 23:20:43 +08:00
|
|
|
|
|
|
|
def execute(_args = nil)
|
|
|
|
return unless SiteSetting.discourse_automation_enabled
|
|
|
|
|
2024-05-07 04:12:55 +08:00
|
|
|
::DiscourseAutomation::PendingAutomation
|
2024-04-03 23:20:43 +08:00
|
|
|
.includes(:automation)
|
|
|
|
.limit(BATCH_LIMIT)
|
|
|
|
.where("execute_at < ?", Time.now)
|
|
|
|
.find_each { |pending_automation| run_pending_automation(pending_automation) }
|
|
|
|
|
2024-05-07 04:12:55 +08:00
|
|
|
::DiscourseAutomation::PendingPm
|
2024-04-03 23:20:43 +08:00
|
|
|
.includes(:automation)
|
|
|
|
.limit(BATCH_LIMIT)
|
|
|
|
.where("execute_at < ?", Time.now)
|
|
|
|
.find_each { |pending_pm| send_pending_pm(pending_pm) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def send_pending_pm(pending_pm)
|
2024-05-01 14:01:58 +08:00
|
|
|
DistributedMutex.synchronize(
|
|
|
|
"automation_send_pending_pm_#{pending_pm.id}",
|
|
|
|
validity: 30.minutes,
|
|
|
|
) do
|
2024-05-07 04:12:55 +08:00
|
|
|
next if !::DiscourseAutomation::PendingPm.exists?(pending_pm.id)
|
2024-05-01 14:01:58 +08:00
|
|
|
|
2024-05-07 04:12:55 +08:00
|
|
|
::DiscourseAutomation::Scriptable::Utils.send_pm(
|
2024-05-01 14:01:58 +08:00
|
|
|
pending_pm.attributes.slice("target_usernames", "title", "raw"),
|
|
|
|
sender: pending_pm.sender,
|
|
|
|
prefers_encrypt: pending_pm.prefers_encrypt,
|
|
|
|
)
|
|
|
|
|
|
|
|
pending_pm.destroy!
|
|
|
|
end
|
2024-04-03 23:20:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def run_pending_automation(pending_automation)
|
2024-05-01 14:01:58 +08:00
|
|
|
DistributedMutex.synchronize(
|
|
|
|
"process_pending_automation_#{pending_automation.id}",
|
|
|
|
validity: 30.minutes,
|
|
|
|
) do
|
2024-05-07 04:12:55 +08:00
|
|
|
next if !::DiscourseAutomation::PendingAutomation.exists?(pending_automation.id)
|
2024-05-01 14:01:58 +08:00
|
|
|
|
|
|
|
pending_automation.automation.trigger!(
|
|
|
|
"kind" => pending_automation.automation.trigger,
|
|
|
|
"execute_at" => pending_automation.execute_at,
|
|
|
|
)
|
|
|
|
|
|
|
|
pending_automation.destroy!
|
|
|
|
end
|
2024-04-03 23:20:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|