【问题标题】:How to run a resque job in the console?如何在控制台中运行 resque 作业?
【发布时间】:2019-07-31 23:33:38
【问题描述】:

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

遵循以下文档:

创建了以下文件: (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'

如果我试试这个:

Jobs::ArchiveSurveysJob.perform(`string_here`)

或者:

Jobs::ArchiveSurveysJob.perform_now('string_here')

我明白了:

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

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

【问题讨论】:

  • 你为什么把这个类放在一个模块中?您是否尝试过在模块外运行perform_now
  • @GrahamSlick 如果我将其从模块中删除并运行:ArchiveSurveysJob.perform_now(string_here')` 我 ged:uninitialized constant ArchiveSurveysJob
  • Jobs::ArchiveSurveysJob.perform_now('string_here') 应该可以工作。

标签: ruby-on-rails ruby resque resque-scheduler resque-status


【解决方案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?

【讨论】:

【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2013-12-04
    • 2013-01-10
    • 2013-04-19
    • 1970-01-01
    • 1970-01-01
    • 2013-12-05
    • 2016-07-14
    • 2016-04-16
    • 2016-05-29
    相关资源
    最近更新 更多