From 9730d2e3a7f545edf7bd34e921fb25c6fe181751 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Tue, 19 Jul 2016 17:18:40 +0800 Subject: [PATCH] FIX: Load scheduled job from a file. --- .../scheduled/daily_performance_report.rb | 57 ++++++++++++++++++ .../plugin.rb | 59 +------------------ 2 files changed, 58 insertions(+), 58 deletions(-) create mode 100644 plugins/discourse-nginx-performance-report/app/jobs/scheduled/daily_performance_report.rb diff --git a/plugins/discourse-nginx-performance-report/app/jobs/scheduled/daily_performance_report.rb b/plugins/discourse-nginx-performance-report/app/jobs/scheduled/daily_performance_report.rb new file mode 100644 index 00000000000..78b25666e4d --- /dev/null +++ b/plugins/discourse-nginx-performance-report/app/jobs/scheduled/daily_performance_report.rb @@ -0,0 +1,57 @@ +module Jobs + class DailyPerformanceReport < Jobs::Scheduled + every 1.day + per_host + + def execute(args) + if SiteSetting.daily_performance_report && + RailsMultisite::ConnectionManagement.current_db == "default" + result = `ruby #{Rails.root}/plugins/discourse-nginx-performance-report/script/nginx_analyze.rb --limit 1440` + + report_data = + if result.strip.empty? + <<~TEXT + Report is only available in latest image, please run: + + ```text + cd /var/discourse + ./launcher rebuild app + ``` + TEXT + else + "```text\n#{result}\n```" + end + + PostCreator.create(Discourse.system_user, + topic_id: performance_topic_id, + raw: report_data, + skip_validations: true) + + end + end + + def performance_topic_id + if SiteSetting.performance_report_topic_id > 0 + topic = Topic.find_by(id: SiteSetting.performance_report_topic_id) + return topic.id if topic + end + + staff_category = Category.find_by(id: SiteSetting.staff_category_id) + raise StandardError, "Staff category was not found" unless staff_category + + post = PostCreator.create(Discourse.system_user, + raw: I18n.t('performance_report.initial_post_raw'), + category: staff_category.name, + title: I18n.t('performance_report.initial_topic_title'), + skip_validations: true) + + + unless post && post.topic_id + raise StandardError, "Could not create or retrieve performance report topic id" + end + + SiteSetting.performance_report_topic_id = post.topic_id + end + + end +end diff --git a/plugins/discourse-nginx-performance-report/plugin.rb b/plugins/discourse-nginx-performance-report/plugin.rb index 7655a20666d..4b139778def 100644 --- a/plugins/discourse-nginx-performance-report/plugin.rb +++ b/plugins/discourse-nginx-performance-report/plugin.rb @@ -4,62 +4,5 @@ # url: https://github.com/discourse/discourse/tree/master/plugins/discourse-nginx-performance-report after_initialize do - module ::Jobs - class DailyPerformanceReport < Scheduled - every 1.day - per_host - - def execute(args) - if SiteSetting.daily_performance_report && - RailsMultisite::ConnectionManagement.current_db == "default" - result = `ruby #{Rails.root}/plugins/discourse-nginx-performance-report/script/nginx_analyze.rb --limit 1440` - - report_data = - if result.strip.empty? - <<~TEXT - Report is only available in latest image, please run: - - ```text - cd /var/discourse - ./launcher rebuild app - ``` - TEXT - else - "```text\n#{result}\n```" - end - - PostCreator.create(Discourse.system_user, - topic_id: performance_topic_id, - raw: report_data, - skip_validations: true) - - end - end - - def performance_topic_id - if SiteSetting.performance_report_topic_id > 0 - topic = Topic.find_by(id: SiteSetting.performance_report_topic_id) - return topic.id if topic - end - - staff_category = Category.find_by(id: SiteSetting.staff_category_id) - raise StandardError, "Staff category was not found" unless staff_category - - post = PostCreator.create(Discourse.system_user, - raw: I18n.t('performance_report.initial_post_raw'), - category: staff_category.name, - title: I18n.t('performance_report.initial_topic_title'), - skip_validations: true) - - - unless post && post.topic_id - raise StandardError, "Could not create or retrieve performance report topic id" - end - - SiteSetting.performance_report_topic_id = post.topic_id - end - - end - end - + load File.expand_path("../app/jobs/scheduled/daily_performance_report.rb", __FILE__) end