delayed_job 的未定义局部变量或方法

undefined local variable or method for delayed_job

我正在尝试修改 App 模型上的现有回调以通过延迟作业执行。删除应用程序时出现错误 undefined local variable or method app_name for main:Object

app/models/app.rb

# == Schema Information
#
# Table name: apps
#
#  id           :integer          not null, primary key
#  name         :string(255)
#  created_at   :datetime
#  updated_at   :datetime
#  app_type     :string(255)
#  package_name :string(255)
#  icon         :string(255)
#

# app/models/app.rb
class App < ActiveRecord::Base
  has_many :versions, dependent: :destroy

app/models/version.rb

class Version < ActiveRecord::Base
  DEFAULT_ICON_URL = 'placeholder_med@2x.png'
  belongs_to :app
  delegate :name, :id, :users, :app_type, to: :app, prefix: true
  after_create :notify_subscribers
  before_destroy :remove_stored_files

  scope :since, ->(time) { where('created_at > ?', time) }

  def updated_or_created_at
    updated_at || created_at
  end

  def display_icon
    if icon_url.blank? || icon_url.match(/default.png/)
      DEFAULT_ICON_URL
    else
      icon_url
    end
  end

  def main?
    version_type == 'main'
  end

  def release_notes?
    !release_notes_url.blank?
  end

  private

  def notify_subscribers
    AppMailer.notify_new_build(id)
  end

  def remove_stored_files
    Delayed::Job.enqueue(DeleteAppFilesJob.new(app_name, version_number, build_number), priority: 1, run_at: 5.minute.from_now)
  end
end

app/jobs/delete_app_files_job.rb

class DeleteAppFilesJob < Struct.new(app_name, version_number, build_number)
  def perform
    remove_stored_files(app_name, version_number, build_number)
  end

  protected

  def remove_stored_files(app_name, version_number, build_number)
    S3_BUCKET.objects.select { |obj| obj.key.match(%r{(ios|android)/#{app_name}/#{version_number}/#{build_number}}) }.each do |obj|
      puts "Deleting #{obj.key}"
      obj.delete
    end
  end
end

要创建一个匿名的 Struct(当像这样进行子类化时通常需要这样做),您将 Symbol 参数传递给 Struct.new:

class DeleteAppFilesJob < Struct.new(:app_name, :version_number, :build_number)

您正在尝试传递不存在的变量,因此出现 "undefined local variable or method" 错误。