A platform for community discussion. Free, open, simple.
# frozen_string_literal: true
module Jobs
class CheckUpcomingChanges < ::Jobs::Scheduled
every 20.minutes
def execute(args)
site = RailsMultisite::ConnectionManagement.current_db
verbose_log(
site,
:info,
"Starting change tracker and promotion notifier for upcoming changes",
)
if SiteSetting.upcoming_change_site_settings.empty?
verbose_log(site, :info, "No upcoming changes present.")
return
end
DistributedMutex.synchronize("check_upcoming_changes_#{site}", validity: 10.minutes) do
track_changes(site)
notify_promotions(site)
end
end
private
# Note that we do not notify admins about new upcoming changes,
# or those that reach promotion status - 1, inline in this service.
# That only happens once a week in Jobs::NotifyAdminsOfAvailableUpcomingChanges
def track_changes(site)
UpcomingChanges::Track.call(guardian: Discourse.system_user.guardian) do |result|
on_success do |added_changes:, removed_changes:, status_changes:|
added_changes.each do |change_name|
verbose_log(site, :info, "Added upcoming change '#{change_name}'")
end
removed_changes.each do |change_name|
verbose_log(site, :info, "Removed upcoming change '#{change_name}'")
end
status_changes.each do |change_name, details|
verbose_log(
site,
:info,
"Status changed for upcoming change '#{change_name}' from #{details[:previous_value]} to #{details[:new_value]}",
)
end
end
on_model_not_found(:all_admins) do |model_name|
verbose_log(site, :debug, "No admins found, skipping tracking of upcoming changes")
end
on_failure do
verbose_log(
site,
:error,
"Failed to track upcoming changes, an unexpected error occurred.\n\n#{result.inspect_steps}",
)
end
end
end
def notify_promotions(site)
UpcomingChanges::NotifyPromotions.call(guardian: Discourse.system_user.guardian) do |result|
on_success do |change_notification_statuses:|
change_notification_statuses.each do |setting_name, status|
if status[:success]
verbose_log(site, :info, "Notified site admins about promotion of '#{setting_name}'")
next
end
log_level =
case status[:error_key]
when :does_not_meet_or_exceed_promotion_status
:info
when :should_not_be_displayed
:debug
when :already_promoted
:debug
when :already_manually_toggled
:debug
else
:error
end
backtrace =
if status[:backtrace].present?
backtrace = status[:backtrace]&.join("\n")
else
backtrace = status[:result_inspect_steps] || "N/A"
end
verbose_log(
site,
log_level,
"Failed to notify about promotion of '#{setting_name}': #{status[:error]} with backtrace: #{backtrace}",
)
end
end
on_model_not_found(:admin_user_ids) do |model_name|
verbose_log(site, :debug, "No admins found, skipping notification of promotions")
end
on_failure do
verbose_log(
site,
:error,
"Failed to notify about promotions, an unexpected error occurred.\n\n#{result.inspect_steps}",
)
end
end
end
def verbose_log(site, level, message)
return unless SiteSetting.upcoming_change_verbose_logging
Rails.logger.public_send(level, "[CheckUpcomingChanges (#{site})] #{message}")
end
end
end