6

How to run delayed jobs in production in Rails 4.2 without running the rake jobs...

 3 years ago
source link: https://www.codesd.com/item/how-to-run-delayed-jobs-in-production-in-rails-4-2-without-running-the-rake-jobs-command.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to run delayed jobs in production in Rails 4.2 without running the rake jobs command?

advertisements

In development mode, we use rake jobs:work. In the same way, inorder to test in the production mode, we use 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?


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. Sidekiq is basically a queue adapter that connects Rails' ActiveJob with Redis. You can create Jobs with ActiveJob and kick them off by calling the perform method from your Job class. This will queue them in a Sidekiq queue, pass them to redis, and they will be performed asynchronously. Your code might look something like this: in 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

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

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

: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. After adding the sidekiq gem to gemfile, and bundle installing, run:

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

this will start sidekiq as daemon process.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK