Search code examples
ruby-on-railssidekiqruby-on-rails-4.2rails-activejob

Using ActiveJob, perform_now seems to have different results than perform_later


I'm using ActiveJob with SideKiq and with the code below I'm getting differing results when using perform_later than perform_now.

When using perform_now my code creates the specified folder and file. When using perform_later I can see the code executes and doesn't get thrown into the Retry queue of Sidekiq, yet doesn't create the folder or file.

If there is anything else I could produce to help troubleshoot the issue please let me know, as I've likely just overlooked it.

app/controllers/concerns/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

app/jobs/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

Solution

  • The issue (to my understanding) seems to be that I need to cancel my sidekiq process and restart it (sidekiq -C config/sidekiq.yml) anytime I change code on the Job file.