From 9c934e27bec8a2d85ba3a5e548db5964ce1590f8 Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Fri, 8 Apr 2016 16:44:04 -0400 Subject: [PATCH] FEATURE: notify admins if problems have been reported on the dashboard for a while --- app/jobs/scheduled/dashboard_stats.rb | 7 +++++++ app/models/admin_dashboard_data.rb | 28 ++++++++++++++++++++++++++- config/locales/server.en.yml | 7 +++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/app/jobs/scheduled/dashboard_stats.rb b/app/jobs/scheduled/dashboard_stats.rb index 3a04c1070ec..b891b182a42 100644 --- a/app/jobs/scheduled/dashboard_stats.rb +++ b/app/jobs/scheduled/dashboard_stats.rb @@ -5,6 +5,13 @@ module Jobs every 30.minutes def execute(args) + problems_started_at = AdminDashboardData.problems_started_at + if problems_started_at && problems_started_at < 2.days.ago + # If there have been problems reported on the dashboard for a while, + # send a message to admins no more often than once per week. + GroupMessage.create(Group[:admins].name, :dashboard_problems, {limit_once_per: 7.days.to_i}) + end + stats = AdminDashboardData.fetch_stats set_cache(AdminDashboardData, stats) stats diff --git a/app/models/admin_dashboard_data.rb b/app/models/admin_dashboard_data.rb index 6a492ac9bb9..aa69d36a05e 100644 --- a/app/models/admin_dashboard_data.rb +++ b/app/models/admin_dashboard_data.rb @@ -50,7 +50,33 @@ class AdminDashboardData AdminDashboardData.problem_messages.each do |i18n_key| problems << AdminDashboardData.problem_message_check(i18n_key) end - problems.compact + problems.compact! + + if problems.empty? + self.class.clear_problems_started + else + self.class.set_problems_started + end + + problems + end + + def self.problems_started_key + "dash-problems-started-at" + end + + def self.set_problems_started + existing_time = $redis.get(problems_started_key) + $redis.setex(problems_started_key, 14.days.to_i, existing_time || Time.zone.now.to_s) + end + + def self.clear_problems_started + $redis.del problems_started_key + end + + def self.problems_started_at + s = $redis.get(problems_started_key) + s ? Time.zone.parse(s) : nil end # used for testing diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index b07da3cf194..0c0aaade66c 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -2044,6 +2044,13 @@ en: subject_template: "Downloading remote images disabled" text_body_template: "The `download_remote_images_to_local` setting was disabled because the disk space limit at `download_remote_images_threshold` was reached." + dashboard_problems: + subject_template: "Problems have been found" + text_body_template: | + Some problems are being reported on your admin dashboard. + + [Please review and fix them](%{base_url}/admin). + unsubscribe_link: | To stop receiving notifications for this particular topic, [click here](%{unsubscribe_url}). To unsubscribe from these emails, change your [user preferences](%{user_preferences_url})