From c8df2e097ba73ced41df81161b3fe1de8a38823e Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Fri, 17 Jan 2020 14:32:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=BC=E5=87=BApdf=E5=A4=9A=E7=BA=BF?= =?UTF-8?q?=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/tasks/zip_pack.rake | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/lib/tasks/zip_pack.rake b/lib/tasks/zip_pack.rake index 2f11cfe5a..e3215090b 100644 --- a/lib/tasks/zip_pack.rake +++ b/lib/tasks/zip_pack.rake @@ -1,4 +1,4 @@ -# 执行示例 bundle exec rake zip_pack:shixun_pack class=Course ids=123,2323 +# 执行示例 bundle exec rake zip_pack:shixun_pack class=Course ids=123,2323 parallel_size=4 # 执行示例 bundle exec rake zip_pack:shixun_pack class=HomeworkCommon ids=123,2323 namespace :zip_pack do desc "手工打包作品" @@ -7,6 +7,8 @@ namespace :zip_pack do task :shixun_pack => :environment do if ENV['class'] && ENV['ids'] + parallel_size = ENV['parallel_size'] || 2 + parallel_size = parallel_size.to_i env_ids = ENV['ids'].split(",").map(&:to_i) folders = [] if ENV['class'] == "Course" @@ -17,7 +19,7 @@ namespace :zip_pack do new_dir_name.gsub!(" ", "-") new_dir_name.gsub!("/", "_") new_folder = "#{OUTPUT_FOLDER}/#{new_dir_name}" - zip_homework_pdf homeworks, new_folder + zip_homework_pdf homeworks, new_folder, parallel_size folders << new_folder end else @@ -26,7 +28,7 @@ namespace :zip_pack do new_dir_name.gsub!(" ", "-") new_dir_name.gsub!("/", "_") new_folder = "#{OUTPUT_FOLDER}/#{new_dir_name}" - zip_homework_pdf homeworks, new_folder + zip_homework_pdf homeworks, new_folder, parallel_size folders << new_folder end @@ -34,38 +36,39 @@ namespace :zip_pack do end end - def zip_homework_pdf homeworks, folder + def zip_homework_pdf homeworks, folder, parallel_size Dir.mkdir(folder) unless File.directory?(folder) + homeworks = homeworks.includes(:score_student_works) - homeworks.includes(:score_student_works).each do |homework| + Parallel.each(homeworks, in_processes: 2) do |homework| out_file_name = "#{Time.now.strftime("%Y%m%d%H%M%S").to_s}-#{homework.course_id}-#{homework.name}.zip" out_file_name.gsub!(" ", "-") out_file_name.gsub!("/", "_") zipfile_name = "#{folder}/#{out_file_name}" - # Dir.mkdir(File.dirname(zipfile_name)) unless File.exist?(File.dirname(zipfile_name)) student_works = homework.score_student_works if student_works.size > 0 pdfs = [] Zip::File.open(zipfile_name, Zip::File::CREATE) do |zip| - student_works.find_each.map do |student_work| - export = ExportShixunReportService.new(homework, student_work) - pdf = export.to_pdf - pdfs << pdf - begin - zip.add(export.filename, pdf.path) - puts "out: #{export.filename}_#{pdf.path}" - rescue => ex - Rails.logger.error(ex.message) + student_works.find_in_batches(batch_size: 500) do |sw| + Parallel.each(sw, in_processes: parallel_size) do |student_work| + export = ExportShixunReportService.new(homework, student_work) + pdf = export.to_pdf + pdfs << pdf + begin + zip.add(export.filename, pdf.path) + puts "out: #{export.filename}_#{pdf.path}" + rescue => ex + Rails.logger.error(ex.message) - zip.get_output_stream('FILE_NOTICE.txt'){|os| os.write("文件重复:#{export.filename}") } - next + zip.get_output_stream('FILE_NOTICE.txt'){|os| os.write("文件重复:#{export.filename}") } + next + end end end end - zipfile = zipfile_name else zipfile = {:message => "no file"} end