我怎样才能把我所有的“perform_later的进入” perform_now本地所?

问题描述 投票:5回答:3

我正在perform_later工作调用的产物。这适用于我们生产的产品,因为我们有一系列谁运行的所有作业的工人。

但是,当我使用本地应用程序,我没有获得这些工人,我想改变所有的perform_laters到perform_nows只有当我在本地使用的应用程序。

什么是做到这一点的最好方法是什么?我有一个想法是添加在我env文件的东西,会增加一个变量来使所有perform_laters到perform_nows - 但我不知道一个标志或变量一样,将是什么样子。

想法?

ruby-on-rails environment-variables jobs
3个回答
5
投票

在您的应用程序,你可以有:

/没有_app/config/initializers/Jobs_initializer.日本

module JobsExt
  extend ActiveSupport::Concern

  class_methods do
    def perform_later(*args)
      puts "I'm on #{Rails.env} envirnoment. So, I'll run right now"
      perform_now(*args)
    end
  end
end

if Rails.env != "production"
  puts "including mixin"
  ActiveJob::Base.send(:include, JobsExt)
end

这mixin将被列入仅testdevelopment环境。

然后,如果你在工作:

/没有_app/app/Jobs/没有_job.日本

class MyJob < ActiveJob::Base
  def perform(param)
    "I'm a #{param}!"
  end
end

您可以执行:

MyJob.perform_later("job")

并获得:

#=> "I'm a job!"

相反,工作实例:

#<MyJob:0x007ff197cd1938 @arguments=["job"], @job_id="aab4dbfb-3d57-4f6d-8994-065a178dc09a", @queue_name="default">

请记住:这样做,你的所有工作将马上在测试和开发环境中执行。如果要启用此功能单一的工作,你将需要在只有工作的JobsExt混入。


8
投票

干净的解决方案是在开发环境中change the adapter

在你/config/environments/development.rb你需要添加:

Rails.application.configure do
  config.active_job.queue_adapter = :inline
end

“当与内联适配器进行排队作业的作业将立即执行。”


2
投票

我们解决了这个通过调用中间方法,然后叫perform_later或者根据Rails的配置perform_now:

def self.perform(*args)
  if Rails.application.config.perform_later
    perform_later(*args)
  else
    perform_now(*args)
  end
end

而且只需相应地更新环境CONFIGS

© www.soinside.com 2019 - 2024. All rights reserved.