0

我试图在控制台中调用作业,但总是出错。

遵循以下文档:

创建了以下文件:(lib/jobs/archive_survey_jobs.rb)

module Jobs
  class ArchiveSurveysJob < ActiveJob::Base
    @queue = :setup

    def perform(survey_type)
      if SurveyTypes.all_classes.map(&:to_s).include?(survey_type)

        surveys = Survey.where(something: 'stuff')\
                        .where.not(something: 'stuff',
                                   something: 'stuff')

        surveys.each do |survey|
          do_something
        end
      end
    end
  end
end

我知道我可以做类似的事情Resque.enqueue(ArchiveSurveysJob, 'string_here')

如何在控制台中调用它?如果我尝试: Jobs::ArchiveSurveysJob.create(survey_type: 'string_here'),当我检查 Resque 状态时,它会导致错误:`任务因错误而失败:

未定义的局部变量或方法 `args' for #

如果我试试这个:

Jobs::ArchiveSurveysJob.perform(`string_here`)

或者:

Jobs::ArchiveSurveysJob.perform_now('string_here')

我得到:

ArgumentError:参数数量错误(给定 0,预期 1..2)

如果我遗漏了一些文档或者我做错了什么,请告诉我。

4

2 回答 2

1

在您的示例中,perform是一个实例方法。您正在调用perform可能没有任何参数的类级别。我不具体了解您的 resque 版本,但是您可以像这样在至少一个版本的控制台中将作业排入队列:Resque.enqueue(Jobs::ArchiveSurveysJob)

ps,确保您的 resque 工作人员正在运行。在开发环境中,我通常会在线运行 resque:

# config/initializers/resque.rb
Resque.inline = true if Rails.env.dev? || Rails.env.test?
于 2021-08-26T18:53:11.367 回答
0

我如何做到这一点是通过创建一个 Rake 任务并在那里调用 Job。

于 2019-03-11T13:37:11.200 回答