From 3d92f8ef1162e543ff4cff64277d92fe4f338f28 Mon Sep 17 00:00:00 2001 From: Shana Moore Date: Fri, 24 Jan 2025 14:37:54 -0800 Subject: [PATCH] Automatically schedule jobs when account settings change When certain account settings (user_analytics, batch_email_notifications, or depositor_email_notifications) are updated, the system will now automatically schedule the corresponding background jobs. This eliminates the need for a server restart to activate these features. - Added after_save callback to detect settings changes - Added logic to compare relevant settings before and after save - Triggers find_or_schedule_jobs when settings change This ensures features like user analytics start working immediately after being enabled through the settings interface. --- app/models/account.rb | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/app/models/account.rb b/app/models/account.rb index c62e382a6..00dc0f2bc 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -47,6 +47,8 @@ class Account < ApplicationRecord format: { with: /\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/ }, unless: proc { |a| a.tenant == 'public' || a.tenant == 'single' } + after_save :schedule_jobs_if_settings_changed + def self.admin_host host = ENV.fetch('HYKU_ADMIN_HOST', nil) host ||= ENV['HOST'] @@ -182,7 +184,6 @@ def find_or_schedule_jobs LeaseAutoExpiryJob ] - # Add conditional checks based on a config or environment variable jobs_to_schedule << BatchEmailNotificationJob if batch_email_notifications jobs_to_schedule << DepositorEmailNotificationJob if depositor_email_notifications jobs_to_schedule << UserStatCollectionJob if user_analytics @@ -193,5 +194,24 @@ def find_or_schedule_jobs account ? AccountElevator.switch!(account) : reset! end + + private + + def schedule_jobs_if_settings_changed + return unless self.class.column_names.include?('settings') + + relevant_settings = [ + 'batch_email_notifications', + 'depositor_email_notifications', + 'user_analytics' + ] + + return unless saved_changes['settings'] + old_settings = saved_changes['settings'][0] || {} + new_settings = saved_changes['settings'][1] || {} + + return unless old_settings.slice(*relevant_settings) != new_settings.slice(*relevant_settings) + find_or_schedule_jobs + end end # rubocop:enable Metrics/ClassLength