Active Job: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 20: Line 20:
We will introduce how to creating a job and and how to add the job into a queue. <ref name= "Active-Job-Basics">Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]</ref> <br>
We will introduce how to creating a job and and how to add the job into a queue. <ref name= "Active-Job-Basics">Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]</ref> <br>
=== Download ===
=== Download ===
Active Job can be installed with latest version using RubyGems:
With RubyGems you can install the Active Job into your computer:
<pre>
<pre>
$ gem install activejob
$ gem install activejob
Line 28: Line 28:


=== Create a Job ===
=== Create a Job ===
In Active Job, a process which is inserted in a queue and waiting for carry out is called “Job”. It’s possible to generate a Job using the Generator provided by Rails. The following will create a job in app/jobs, with the name “update_wiki”.<br>
In Active Job, a process which is inserted in a queue and waiting for carry out is called “Job”. It’s possible to generate a Job using the Generator provided by Rails. You can create a job in app/jobs, with the name “update_wiki”, by doing the following.<br>
<pre>
<pre>
$ bin/rails generate job update_wiki
$ bin/rails generate job update_wiki
Line 35: Line 35:
   create  app/jobs/update_wiki_job.rb
   create  app/jobs/update_wiki_job.rb
</pre>
</pre>
You can also create a job that will run on a specific queue:<br>
Active Job provides the ability to run your Job on a specific queue by create a job as following:<br>
<pre>
<pre>
$ bin/rails generate job update_wiki --queue urgent
$ bin/rails generate job update_wiki --queue urgent
</pre>
</pre>
You could create your own file inside of app/jobs, without using a generator. In Rails 4.2 an ActiveJob inherits from ActiveJob::Base. In Rails 5.0 this behavior has changed to now inherit from ApplicationJob. When upgrading from Rails 4.2 to Rails 5.0 you need to create an application_job.rb file in app/jobs/ and add the following content:<ref> Rails Guides [http://edgeguides.rubyonrails.org/upgrading_ruby_on_rails.html#upgrading-from-rails-4-1-to-rails-4-2 “A Guide for Upgrading Ruby on Rails” 2015] </ref>
Files inside of app/jobs can be created manually, instead of using a generator. In Rails 4.2 an ActiveJob class inherits from ActiveJob::Base. In Rails 5.0, it has changed to now inherit from ApplicationJob. When upgrading from Rails 4.2 to Rails 5.0, an application_job.rb file is needed to be created in app/jobs/ and add the following content:<ref> Rails Guides [http://edgeguides.rubyonrails.org/upgrading_ruby_on_rails.html#upgrading-from-rails-4-1-to-rails-4-2 “A Guide for Upgrading Ruby on Rails” 2015] </ref>
<pre>
<pre>
class ApplicationJob < ActiveJob::Base
class ApplicationJob < ActiveJob::Base
Line 55: Line 55:
end
end
</pre>
</pre>
There is a perform method which is called when the job was first enqueued. You can define perform with as many arguments as you want. <br>
There is a perform method to be called when the job was first enqueued. <br>


=== Adding a Job to the queue ===
=== Adding a Job to the queue ===
Line 66: Line 66:
UpdateWikiJob.set(wait_until: Date.tomorrow.noon).perform_later(wiki)
UpdateWikiJob.set(wait_until: Date.tomorrow.noon).perform_later(wiki)
</pre>
</pre>
If you want your Job be performed a week from now, some queueing backends allow you to set a delay time.
If you want your Job be performed a week from now, most of the queueing backends ( Sidekiq
, Delayed Job, etc. ) allow you to set a delay time.
<pre>
<pre>
UpdateWikiJob.set(wait: 1.week).perform_later(wiki)
UpdateWikiJob.set(wait: 1.week).perform_later(wiki)
</pre>
</pre>
=== Execution of Job ===
=== Execution of Job ===
Active Job has built-in adapters for multiple queueing backends ([https://github.com/mperham/sidekiq/wiki Sidekiq], [https://github.com/resque/resque Resque], [https://github.com/collectiveidea/delayed_job Delayed Job] and [http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters.html others]).<ref name= "Active-Job-Basics">Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]</ref> If no adapter is set, the job is immediately executed.<br>
Active Job provides adapters for multiple queueing backends ([https://github.com/mperham/sidekiq/wiki Sidekiq], [https://github.com/resque/resque Resque], [https://github.com/collectiveidea/delayed_job Delayed Job] and [http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters.html others]).<ref name= "Active-Job-Basics">Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]</ref> Without setting any adapter, the job would be performed immediately.<br>
You can set your queueing backend at: /config/application.rb, in this example we use the Sidekiq.
Queueing backend can be set at: /config/application.rb, in this example we use the Sidekiq.
<pre>
<pre>
module YourApp
module YourApp
Line 90: Line 91:
end
end
</pre>
</pre>
You can prefix the queue name for all your jobs using config.active_job.queue_name_prefixin application.rb:
Queue name can be prefixed for all jobs using config.active_job.queue_name_prefixin application.rb:
<pre>
<pre>
# config/application.rb
# config/application.rb
Line 99: Line 100:
end
end
   
   
# app/jobs/guests_cleanup.rb
# app/jobs/update_wiki_job.rb
class UpdateWikiJob < ActiveJob::Base
class UpdateWikiJob < ActiveJob::Base
   queue_as :low_priority
   queue_as :low_priority
Line 111: Line 112:




The default queue name prefix delimiter is '_'. This can be changed by setting config.active_job.queue_name_delimiter in application.rb:
You can control a job to run on a queue you like, by passing a :queue option to #set :
<pre>
# config/application.rb
module YourApp
  class Application < Rails::Application
    config.active_job.queue_name_prefix = Rails.env
    config.active_job.queue_name_delimiter = '.'
  end
end
# app/jobs/guests_cleanup.rb
class UpdateWikiJob < ActiveJob::Base
  queue_as :low_priority
  #....
end
# Now your job will run on queue production.low_priority on your
# production environment and on staging.low_priority on your staging
# environment
</pre>
If you want more control on what queue a job will be run you can pass a :queue option to #set:
<pre>
<pre>
MyJob.set(queue: :another_queue).perform_later(record)
MyJob.set(queue: :another_queue).perform_later(record)
</pre>
To control the queue from the job level you can pass a block to #queue_as. The block will be executed in the job context (so you can access self.arguments) and you must return the queue name:
<pre>
class ProcessVideoJob < ActiveJob::Base
  queue_as do
    video = self.arguments.first
    if video.owner.premium?
      :premium_videojobs
    else
      :videojobs
    end
  end
  def perform(video)
    # do process video
  end
end
ProcessVideoJob.perform_later(Video.last)
</pre>
</pre>



Revision as of 19:46, 2 February 2016

Introduction

Active Job is a framework help developers writing codes and run them on the background automatically under different scenarios. Jobs can vary from schedule newsletter, follow-up emails to database housekeeping. It’s a interface that adapts different queueing backends like Backburner[1], Delayed Job[2], Qu[3] and so on. <ref>http://guides.rubyonrails.org/active_job_basics.html</ref> Overall, Active Job is a interface which you can work with common queues.

Active Job adapters<ref>http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters.html</ref>

How to use <ref>http://guides.rubyonrails.org/active_job_basics.html</ref>

We will introduce how to creating a job and and how to add the job into a queue. <ref name= "Active-Job-Basics">Rails Guides “Active Job Basics” 2014</ref>

Download

With RubyGems you can install the Active Job into your computer:

$ gem install activejob

Souce code of Active Job available on GitHub, as part of Rails.<ref> GitHub “Active Job -- Make work happen later” 2015 </ref>

Create a Job

In Active Job, a process which is inserted in a queue and waiting for carry out is called “Job”. It’s possible to generate a Job using the Generator provided by Rails. You can create a job in app/jobs, with the name “update_wiki”, by doing the following.

$ bin/rails generate job update_wiki
  invoke  test_unit
  create  test/jobs/update_wiki_job_test.rb
  create  app/jobs/update_wiki_job.rb

Active Job provides the ability to run your Job on a specific queue by create a job as following:

$ bin/rails generate job update_wiki --queue urgent

Files inside of app/jobs can be created manually, instead of using a generator. In Rails 4.2 an ActiveJob class inherits from ActiveJob::Base. In Rails 5.0, it has changed to now inherit from ApplicationJob. When upgrading from Rails 4.2 to Rails 5.0, an application_job.rb file is needed to be created in app/jobs/ and add the following content:<ref> Rails Guides “A Guide for Upgrading Ruby on Rails” 2015 </ref>

class ApplicationJob < ActiveJob::Base
end

In Rails 4.2 a Job class looks like:

class UpdateWikiJob < ActiveJob::Base
  queue_as :default
 
  def perform(*wiki)
   # Do something later
  wiki.update_contents
  end
end

There is a perform method to be called when the job was first enqueued.

Adding a Job to the queue

If you wish your Job be processed as soon as the queuing system is free, you can enqueue a Job like:

UpdateWikiJob.perform_later wiki

Or you can add a Job be performed tomorrow at noon:

UpdateWikiJob.set(wait_until: Date.tomorrow.noon).perform_later(wiki)

If you want your Job be performed a week from now, most of the queueing backends ( Sidekiq , Delayed Job, etc. ) allow you to set a delay time.

UpdateWikiJob.set(wait: 1.week).perform_later(wiki)

Execution of Job

Active Job provides adapters for multiple queueing backends (Sidekiq, Resque, Delayed Job and others).<ref name= "Active-Job-Basics">Rails Guides “Active Job Basics” 2014</ref> Without setting any adapter, the job would be performed immediately.
Queueing backend can be set at: /config/application.rb, in this example we use the Sidekiq.

module YourApp
  class Application < Rails::Application
    # Be sure to have the adapter's gem in your Gemfile and follow
    # the adapter's specific installation and deployment instructions.
    config.active_job.queue_adapter = :sidekiq
  end
end

Play with Queues

Active Job allows to schedule the job to be processed on a specific queue, this became helpful as common adapters support multiple queues.

class UpdateWikiJob < ActiveJob::Base
  queue_as :low_priority
  #....
end

Queue name can be prefixed for all jobs using config.active_job.queue_name_prefixin application.rb:

# config/application.rb
module YourApp
  class Application < Rails::Application
    config.active_job.queue_name_prefix = Rails.env
  end
end
 
# app/jobs/update_wiki_job.rb
class UpdateWikiJob < ActiveJob::Base
  queue_as :low_priority
  #....
end
 
# Now your job will run on queue production_low_priority on your
# production environment and on staging_low_priority on your staging
# environment


You can control a job to run on a queue you like, by passing a :queue option to #set :

MyJob.set(queue: :another_queue).perform_later(record)

All the code snippets are referred from the source<ref name= "Active-Job-Basics">Rails Guides “Active Job Basics” 2014</ref>

References

<references/>