Skip to content

Commit

Permalink
Automatically schedule jobs when account settings change
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Shana Moore committed Jan 24, 2025
1 parent 627f3cb commit 3d92f8e
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion app/models/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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']
Expand Down Expand Up @@ -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
Expand All @@ -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

0 comments on commit 3d92f8e

Please sign in to comment.