使用ActiveJob,perform_now似乎有不同的结果比perform_later
问题描述:
我使用ActiveJob
与SideKiq
和下面的代码我使用perform_later
比perform_now
时得到不同的结果。使用ActiveJob,perform_now似乎有不同的结果比perform_later
当使用perform_now
我的代码创建指定的文件夹和文件。 当使用perform_later
时,我可以看到代码执行并且不会被引入到Sidekiq的重试队列中,但不会创建文件夹或文件。
如果还有其他事情可以帮助解决问题,请让我知道,因为我可能忽略了它。
应用程序/控制器/顾虑/ pdf_player_reports.rb
module PdfPlayerReports
extend ActiveSupport::Concern
included do
# before_action :set_player
# before_action :set_user_report
end
def director_report_pdf
@players = Player.where(id: params["id"])
html = render_to_string template: "players/director_summary_report.pdf.erb"
CreatePdfJob.perform_later(html)
#CreatePdfJob.perform_now(html)
end
end
应用程序/工作/ create_pdf_job.rb
class CreatePdfJob < ActiveJob::Base
queue_as :high_priority
def perform(*args)
generate_pdf_from_string(args.first)
end
def generate_pdf_from_string(html)
# create pdf from passed in HTML string
pdf = WickedPdf.new.pdf_from_string(html)
# Create the directory for the pdfs that will be gernereated incases where it doesn't already exist
pdf_directory_path = Rails.root.join('public','uploads','pdfs')
unless File.directory? pdf_directory_path
FileUtils.mkdir_p(pdf_directory_path)
end
# Open the specified file and then write the contents to disk
File.open(pdf_directory_path.join('output.pdf'),'wb') do |f|
f << pdf
end
end
end
答
问题(我的理解)似乎是我需要取消我的sidekiq进程并在任何时候更改作业文件上的代码时重新启动它(sidekiq -C config/sidekiq.yml
)。
+0
看看这个:https://github.com/mperham/sidekiq/issues/2450 –
我从我的'config/application.rb'中删除了以下行:'config.active_job.queue_adapter =:sidekiq'和perform_later工作。现在我认为这是因为这只是使它内联运行 - 如果与使用'perform_now'不同,非常相似。这可能是服务器端问题,sidekiq工作人员没有适当的权限来写入文件夹/文件到磁盘? – daveomcd
您的所有进程是否都在单台计算机上运行? (即工作可能是在不同的服务器上创建文件,而不是您正在查看的文件?)也可能是您使用splat(*)传递参数的问题。考虑到当你调用perform_later时,作业的参数被序列化,然后在sidekiq运行时被反序列化。您可能正在调用.first字符串,而不是参数列表。 –
@AdamFinley,同一台机器,还有什么是调用参数传递我的一个字符串参数的正确方法? – daveomcd