DEV: Convert some files to autoloading and various improvements (#26860)

This commit is contained in:
Osama Sayegh
2024-05-06 23:12:55 +03:00
committed by GitHub
parent 8bbcd409e3
commit 2f2355b0ad
63 changed files with 90 additions and 186 deletions

View File

@ -1,7 +1,7 @@
# frozen_string_literal: true
module Jobs
class DiscourseAutomationCallZapierWebhook < ::Jobs::Base
class DiscourseAutomation::CallZapierWebhook < ::Jobs::Base
def execute(args)
RateLimiter.new(nil, "discourse_automation_call_zapier", 5, 30).performed!

View File

@ -1,7 +1,7 @@
# frozen_string_literal: true
module Jobs
class DiscourseAutomationTrigger < ::Jobs::Base
class DiscourseAutomation::Trigger < ::Jobs::Base
RETRY_TIMES = [5.minute, 15.minute, 120.minute]
sidekiq_options retry: RETRY_TIMES.size
@ -18,11 +18,12 @@ module Jobs
end
def execute(args)
automation = DiscourseAutomation::Automation.find_by(id: args[:automation_id], enabled: true)
automation =
::DiscourseAutomation::Automation.find_by(id: args[:automation_id], enabled: true)
return if !automation
context = DiscourseAutomation::Automation.deserialize_context(args[:context])
context = ::DiscourseAutomation::Automation.deserialize_context(args[:context])
automation.running_in_background!
automation.trigger!(context)

View File

@ -1,13 +1,13 @@
# frozen_string_literal: true
module Jobs
class StalledTopicTracker < ::Jobs::Scheduled
class DiscourseAutomation::StalledTopicTracker < ::Jobs::Scheduled
every 1.hour
def execute(_args = nil)
name = DiscourseAutomation::Triggers::STALLED_TOPIC
name = ::DiscourseAutomation::Triggers::STALLED_TOPIC
DiscourseAutomation::Automation
::DiscourseAutomation::Automation
.where(trigger: name, enabled: true)
.find_each do |automation|
fields = automation.serialized_fields
@ -17,7 +17,7 @@ module Jobs
categories = fields.dig("categories", "value")
tags = fields.dig("tags", "value")
StalledTopicFinder
::DiscourseAutomation::StalledTopicFinder
.call(stalled_date, categories: categories, tags: tags)
.each do |result|
topic = Topic.find_by(id: result.id)
@ -30,7 +30,7 @@ module Jobs
def run_trigger(automation, topic)
automation.trigger!(
"kind" => DiscourseAutomation::Triggers::STALLED_TOPIC,
"kind" => ::DiscourseAutomation::Triggers::STALLED_TOPIC,
"topic" => topic,
"placeholders" => {
"topic_url" => topic.url,

View File

@ -1,13 +1,13 @@
# frozen_string_literal: true
module Jobs
class StalledWikiTracker < ::Jobs::Scheduled
class DiscourseAutomation::StalledWikiTracker < ::Jobs::Scheduled
every 10.minutes
def execute(_args = nil)
name = DiscourseAutomation::Triggers::STALLED_WIKI
name = ::DiscourseAutomation::Triggers::STALLED_WIKI
DiscourseAutomation::Automation
::DiscourseAutomation::Automation
.where(trigger: name, enabled: true)
.find_each do |automation|
stalled_after = automation.trigger_field("stalled_after")
@ -43,7 +43,7 @@ module Jobs
).compact.uniq
automation.trigger!(
"kind" => DiscourseAutomation::Triggers::STALLED_WIKI,
"kind" => ::DiscourseAutomation::Triggers::STALLED_WIKI,
"post" => post,
"topic" => post.topic,
"usernames" => User.where(id: user_ids).pluck(:username),

View File

@ -1,7 +1,7 @@
# frozen_string_literal: true
module Jobs
class DiscourseAutomationTracker < ::Jobs::Scheduled
class DiscourseAutomation::Tracker < ::Jobs::Scheduled
every 1.minute
BATCH_LIMIT ||= 300
@ -9,13 +9,13 @@ module Jobs
def execute(_args = nil)
return unless SiteSetting.discourse_automation_enabled
DiscourseAutomation::PendingAutomation
::DiscourseAutomation::PendingAutomation
.includes(:automation)
.limit(BATCH_LIMIT)
.where("execute_at < ?", Time.now)
.find_each { |pending_automation| run_pending_automation(pending_automation) }
DiscourseAutomation::PendingPm
::DiscourseAutomation::PendingPm
.includes(:automation)
.limit(BATCH_LIMIT)
.where("execute_at < ?", Time.now)
@ -27,9 +27,9 @@ module Jobs
"automation_send_pending_pm_#{pending_pm.id}",
validity: 30.minutes,
) do
next if !DiscourseAutomation::PendingPm.exists?(pending_pm.id)
next if !::DiscourseAutomation::PendingPm.exists?(pending_pm.id)
DiscourseAutomation::Scriptable::Utils.send_pm(
::DiscourseAutomation::Scriptable::Utils.send_pm(
pending_pm.attributes.slice("target_usernames", "title", "raw"),
sender: pending_pm.sender,
prefers_encrypt: pending_pm.prefers_encrypt,
@ -44,7 +44,7 @@ module Jobs
"process_pending_automation_#{pending_automation.id}",
validity: 30.minutes,
) do
next if !DiscourseAutomation::PendingAutomation.exists?(pending_automation.id)
next if !::DiscourseAutomation::PendingAutomation.exists?(pending_automation.id)
pending_automation.automation.trigger!(
"kind" => pending_automation.automation.trigger,

View File

@ -126,7 +126,7 @@ module DiscourseAutomation
def trigger_in_background!(context = {})
Jobs.enqueue(
:discourse_automation_trigger,
Jobs::DiscourseAutomation::Trigger,
automation_id: id,
context: self.class.serialize_context(context),
)

View File

@ -1,44 +0,0 @@
# frozen_string_literal: true
class StalledTopicFinder
def self.call(stalled_date, tags: nil, categories: nil)
sql = <<~SQL
SELECT t.id
FROM topics t
SQL
sql += <<~SQL if tags
JOIN topic_tags ON topic_tags.topic_id = t.id
JOIN tags
ON tags.name IN (:tags)
AND tags.id = topic_tags.tag_id
SQL
sql += <<~SQL
WHERE t.deleted_at IS NULL
AND t.posts_count > 0
AND t.archetype != 'private_message'
AND NOT t.closed
AND NOT t.archived
AND NOT EXISTS (
SELECT p.id
FROM posts p
WHERE t.id = p.topic_id
AND p.deleted_at IS NULL
AND t.user_id = p.user_id
AND p.created_at > :stalled_date
LIMIT 1
)
SQL
sql += <<~SQL if categories
AND t.category_id IN (:categories)
SQL
sql += <<~SQL
LIMIT 250
SQL
DB.query(sql, categories: categories, tags: tags, stalled_date: stalled_date)
end
end