Ruby on Rails 并行多记录PDF下载仅生效最后一个的问题
并行触发PDF下载仅最后一个文件被保存问题
问题现象
用户并行点击多条记录的下载链接时,客户端最终只会下载最后触发的那一个PDF文件;服务端控制台日志显示,所有请求对应的PDF都已完成渲染、成功发送了完整响应。
相关实现代码
视图层(View)
<%= link_to(inline_mat_svg :download, stock_item_generate_part_tag_path(stock_item, format: :pdf) %>
控制器层(Controller)
respond_to do |format| format.html format.pdf do pdf = Grover.new("#{request.original_url[0..-5]}?&token=#{@report_user.token}", { format: 'A4', margin: { top: '5px', bottom: '5px', left: '10px', right: '10px' }, viewport: { width: 1280, height: 1024 }, prefer_css_page_size: true, emulate_media: 'screen', # cache: false, timeout: 0, # 超时时间单位为毫秒,0代表无超时限制 launch_args: ['--font-render-hinting=medium'], executable_path: Rails.configuration.chromium_executable_path, wait_until: 'networkidle0' }).to_pdf send_data pdf, type: 'application/pdf', filename: "Test_#{Time.now.strftime('%m-%d-%Y')}.pdf", disposition: :attachment end end
服务端运行日志
Started GET "/stock_items/502393374/part_tag.pdf" for ::1 at 2020-11-09 19:25:10 -0500 Processing by StockItemsController#part_tag as PDF Parameters: {"stock_item_id"=>"502393374"} User Load (1.1ms) SELECT `users`.* FROM `users` WHERE `users`.`email` = 'test.com' LIMIT 1 ↳ app/controllers/concerns/authentication.rb:58:in `in_development_mode?' Profile Load (0.7ms) SELECT `profiles`.* FROM `profiles` WHERE `profiles`.`id` = 185443385 LIMIT 1 ↳ app/models/ability.rb:10:in `block in initialize' Permission Load (1.1ms) SELECT DISTINCT `permissions`.* FROM `permissions` INNER JOIN `permissions_profiles` ON `permissions`.`id` = `permissions_profiles`.`permission_id` WHERE `permissions_profiles`.`profile_id` = 185443385 ↳ app/models/ability.rb:10:in `block in initialize' StockItem Load (0.8ms) SELECT `stock_items`.* FROM `stock_items` WHERE `stock_items`.`id` = 502393374 LIMIT 1 ↳ app/controllers/stock_items_controller.rb:104:in `part_tag' User Load (0.9ms) SELECT `users`.* FROM `users` WHERE `users`.`email` = 'test.com' LIMIT 1 ↳ app/controllers/stock_items_controller.rb:105:in `part_tag' Started GET "/stock_items/747718251/part_tag.pdf" for ::1 at 2020-11-09 19:25:11 -0500 Processing by StockItemsController#part_tag as PDF Parameters: {"stock_item_id"=>"747718251"} User Load (0.8ms) SELECT `users`.* FROM `users` WHERE `users`.`email` = 'abc.com' LIMIT 1 ↳ app/controllers/concerns/authentication.rb:58:in `in_development_mode?' Profile Load (0.7ms) SELECT `profiles`.* FROM `profiles` WHERE `profiles`.`id` = 185443385 LIMIT 1 ↳ app/models/ability.rb:10:in `block in initialize' Permission Load (0.8ms) SELECT DISTINCT `permissions`.* FROM `permissions` INNER JOIN `permissions_profiles` ON `permissions`.`id` = `permissions_profiles`.`permission_id` WHERE `permissions_profiles`.`profile_id` = 185443385 ↳ app/models/ability.rb:10:in `block in initialize' StockItem Load (0.8ms) SELECT `stock_items`.* FROM `stock_items` WHERE `stock_items`.`id` = 747718251 LIMIT 1 ↳ app/controllers/stock_items_controller.rb:104:in `part_tag' User Load (0.8ms) SELECT `users`.* FROM `users` WHERE `users`.`email` = 'test.com' LIMIT 1 ↳ app/controllers/stock_items_controller.rb:105:in `part_tag' Started GET "/stock_items/502393374/part_tag?&token=ccc" for ::1 at 2020-11-09 19:25:12 -0500 Processing by StockItemsController#part_tag as HTML Parameters: {"token"=>"ccc", "stock_item_id"=>"502393374"} User Load (1.1ms) SELECT `users`.* FROM `users` WHERE `users`.`email` = 'abc.com' LIMIT 1 ↳ app/controllers/concerns/authentication.rb:58:in `in_development_mode?' Profile Load (1.0ms) SELECT `profiles`.* FROM `profiles` WHERE `profiles`.`id` = 185443385 LIMIT 1 ↳ app/models/ability.rb:10:in `block in initialize' Permission Load (1.4ms) SELECT DISTINCT `permissions`.* FROM `permissions` INNER JOIN `permissions_profiles` ON `permissions`.`id` = `permissions_profiles`.`permission_id` WHERE `permissions_profiles`.`profile_id` = 185443385 ↳ app/models/ability.rb:10:in `block in initialize' StockItem Load (1.1ms) SELECT `stock_items`.* FROM `stock_items` WHERE `stock_items`.`id` = 502393374 LIMIT 1 ↳ app/controllers/stock_items_controller.rb:104:in `part_tag' User Load (1.1ms) SELECT `users`.* FROM `users` WHERE `users`.`email` = 'test.com' LIMIT 1 ↳ app/controllers/stock_items_controller.rb:105:in `part_tag' Rendering layouts/application.html.erb Rendering stock_items/part_tag.html.erb within layouts/application Product Load (1.6ms) SELECT `products`.* FROM `products` WHERE `products`.`id` = 792659304 LIMIT 1 ↳ app/views/stock_items/_serviceable_part_tag.html.erb:10 Certificate Load (1.0ms) SELECT `certificates`.* FROM `certificates` WHERE `certificates`.`stock_item_id` = 502393374 ↳ app/views/stock_items/_serviceable_part_tag.html.erb:60 Transfer Load (1.7ms) SELECT `transfers`.* FROM `transfers` WHERE `transfers`.`id` = 273907479 LIMIT 1 ↳ app/views/stock_items/_serviceable_part_tag.html.erb:71 User Load (1.0ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 207907133 LIMIT 1 ↳ app/views/stock_items/_serviceable_part_tag.html.erb:71 Rendered stock_items/_serviceable_part_tag.html.erb (Duration: 39.2ms | Allocations: 7894) Rendered stock_items/_unserviceable_part_tag.html.erb (Duration: 0.1ms | Allocations: 35) Rendered stock_items/part_tag.html.erb within layouts/application (Duration: 46.1ms | Allocations: 10204) [Webpacker] Everything's up-to-date. Nothing to do Rendered layouts/_nav_bar_common_option.html.erb (Duration: 14.0ms | Allocations: 281) Rendered layouts/_header.html.erb (Duration: 32.5ms | Allocations: 443) Rendered layouts/_nav_bar_common_option.html.erb (Duration: 0.6ms | Allocations: 276) Rendered layouts/_focused_object_header.html.erb (Duration: 1.6ms | Allocations: 422) Rendered layouts/_footer.html.erb (Duration: 0.2ms | Allocations: 51) Rendered layouts/application.html.erb (Duration: 407.8ms | Allocations: 142784) Completed 200 OK in 458ms (Views: 405.1ms | ActiveRecord: 11.1ms | Allocations: 151063) Started GET "/stock_items/747718251/part_tag?&token=cccc" for ::1 at 2020-11-09 19:25:14 -0500 Processing by StockItemsController#part_tag as HTML Parameters: {"token"=>"ccc", "stock_item_id"=>"747718251"} User Load (1.1ms) SELECT `users`.* FROM `users` WHERE `users`.`email` = 'test.com' LIMIT 1 ↳ app/controllers/concerns/authentication.rb:58:in `in_development_mode?' Profile Load (0.7ms) SELECT `profiles`.* FROM `profiles` WHERE `profiles`.`id` = 185443385 LIMIT 1 ↳ app/models/ability.rb:10:in `block in initialize' Permission Load (1.0ms) SELECT DISTINCT `permissions`.* FROM `permissions` INNER JOIN `permissions_profiles` ON `permissions`.`id` = `permissions_profiles`.`permission_id` WHERE `permissions_profiles`.`profile_id` = 185443385 ↳ app/models/ability.rb:10:in `block in initialize' StockItem Load (0.9ms) SELECT `stock_items`.* FROM `stock_items` WHERE `stock_items`.`id` = 747718251 LIMIT 1 ↳ app/controllers/stock_items_controller.rb:104:in `part_tag' User Load (1.4ms) SELECT `users`.* FROM `users` WHERE `users`.`email` = 'test.com' LIMIT 1 ↳ app/controllers/stock_items_controller.rb:105:in `part_tag' Rendering layouts/application.html.erb Rendering stock_items/part_tag.html.erb within layouts/application Product Load (1.0ms) SELECT `products`.* FROM `products` WHERE `products`.`id` = 792659304 LIMIT 1 ↳ app/views/stock_items/_serviceable_part_tag.html.erb:10 Certificate Load (1.0ms) SELECT `certificates`.* FROM `certificates` WHERE `certificates`.`stock_item_id` = 747718251 ↳ app/views/stock_items/_serviceable_part_tag.html.erb:60 Transfer Load (0.7ms) SELECT `transfers`.* FROM `transfers` WHERE `transfers`.`id` = 1026987850 LIMIT 1 ↳ app/views/stock_items/_serviceable_part_tag.html.erb:71 User Load (1.1ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 207907133 LIMIT 1 ↳ app/views/stock_items/_serviceable_part_tag.html.erb:71 Rendered stock_items/_serviceable_part_tag.html.erb (Duration: 22.2ms | Allocations: 7229) Rendered stock_items/_unserviceable_part_tag.html.erb (Duration: 0.1ms | Allocations: 35) Rendered stock_items/part_tag.html.erb within layouts/application (Duration: 30.8ms | Allocations: 10908) [Webpacker] Everything's up-to-date. Nothing to do Rendered layouts/_nav_bar_common_option.html.erb (Duration: 0.3ms | Allocations: 281) Rendered layouts/_header.html.erb (Duration: 0.9ms | Allocations: 479) Rendered layouts/_nav_bar_common_option.html.erb (Duration: 0.3ms | Allocations: 276) Rendered layouts/_focused_object_header.html.erb (Duration: 0.6ms | Allocations: 422) Rendered layouts/_footer.html.erb (Duration: 0.1ms | Allocations: 51) Rendered layouts/application.html.erb (Duration: 283.9ms | Allocations: 136084) Completed 200 OK in 328ms (Views: 281.3ms | ActiveRecord: 8.9ms | Allocations: 144078) Rendering text template Rendered text template (Duration: 0.0ms | Allocations: 1) Sent data Serviceable_JBAEA00969_11-09-2020.pdf (2.1ms) Completed 200 OK in 6688ms (Views: 1.8ms | ActiveRecord: 4.7ms | Allocations: 2706188) Rendering text template Rendered text template (Duration: 0.0ms | Allocations: 1) Sent data Serviceable_JBAEA00967_11-09-2020.pdf (1.3ms) Completed 200 OK in 6596ms (Views: 1.1ms | ActiveRecord: 3.9ms | Allocations: 2582540)
问题根因
- 同名文件覆盖:控制器给所有PDF指定的文件名仅按日期格式化,同一秒内触发的多个请求生成的文件名完全一致,浏览器开启自动下载时会直接用后一个文件覆盖前一个,不会弹出重名确认提示。
- 浏览器请求队列拦截:多个同源的PDF附件请求几乎同时发起,且Grover通过Chromium渲染的流程导致单请求耗时超过6秒,长时间挂起的请求会被浏览器判定为同一路由的重复响应,自动中断前面的请求,只保留最后到达的响应触发下载。
修复方案
- 调整文件命名规则:把泛用的测试文件名替换为绑定当前记录唯一标识的文件名,从根源上避免重名覆盖。示例代码:
send_data pdf, type: 'application/pdf', filename: "PartTag_#{@stock_item.unique_code}_#{Time.now.strftime('%m-%d-%Y_%H%M%S')}.pdf", disposition: :attachment
- 给下载链接增加唯一区分参数:在生成的下载URL后加随机时间戳,让浏览器把每个请求识别为独立资源,避免队列去重。示例代码:
<%= link_to(inline_mat_svg :download, stock_item_generate_part_tag_path(stock_item, format: :pdf, t: Time.now.to_i) %>
- 优化下载触发逻辑:将并行同步的点击下载调整为间隔100ms的逐次触发,或通过创建隐藏iframe的方式发起下载,降低浏览器对同站连续附件请求的拦截概率。
内容的提问来源于stack exchange,提问作者Veda
相关产品推荐
相关产品推荐

