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
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

Loading…
Cancel
Save