在Sidekiq任务中访问wicked_pdf的对象元素

时间:2019-01-15 02:37:55

标签: ruby-on-rails-5 sidekiq wicked-pdf

我正在尝试根据WickedPDF中要求的对象向文件添加自定义名称。此操作可以很好地用作控制器操作,但是一旦进入Sidekiq工作程序,它就不会说没有针对Issueable(多态)对象的方法name,该方法以字符串形式返回(这很可能是我所有问题在那里,但我不知道如何解决它。)

相关行:

save_path = Rails.root.join('public', "#{@issueable.name}_checklist.pdf")

Sidekiq控制台输出中上下文中的准确错误:

2019-01-15T02:36:46.194Z 10080 TID-oxwns2nmk GenerateChecklistWorker JID-cefb15a101c80e1c8727b646 INFO: start
"***************[\"/Users/jathayde/.rbenv/versions/2.5.2/bin/wkhtmltopdf\", \"-q\", \"--dpi\", \"144\", \"--margin-top\", \"30\", \"--margin-bottom\", \"20\", \"--margin-left\", \"10\", \"--margin-right\", \"10\", \"--header-spacing\", \"5\", \"--footer-spacing\", \"0\", \"file:////var/folders/1b/9267f2s126q7c_8dztswqps40000gn/T/wicked_pdf20190114-10080-40sij4.html\", \"/var/folders/1b/9267f2s126q7c_8dztswqps40000gn/T/wicked_pdf_generated_file20190114-10080-kekkfo.pdf\"]***************"
2019-01-15T02:36:58.263Z 10080 TID-oxwns2nmk GenerateChecklistWorker JID-cefb15a101c80e1c8727b646 INFO: fail: 12.069 sec
2019-01-15T02:36:58.268Z 10080 TID-oxwns2nmk WARN: {"context":"Job raised exception","job":{"class":"GenerateChecklistWorker","args":["#<Lodge:0x00007fcdb8cc86b0>",["#<Issue:0x00007fcdc267ea70>","#<Issue:0x00007fcdc26c0a88>"],"#<Issue::ActiveRecord_AssociationRelation:0x00007fcdc22e1c28>"],"retry":false,"queue":"default","jid":"cefb15a101c80e1c8727b646","created_at":1547519806.182058,"enqueued_at":1547519806.18223},"jobstr":"{\"class\":\"GenerateChecklistWorker\",\"args\":[\"#<Lodge:0x00007fcdb8cc86b0>\",[\"#<Issue:0x00007fcdc267ea70>\",\"#<Issue:0x00007fcdc26c0a88>\"],\"#<Issue::ActiveRecord_AssociationRelation:0x00007fcdc22e1c28>\"],\"retry\":false,\"queue\":\"default\",\"jid\":\"cefb15a101c80e1c8727b646\",\"created_at\":1547519806.182058,\"enqueued_at\":1547519806.18223}"}
2019-01-15T02:36:58.268Z 10080 TID-oxwns2nmk WARN: NoMethodError: undefined method `name' for "#<Lodge:0x00007fcdb8cc86b0>":String
2019-01-15T02:36:58.268Z 10080 TID-oxwns2nmk WARN: /Users/jathayde/Development/Meticulous/Products/patchvault/app/workers/generate_checklist_worker.rb:30:in `perform'

完整的generate_checklist_worker.rb

class GenerateChecklistWorker
  include Sidekiq::Worker
  sidekiq_options retry: false

  def perform(issueable, issues, event_issues)
    @issueable = issueable

    av = ActionView::Base.new()
    av.view_paths = ActionController::Base.view_paths

    av.class_eval do
      include Rails.application.routes.url_helpers
      include ApplicationHelper
    end

    pdf = av.render template: 'issues/checklist.pdf.erb',
    locals: {:issueable => @issueable, :issues => @issues, :event_issues => @event_issues}

    pdf = WickedPdf.new.pdf_from_string(
      pdf,
      disposition:  'attachment',
      dpi:          '144',
      header:       {html: {template: 'layouts/_checklist_header'}, spacing: 5 },
      footer:       {html: {template: 'layouts/_checklist_footer'}, spacing: 0 },
      margin:       {top: 30, bottom: 20, left: 10, right: 10},
      locals:       {:issueable => @issueable},
      #cover:        ApplicationController.new.render_to_string('issues/checklist_cover.pdf.erb'), locals: {:issueable => issueable}
    )

    save_path = Rails.root.join('public', "#{@issueable.name}_checklist.pdf")
    File.open(save_path, 'wb') do |file|
      file << pdf
    end
  end
end

这是控制器中调用此方法的方法:

def checklist
    @issues = @issueable.issues.kept.non_event_issues.ordered_issues
    @event_issues = @issueable.issues.kept.event_issues.order(issue_number: :asc)
    GenerateChecklistWorker.perform_async(@issueable, @issues, @event_issues)
    redirect_to @issueable
end

1 个答案:

答案 0 :(得分:1)

您不能将复杂的Ruby对象序列化为Sidekiq参数。您需要在工作中查找事物(即,通过调用find)。

https://github.com/mperham/sidekiq/wiki/Best-Practices#1-make-your-job-parameters-small-and-simple