Merge branch 'dev_aliyun' into develop

dev_tj
cxt 5 years ago
commit 89a2253662

@ -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 # 执行示例 bundle exec rake zip_pack:shixun_pack class=HomeworkCommon ids=123,2323
namespace :zip_pack do namespace :zip_pack do
desc "手工打包作品" desc "手工打包作品"
@ -7,6 +7,8 @@ namespace :zip_pack do
task :shixun_pack => :environment do task :shixun_pack => :environment do
if ENV['class'] && ENV['ids'] 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) env_ids = ENV['ids'].split(",").map(&:to_i)
folders = [] folders = []
if ENV['class'] == "Course" if ENV['class'] == "Course"
@ -17,7 +19,7 @@ namespace :zip_pack do
new_dir_name.gsub!(" ", "-") new_dir_name.gsub!(" ", "-")
new_dir_name.gsub!("/", "_") new_dir_name.gsub!("/", "_")
new_folder = "#{OUTPUT_FOLDER}/#{new_dir_name}" new_folder = "#{OUTPUT_FOLDER}/#{new_dir_name}"
zip_homework_pdf homeworks, new_folder zip_homework_pdf homeworks, new_folder, parallel_size
folders << new_folder folders << new_folder
end end
else else
@ -26,7 +28,7 @@ namespace :zip_pack do
new_dir_name.gsub!(" ", "-") new_dir_name.gsub!(" ", "-")
new_dir_name.gsub!("/", "_") new_dir_name.gsub!("/", "_")
new_folder = "#{OUTPUT_FOLDER}/#{new_dir_name}" new_folder = "#{OUTPUT_FOLDER}/#{new_dir_name}"
zip_homework_pdf homeworks, new_folder zip_homework_pdf homeworks, new_folder, parallel_size
folders << new_folder folders << new_folder
end end
@ -34,38 +36,39 @@ namespace :zip_pack do
end end
end end
def zip_homework_pdf homeworks, folder def zip_homework_pdf homeworks, folder, parallel_size
Dir.mkdir(folder) unless File.directory?(folder) 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 = "#{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!(" ", "-")
out_file_name.gsub!("/", "_") out_file_name.gsub!("/", "_")
zipfile_name = "#{folder}/#{out_file_name}" 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 student_works = homework.score_student_works
if student_works.size > 0 if student_works.size > 0
pdfs = [] pdfs = []
Zip::File.open(zipfile_name, Zip::File::CREATE) do |zip| Zip::File.open(zipfile_name, Zip::File::CREATE) do |zip|
student_works.find_each.map do |student_work| student_works.find_in_batches(batch_size: 500) do |sw|
export = ExportShixunReportService.new(homework, student_work) Parallel.each(sw, in_processes: parallel_size) do |student_work|
pdf = export.to_pdf export = ExportShixunReportService.new(homework, student_work)
pdfs << pdf pdf = export.to_pdf
begin pdfs << pdf
zip.add(export.filename, pdf.path) begin
puts "out: #{export.filename}_#{pdf.path}" zip.add(export.filename, pdf.path)
rescue => ex puts "out: #{export.filename}_#{pdf.path}"
Rails.logger.error(ex.message) rescue => ex
Rails.logger.error(ex.message)
zip.get_output_stream('FILE_NOTICE.txt'){|os| os.write("文件重复:#{export.filename}") } zip.get_output_stream('FILE_NOTICE.txt'){|os| os.write("文件重复:#{export.filename}") }
next next
end
end end
end end
end end
zipfile = zipfile_name
else else
zipfile = {:message => "no file"} zipfile = {:message => "no file"}
end end

Loading…
Cancel
Save