© Stefan Wintermeyer 2018
Stefan WintermeyerLearn Rails 5.2https://doi.org/10.1007/978-1-4842-3489-1_10

10. Active Job

Stefan Wintermeyer1 
(1)
Bochum, Germany
 

Sometimes a specific piece of code takes a long time to run but doesn’t need to run right away. An example is sending an e-mail after creating an order at the end of an online shopping workflow. It can take a long time to send an e-mail, but you don’t want your user to wait for that to happen within the controller. It makes more sense to use a queueing mechanism for these tasks.

Active Job provides such a queueing system. You can create jobs that are processed asynchronously by the active job.

Create a New Job

The quickest way to create a new job is to use the job generator. Let’s create an example job that waits for ten seconds and then logs an info message, as shown here:

$ rails new shop
  [...]
$ cd shop
$ rails db:migrate
$ rails generate job example
Running via Spring preloader in process 5301
      invoke  test_unit
      create    test/jobs/example_job_test.rb
      create  app/jobs/example_job.rb
$

All jobs are created in the app/jobs directory. Please change the app/jobs/example_job.rb file accordingly, as shown in Listing 10-1.

class ExampleJob < ApplicationJob
  queue_as :default
  def perform(*args)
    sleep 10
    logger.info "Just waited 10 seconds."
  end
end
Listing 10-1

app/jobs/example_job.rb

You can test the job in your console with ExampleJob.perform_later, which creates it.

$ rails console
Running via Spring preloader in process 98485
Loading development environment (Rails 5.2.0)
>> ExampleJob.perform_later
Enqueued ExampleJob (Job ID: 21526c3c-7839-49e7-975e-2a176a07dbc4) to Async(default)
Performing ExampleJob (Job ID: 21526c3c-7839-49e7-975e-2a176a07dbc4) from Async(default)
=> #<ExampleJob:0x007fceee71f498 @arguments=[], @job_id="21526c3c-7839-49e7-975e-2a176a07dbc4", @queue_name="default", @priority=nil, @executions=0, @provider_job_id="4c814d91-45d1-4c3e-a57a-3bfd08c1c56f">

Now you have to wait ten seconds to see the following output in the console:

Just waited 10 seconds.
Performed ExampleJob (Job ID: bb6e9781-8ffb-4bf2-8dfc-8ac983ed8bf6)
from Async(default) in 10012.97ms
?> exit

../images/460214_1_En_10_Chapter/460214_1_En_10_Figa_HTML.gif The file log/development.log contains the logging output.

You’ll find a more concrete example of using jobs in Chapter 11 where an e-mail gets sent.

Set the Time for Future Execution

The set method provides two arguments that can be used to set the execution of a job in the future.
  • wait

    ExampleJob.set(wait: 1.hour).perform_later
  • wait_until

    ExampleJob.set(wait_until: Date.tomorrow.noon).perform_later

Configure the Job Server Back End

The page http://api.rubyonrails.org/classes/ActiveJob/QueueAdapters.html lists all the available back ends. To use one of them, you have to install the needed gem. Listing 10-2 shows an example of using the popular Sidekiq . To use the gem, you have to add it to the Gemfile and run a bundle install command afterward.

[...]
gem 'sidekiq'
$ bundle install
Listing 10-2

Gemfile

In config/application.rb, you can configure the use of it, as shown in Listing 10-3.

require_relative 'boot'
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module Shop
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 5.2
    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration should go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded.
    # Sidekiq Configuration
    config.active_job.queue_adapter = :sidekiq
  end
end
Listing 10-3

config/application.rb

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.168.46