在 Rails 4.2 中,我在哪里将作业排入 ActiveJob?

Where do I enqueue jobs into ActiveJob in Rails 4.2?

Rails我是初学者。我正在尝试遵循此示例:

http://ryanselk.com/2014/09/25/using-background-jobs-in-rails-42-with-active-job/

它说:

"Jobs can be added to the job queue from anywhere. We can add a job to the queue by: ResizeImage.perform_later 'http://example.com/ex.png' "

[更新]对不起,我很笨。我想到了这个任务:

namespace :simple do

  # call from command line:
  # rake simple:resize_images 

  desc "Resize images"
  task resize_images: :environment do

    Dir.foreach('storage') do |next_image|
      puts next_image
      next if next_image == '.' or next_image == '..'
      ResizeImage.perform_later next_image
    end

  end

end

但现在我这样做了:

rake simple:resize_images 

我得到:

zacek2_phpP9JGif.jpg
rake aborted!
NameError: uninitialized constant ResizeImage

我试过:

require ResizeImage

但这并没有解决问题。

恐怕我不明白 Rails 中的加载是如何工作的。如何加载 ResizeImage?

Do I set it up as a cron job?

没有

来自the rails guides

Active Job is a framework for declaring jobs and making them run on a variety of queueing backends.

Active Job 是排队后端的接口,例如 sidekiq, delayed_job or resque。它只是一种编写后台作业的方式,您不必关心将使用哪个排队后端。

How do I start ActiveJob?

所以 ActiveJob 本身不会 运行 后台作业。您仍然缺少后端之一。假设您决定使用 delayed_job: Get it installed 然后通过以下方式启动它:

script/delayed_job start

I don't understand where "anywhere" is.

这意味着您可以在代码的任何地方编写如下内容:

user.rb

def send_registration_email
  UserRegistraionMailJob.perform_later self
end