fix error

This commit is contained in:
chiu 2020-08-28 17:09:45 +08:00
parent 0cfb699647
commit 464e195a8c
2 changed files with 17 additions and 33 deletions

View File

@ -56,9 +56,10 @@ def initialize
def send_newsletter def send_newsletter
Orbit::Application.load_tasks Orbit::Application.load_tasks
locale = I18n.locale
Thread.new do Thread.new do
begin begin
rake = Rake::Task['epaper:send_email'].execute(OpenStruct.new({:paper_criteria_id => params[:id], :domain => request.host_with_port})) rake = Rake::Task['epaper:send_email'].execute(OpenStruct.new({:paper_criteria_id => params[:id], :domain => request.host_with_port, :locale => locale}))
rescue => e rescue => e
puts [e,e.backtrace] puts [e,e.backtrace]
end end

View File

@ -1,43 +1,26 @@
# desc "Explaining what the task does" # desc "Explaining what the task does"
namespace :epaper do namespace :epaper do
task :send_email, [:paper_criteria_id, :domain] => :environment do |task,args| task :send_email, [:paper_criteria_id, :domain, :locale] => :environment do |task,args|
paper = PaperCriteria.find(args[:paper_criteria_id]) rescue nil 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) papers_data = Admin::EpaperHelper.get_paper_data(paper)
domain = args[:domain] domain = args[:domain]
if !paper.nil? if !paper.nil?
subscribers = EPaperSubscriber.all subscribers = EPaperSubscriber.where(:language.in=>paper.language_to_send,:email.nin=>['',nil])
emails = [] subscribers.update_all(:last_paper_sent => paper.id.to_s)
mail_limit = 25 emails = subscribers.collect(:email)
subscribers.each do |subscriber| site = Site.first
emails << subscriber.email if paper.language_to_send.include?(subscriber.language) && !subscriber.email.blank? email_limit_reciever = site[:site_settings][:email_limit_reciever].to_i rescue 0
subscriber.last_paper_sent = paper.id.to_s mail_limit = email_limit_reciever==0 ? 25 : email_limit_reciever
subscriber.save emails += paper.other_emails_ids
if emails.count == mail_limit emails.each_slice(mail_limit).each do |slice_emails|
puts "Sending #{mail_limit} mails" email = Email.new(:mail_subject => paper.sending_title, :mail_lang => args[:locale], :template => "admin/e_paper_criterias/emailer", :template_data => {"paper" => paper, "page" => "http://" + domain + page, "siteurl" => "http://" + domain, "papers_data" => papers_data }, :mail_to => slice_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) begin
email.deliver email.deliver
emails = [] puts "Sending #{slice_emails.count} emails"
sleep 0.5 rescue => e
puts [slice_emails,e,e.backtrace]
end end
end
other_emails = paper.other_emails_ids
if !other_emails.blank?
other_emails.each do |email|
emails << email
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, "papers_data" => papers_data }, :mail_to => emails)
email.deliver
emails = []
sleep 0.5
end
end
end
if !emails.empty?
puts "Sending last 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
end end
paper.save paper.save
end end