在自定义延迟作业中创建(虾)PDF 并将其上传到 S3



使用:Rails 4.2,虾,回形针,通过ActiveJobs延迟作业,Heroku。

我有一个非常大的 PDF,需要在后台处理。 在自定义任务中,我想创建它,将其上传到 S3,然后在准备就绪时通过电子邮件向用户发送 URL。 我通过PdfUpload模型来促进这一点。

我的方法/代码有什么问题吗? 我使用 File.open() 如我发现的示例中所述,但这似乎是我错误的根源(类型错误:没有将 FlightsWithGradesReport 隐式转换为字符串)。

  class PdfUpload < ActiveRecord::Base
    has_attached_file :report,
      path: "schools/:school/pdf_reports/:id_:style.:extension"
  end

/pages_controller.rb

  def flights_with_grades_report
    flash[:success] = "The report you requested is being generated.  An email will be sent to '#{ current_user.email }' when it is ready."
    GenerateFlightsWithGradesReportJob.perform_later(current_user.id, @rating.id)
    redirect_to :back
    authorize @rating, :reports?
  end

/工作

class GenerateFlightsWithGradesReportJob < ActiveJob::Base
  queue_as :generate_pdf
  def perform(recipient_user_id, rating_id)
    rating = Rating.find(rating_id)
    pdf = FlightsWithGradesReport.new( rating.id )
    pdf_upload = PdfUpload.new
    pdf_upload.report = File.open( pdf )
    pdf_upload.report_processing = true
    pdf_upload.report_file_name = "report.pdf"
    pdf_upload.report_content_type = "application/pdf"
    pdf_upload.save!
    PdfMailer.pdf_ready(recipient_user_id, pdf_upload.id)
  end
end

这会导致错误:

 TypeError: no implicit conversion of FlightsWithGradesReport into String

更改此设置:

pdf_upload.report = File.open( pdf )

对此:

pdf_upload.report = StringIO.new(pdf.render)

解决了我的问题。

最新更新