From 6c4c028dcc7d7cdaaf6d618181f547ff375debb5 Mon Sep 17 00:00:00 2001 From: chiu Date: Wed, 26 Aug 2020 17:57:30 +0800 Subject: [PATCH] fix error --- .../admin/e_paper_criterias_controller.rb | 36 +++++-------------- app/helpers/admin/epaper_helper.rb | 33 +++++++++++++++++ lib/tasks/e_paper_tasks.rake | 5 +-- 3 files changed, 44 insertions(+), 30 deletions(-) create mode 100644 app/helpers/admin/epaper_helper.rb diff --git a/app/controllers/admin/e_paper_criterias_controller.rb b/app/controllers/admin/e_paper_criterias_controller.rb index 5067f49..f415fc7 100644 --- a/app/controllers/admin/e_paper_criterias_controller.rb +++ b/app/controllers/admin/e_paper_criterias_controller.rb @@ -47,26 +47,8 @@ def initialize def send_test_email paper_criteria = PaperCriteria.find(params[:id]) - papers = paper_criteria.epaper_topics.group_by(&:category) - papers_data = [] page = Page.where(:module => "e_paper").first.url rescue "" - papers_sorted = get_all_categories.map do |v| - tmp = papers.select{|cat,topics| (cat.id==v.id rescue false)} - if tmp.count==0 - tmp = nil - end - tmp - end.compact - papers_sorted.each do |paper| - paper.each do |category, topics| - topics_data = Array(topics).compact - papers_data << { - "category_title" => (category.title rescue nil), - "category" => category, - "topics" => topics_data - } - end - end + papers_data = Admin::EpaperHelper.get_paper_data(paper_criteria) email = Email.new(:mail_subject => paper_criteria.sending_title, :mail_lang => :zh_tw, :template => "admin/e_paper_criterias/emailer", :template_data => {"paper" => paper_criteria, "page" => "http://" + request.host_with_port + page, "siteurl" => "http://" + request.host_with_port ,"papers_data" => papers_data }, :mail_to => params[:email]) email.deliver render :json => {"success" => true}.to_json @@ -74,7 +56,13 @@ def initialize def send_newsletter Orbit::Application.load_tasks - rake = Rake::Task['epaper:send_email'].execute(OpenStruct.new({:paper_criteria_id => params[:id], :domain => request.host_with_port})) + Thread.new do + begin + rake = Rake::Task['epaper:send_email'].execute(OpenStruct.new({:paper_criteria_id => params[:id], :domain => request.host_with_port})) + rescue => e + puts [e,e.backtrace] + end + end # rake = Rake::Task['epaper:send_email'].invoke([params[:id], request.host_with_port]) render :json => {"success" => true}.to_json end @@ -84,12 +72,4 @@ def initialize def paper_criteria_params params.require(:paper_criteria).permit! end - def get_all_categories - app = ModuleApp.where(key: 'e_paper').first - asc_flag = app.asc rescue true - app.categories.enabled.sort_by do |category| - tmp = category.sort_number.to_i rescue 0 - asc_flag ? tmp : -tmp - end - end end \ No newline at end of file diff --git a/app/helpers/admin/epaper_helper.rb b/app/helpers/admin/epaper_helper.rb new file mode 100644 index 0000000..ccde8f0 --- /dev/null +++ b/app/helpers/admin/epaper_helper.rb @@ -0,0 +1,33 @@ +module Admin::EpaperHelper + extend self + def get_all_categories + app = ModuleApp.where(key: 'e_paper').first + asc_flag = app.asc rescue true + app.categories.enabled.sort_by do |category| + tmp = category.sort_number.to_i rescue 0 + asc_flag ? tmp : -tmp + end + end + def get_paper_data(paper_criteria) + papers = paper_criteria.epaper_topics.group_by(&:category) + papers_data = [] + papers_sorted = get_all_categories.map do |v| + tmp = papers.select{|cat,topics| (cat.id==v.id rescue false)} + if tmp.count==0 + tmp = nil + end + tmp + end.compact + papers_sorted.each do |paper| + paper.each do |category, topics| + topics_data = Array(topics).compact + papers_data << { + "category_title" => (category.title rescue nil), + "category" => category, + "topics" => topics_data + } + end + end + papers_data + end +end \ No newline at end of file diff --git a/lib/tasks/e_paper_tasks.rake b/lib/tasks/e_paper_tasks.rake index d0811fc..bbaf797 100644 --- a/lib/tasks/e_paper_tasks.rake +++ b/lib/tasks/e_paper_tasks.rake @@ -2,7 +2,8 @@ namespace :epaper do task :send_email, [:paper_criteria_id, :domain] => :environment do |task,args| paper = PaperCriteria.find(args[:paper_criteria_id]) rescue nil - page = Page.where(:module => "e_paper").first.url rescue "" + page = Page.where(:module => "e_paper").first.url rescue "" + papers_data = Admin::EpaperHelper.get_paper_data(paper) domain = args[:domain] if !paper.nil? subscribers = EPaperSubscriber.all @@ -14,7 +15,7 @@ namespace :epaper do subscriber.save if emails.count == mail_limit puts "Sending #{mail_limit} mails" - email = Email.new(:mail_subject => paper.sending_title, :mail_lang => :zh_tw, :template => "admin/e_paper_criterias/emailer", :template_data => {"paper" => paper, "page" => "http://" + domain + page, "siteurl" => "http://" + domain }, :mail_to => emails) + email = Email.new(:mail_subject => paper.sending_title, :mail_lang => :zh_tw, :template => "admin/e_paper_criterias/emailer", :template_data => {"paper" => paper, "page" => "http://" + domain + page, "siteurl" => "http://" + domain, "papers_data" => papers_data }, :mail_to => emails) email.deliver emails = [] sleep 0.5