【问题标题】:Heroku timing out when generating a Json needed for a view in a Ruby on Rails Application在 Ruby on Rails 应用程序中生成视图所需的 Json 时 Heroku 超时
【发布时间】:2019-08-15 17:11:58
【问题描述】:

我的视图需要在控制器中生成 JSON,当扩展更大的信息集时,生成 JSON 需要很长时间。我的问题是 Heroku 在超时之前有 30 秒的加载时间限制,对于更大的数据集,它会超时。

我已经实现了 Redis to go 和 Sidekiq,它帮助我在应用程序上实现了不同的功能。

当有较大的数据集时,显示视图也会分页。不确定我最好的选择是什么。

我正在考虑创建一个中间页面,同时使用 Sidkiq 异步创建 JSON,然后在创建 JSON 时提醒用户,然后允许用户在加载后转到显示视图,这是一个不错的选择,但随后我想我使用 Kaminari Gem 的分页会有问题。

就像我说的,我已经在使用 Sidekiq,所以我认为这将是我的最佳选择。

def show
# @salesforce = true if @account.Name != nil 
@audit_report = audit_report
@measures_updated = MeasuresUpdated.new(audit_report: @audit_report)
@measures_updated = @measures_updated.check_measures_updated
@page_title = "Report based on \"#{@audit_report.name}\""
@context = ShowAuditReportContext.new(
  user: current_user,
  audit_report: @audit_report).audit_report_as_json
@context_measure_selections = @context[:audit_report][:measure_selections]
@context_measure_selections_array = @context[:audit_report][:measure_selections].to_a
@paginatable_array = Kaminari.paginate_array(@context_measure_selections_array).page(params[:page]).per(15)

结束

这是在控制器中 @context 是问题所在,后端中有很多计算放置在不同的类中,需要很长时间才能加载。

我希望用户访问显示页面而不会因服务器限制而超时。

【问题讨论】:

    标签: ruby-on-rails asynchronous heroku pagination sidekiq


    【解决方案1】:

    您可能希望将此长 JSON 文档视为用户正在生成的报告,而不是用户请求的视图。

    对 JSON 文档进行分页会导致 JSON 无效,因为打开和关闭花括号(可能)不匹配。所以,也许用户下载文档更合适。您可以在文档可用的位置 URL 视图中通知用户。

    @filename = "Report#{@audit_report}.json"
    CreateJsonReportJob.perform_async(@filename, @audit_report, current_user)
    
    

    然后您的 Sidekiq 作业会将文件写入该位置

    json = ShowAuditReportContext.new(
      user: current_user,
      audit_report: audit_report
    ).audit_report_as_json
    
    File.open(filename, "w+") do |f|
      f.write(json)
    end
    

    为了更有趣,您可以创建一个用户可以查看和管理的报告表格。您可以使用 Active Storage 来管理附件。

    【讨论】:

    • 注意:因为它们在 Heroku 上,你不能依赖本地文件名,需要使用 S3 或类似的。
    • 抱歉应该提到该视图也可以使用不同的字段进行编辑,然后使用 ajax 更新视图和模型
    猜你喜欢
    • 2011-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-26
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    相关资源
    最近更新 更多