简体   繁体   English

如何在Rails 4.2中运行生产中的延迟作业而不运行rake jobs命令?

[英]How to run delayed jobs in production in Rails 4.2 without running rake jobs command?

In development mode, we use rake jobs:work . 在开发模式下,我们使用rake jobs:work In the same way, inorder to test in the production mode, we use RAILS_ENV=production rake jobs:work . 同样,为了在生产模式下进行测试,我们使用RAILS_ENV=production rake jobs:work As entire my application is on Apache Nginx server, is there any option like any gem / code that runs background and how it is used to run the jobs without running this command? 由于我的整个应用程序都在Apache Nginx服务器上,是否有任何选项(例如运行后台的gem /代码)以及如何在不运行此命令的情况下将其用于运行作业?

Delayed job is great if you don't have Redis, if you are already using Redis I would recommend Sidekiq over delayed job. 如果您没有Redis,那么延迟的工作就是很棒的选择,如果您已经在使用Redis,我建议您将Sidekiq推荐给延迟的工作。 The main difference is delayed job is an SQL based job worker and Sidekiq uses Redis. 主要区别是延迟的工作是基于SQL的工作工作者,而Sidekiq使用Redis。

Check out the Sidekiq: Getting Started guide for more information about using Sidekiq. 有关使用Sidekiq的更多信息,请查看《 Sidekiq:入门指南》


Delayed also comes with a script to run jobs in the background. 延迟还附带了在后台运行作业的脚本。

From the README: Running Jobs 自述文件:运行作业

script/delayed_job can be used to manage a background process which will start working off jobs. script/delayed_job可用于管理将开始处理工作的后台进程。

To do so, add gem "daemons" to your Gemfile and make sure you've run rails generate delayed_job . 为此,将gem "daemons"添加到您的Gemfile ,并确保已运行rails generate delayed_job

You can then do the following: 然后,您可以执行以下操作:

RAILS_ENV=production script/delayed_job start
RAILS_ENV=production script/delayed_job stop

# Runs two workers in separate processes.
RAILS_ENV=production script/delayed_job -n 2 start
RAILS_ENV=production script/delayed_job stop

# Set the --queue or --queues option to work from a particular queue.
RAILS_ENV=production script/delayed_job --queue=tracking start
RAILS_ENV=production script/delayed_job --queues=mailers,tasks start

# Use the --pool option to specify a worker pool. You can use this option multiple times to start different numbers of workers for different queues.
# The following command will start 1 worker for the tracking queue,
# 2 workers for the mailers and tasks queues, and 2 workers for any jobs:
RAILS_ENV=production script/delayed_job --pool=tracking --pool=mailers,tasks:2 --pool=*:2 start

# Runs all available jobs and then exits
RAILS_ENV=production script/delayed_job start --exit-on-complete
# or to run in the foreground
RAILS_ENV=production script/delayed_job run --exit-on-complete

Rails 4: replace script/delayed_job with bin/delayed_job Rails 4: 用bin / delayed_job替换script / delayed_job

Workers can be running on any computer, as long as they have access to the database and their clock is in sync. 只要工作人员有权访问数据库并且时钟同步,它们就可以在任何计算机上运行。 Keep in mind that each worker will check the database at least every 5 seconds. 请记住,每个工作人员将至少每5秒检查一次数据库。

You can also invoke rake jobs:work which will start working off jobs. 您还可以调用rake jobs:work ,它将开始处理工作。 You can cancel the rake task with CTRL-C . 您可以使用CTRL-C取消耙任务。

If you want to just run all available jobs and exit you can use rake jobs:workoff 如果您只想运行所有可用的作业并退出,则可以使用rake jobs:workoff

Work off queues by setting the QUEUE or QUEUES environment variable. 通过设置QUEUEQUEUES环境变量来处理队列。

QUEUE=tracking rake jobs:work
QUEUES=mailers,tasks rake jobs:work

A couple more things: 还有两件事:

  1. You should always specify which queues to run. 您应该始终指定要运行的队列。
  2. You'll also need to ensure the script is run when your app is deployed. 您还需要确保在部署应用程序时运行脚本。 (You can do this with Capistrano, Mina, Foreman, upstart and many other ways.) (您可以使用Capistrano,Mina,Foreman,暴发户和许多其他方式来执行此操作。)

Miad is correct, Sidekiq is likely what you are looking for, unless you are literally talking about using the delayed job gem, which is another queue adapter. Miad是正确的, Sidekiq可能是您正在寻找的东西,除非您从字面上谈论使用延迟作业 gem,这是另一个队列适配器。 Sidekiq is basically a queue adapter that connects Rails' ActiveJob with Redis . Sidekiq基本上是一个队列适配器,将Rails的ActiveJobRedis连接起来。 You can create Jobs with ActiveJob and kick them off by calling the perform method from your Job class. 您可以使用ActiveJob创建Jobs,并通过在Job类中调用perform方法来启动它们。 This will queue them in a Sidekiq queue, pass them to redis, and they will be performed asynchronously. 这会将它们排在Sidekiq队列中,将它们传递给Redis,然后将它们异步执行。 Your code might look something like this: in app/jobs/your_job.rb 您的代码可能如下所示:在app/jobs/your_job.rb

class YourJob < ActiveJob::Base
  #specify the name of your queue
  queue_as :the_queue

  # you must define perform, this is where the async magic happens
  def perform(something)
   do_stuff_to(something)
  end
end

in app/models/place_where_job_is_kicked_off.rb app/models/place_where_job_is_kicked_off.rb

class PlaceWhereJobIsKickedOff

  def do_the_jobs
    Something.all.each do |something|
      # enqueue your jobs to be performed as soon as the queueing system is free. The queue size is set in your sidekiq.yml
      # each job will be enqueued and run asynchronously, so watch out for race conditions.
      YourJob.perfom_later(something)
    end
  end
end

in app/config/enviroments/production.rb app/config/enviroments/production.rb

Rails.application.configure do
 #other production configs...

 #set the ActiveJob queue adapter to sidekiq
 config.active_job.queue_adapter = :sidekiq

 #other production configs...
end

in app/config/sidekiq.yml app/config/sidekiq.yml

:verbose: true
:pidfile: tmp/pids/sidekiq.pid
:logfile: log/sidekiq.log
# 5 jobs can run asynchronously simultaneously
:concurrency: 5
:queues:
    # queue names go here [name, size]
  - [the_queue, 5]

Make sure that your have Redis installed and running on your production server, and sidekiq is running . 确保已在生产服务器上安装并运行Redis,并且sidekiq正在运行 After adding the sidekiq gem to gemfile, and bundle installing, run: 将sidekiq gem添加到gemfile中并捆绑安装后,运行:

sidekiq -C path/to/sidekiq.yml -d (-e environment)

this will start sidekiq as daemon process. 这将启动sidekiq作为守护进程。

I think what you are looking for is sidekiq gem. 我认为您正在寻找的是sidekiq宝石。 it is used in order to run jobs asynchronously. 它用于异步运行作业。 http://sidekiq.org http://sidekiq.org

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM