From 95b79c1ff4d253d1554615baa5a02b0910d5a6dc Mon Sep 17 00:00:00 2001 From: Cristian Bica Date: Wed, 13 Aug 2014 13:10:59 +0300 Subject: [ci skip] First version of the Active Job guide --- guides/source/active_job_basics.md | 323 +++++++++++++++++++++++++++++++++++++ 1 file changed, 323 insertions(+) create mode 100644 guides/source/active_job_basics.md (limited to 'guides/source') diff --git a/guides/source/active_job_basics.md b/guides/source/active_job_basics.md new file mode 100644 index 0000000000..7b281bfab2 --- /dev/null +++ b/guides/source/active_job_basics.md @@ -0,0 +1,323 @@ +Active Job Basics +================= + +This guide provides you with all you need to get started in creating, +enqueueing and executing background jobs. + +After reading this guide, you will know: + +* How to create jobs. +* How to enqueue jobs. +* How to run jobs in the background. +* How to send emails from your application async. + +-------------------------------------------------------------------------------- + +Introduction +------------ + +Active Job is a framework for declaring jobs and making them run on a variety +of queueing backends. These jobs can be everything from regularly scheduled +clean-ups, billing charges, or mailings. Anything that can be chopped up +into small units of work and run in parallel, really. + + +The Purpose of the Active Job +----------------------------- +The main point is to ensure that all Rails apps will have a job infrastructure +in place, even if it's in the form of an "immediate runner". We can then have +framework features and other gems build on top of that, without having to +worry about API differences between Delayed Job and Resque. Picking your +queuing backend becomes more of an operational concern, then. And you'll +be able to switch between them without having to rewrite your jobs. + + +Creating a Job +-------------- + +This section will provide a step-by-step guide to creating a job and enqueue it. + +### Create the Job + +```bash +$ bin/rails generate job guests_cleanup +create app/jobs/guests_cleanup_job.rb +``` + +As you can see, you can generate jobs just like you use other generators with +Rails. + +If you didn't want to use a generator, you could create your own file inside of +app/jobs, just make sure that it inherits from `ActiveJob::Base`. + +Here's how a job looks like: + +```ruby +class GuestsCleanupJob < ActiveJob::Base + queue_as :default + + def perform + # Do something later + end +end +``` + +### Enqueue the Job + +Enqueue a job like so: + +```ruby +MyJob.enqueue record # Enqueue a job to be performed as soon the queueing system is free. +``` + +```ruby +MyJob.enqueue_at Date.tomorrow.noon, record # Enqueue a job to be performed tomorrow at noon. +``` + +```ruby +MyJob.enqueue_in 1.week, record # Enqueue a job to be performed 1 week from now. +``` + +That's it! + + +Job Execution +------------- + +If not adapter is set, the job is immediately executed. + +### Backends + +Active Job has adapters for the following queueing backends: + +* [Backburner](https://github.com/nesquena/backburner) +* [Delayed Job](https://github.com/collectiveidea/delayed_job) +* [Qu](https://github.com/bkeepers/qu) +* [Que](https://github.com/chanks/que) +* [QueueClassic](https://github.com/ryandotsmith/queue_classic) +* [Resque 1.x](https://github.com/resque/resque) +* [Sidekiq](https://github.com/mperham/sidekiq) +* [Sneakers](https://github.com/jondot/sneakers) +* [Sucker Punch](https://github.com/brandonhilkert/sucker_punch) + +#### Backends Features + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
 AsyncQueuesDelayedPrioritiesTimeoutRetries
BackburnerYesYesYesYesJobGlobal
Delayed JobYesYesYesJobGlobalGlobal
QueYesYesYesJobNoJob
Queue ClassicYesYesGemNoNoNo
ResqueYesYesGemQueueGlobal?
SidekiqYesYesYesQueueNoJob
SneakersYesYesNoQueueQueueNo
Sucker PunchYesYesYesNoNoNo
Active JobYesYesWIPNoNoNo
Active Job InlineNoYesN/AN/AN/AN/A
+ + +### Change Backends + +You can easy change your adapter in your application.rb or development.rb or production.rb +or in an initializer: + +```ruby +# be sure to have the adapter gem in your Gemfile and follow the adapter specific +# installation and deployment instructions +YourApp::Application.config.active_job.adapter = :sidekiq +``` + +Queues +------ + +Most of the adapters supports multiple queues. With Active Job you can schedule the job +to run on a specific queue: + +```ruby +class GuestsCleanupJob < ActiveJob::Base + queue_as :low_prio + #.... +end +``` + +NOTE: Make sure your queueing backend "listens" on your queue name. For some backends +you need to specify the queues to listen to. + + +Callbacks +--------- + +Active Job provides hooks during the lifecycle of a job. Callbacks allows you to trigger +logic during the lifecycle of a job. + +### Available callbacks + +* before_enqueue +* around_enqueue +* after_enqueue +* before_perform +* around_perform +* after_perform + +### Usage + +```ruby +class GuestsCleanupJob < ActiveJob::Base + queue_as :default + + before_enqueue do |job| + # do somthing with the job instance + end + + around_perform do |job, block| + # do something before perform + block.call + # do something after perform + end + + def perform + # Do something later + end +end + +``` + +GlobalID +-------- +Active Job supports GlobalID for parameters. This makes it possible +to pass live Active Record objects to your job instead of class/id pairs, which +you then have to manually deserialize. Before, jobs would look like this: + +```ruby +class TrashableCleanupJob + def perform(trashable_class, trashable_id, depth) + trashable = trashable_class.constantize.find(trashable_id) + trashable.cleanup(depth) + end +end +``` + +Now you can simply do: + +```ruby +class TrashableCleanupJob + def perform(trashable, depth) + trashable.cleanup(depth) + end +end +``` + +This works with any class that mixes in ActiveModel::GlobalIdentification, which +by default has been mixed into Active Model classes. + + +Exceptions +---------- +Active Job provides a way to catch exceptions raised during the execution of the +job: + +```ruby + +class GuestsCleanupJob < ActiveJob::Base + queue_as :default + + rescue_from(ActiveRecord:NotFound) do |exception| + # do something with the exception + end + + def perform + # Do something later + end +end +``` -- cgit v1.2.3 From b6496713e9fb2e5e34716662087418931564646b Mon Sep 17 00:00:00 2001 From: Cristian Bica Date: Wed, 13 Aug 2014 23:05:21 +0300 Subject: Update ActiveJob guide [ci skip] --- guides/source/active_job_basics.md | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'guides/source') diff --git a/guides/source/active_job_basics.md b/guides/source/active_job_basics.md index 7b281bfab2..4442753370 100644 --- a/guides/source/active_job_basics.md +++ b/guides/source/active_job_basics.md @@ -39,15 +39,25 @@ This section will provide a step-by-step guide to creating a job and enqueue it. ### Create the Job +Active Job provides a Rails generator to create jobs. The following will create a +job in app/jobs: + ```bash $ bin/rails generate job guests_cleanup create app/jobs/guests_cleanup_job.rb ``` +You can also create a job that will run on a specific queue: + +```bash +$ bin/rails generate job guests_cleanup --queue urgent +create app/jobs/guests_cleanup_job.rb +``` + As you can see, you can generate jobs just like you use other generators with Rails. -If you didn't want to use a generator, you could create your own file inside of +If you don't want to use a generator, you could create your own file inside of app/jobs, just make sure that it inherits from `ActiveJob::Base`. Here's how a job looks like: @@ -209,8 +219,7 @@ Active Job has adapters for the following queueing backends: ### Change Backends -You can easy change your adapter in your application.rb or development.rb or production.rb -or in an initializer: +You can easy change your adapter: ```ruby # be sure to have the adapter gem in your Gemfile and follow the adapter specific -- cgit v1.2.3 From 32e184864f6b69f74b90112cd1755ea7a33e5656 Mon Sep 17 00:00:00 2001 From: Aditya Kapoor Date: Thu, 14 Aug 2014 02:33:35 +0530 Subject: [ci skip] add note about the ERB escape in generator docs --- guides/source/generators.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'guides/source') diff --git a/guides/source/generators.md b/guides/source/generators.md index 5e88fa0c70..2349908979 100644 --- a/guides/source/generators.md +++ b/guides/source/generators.md @@ -8,6 +8,7 @@ After reading this guide, you will know: * How to see which generators are available in your application. * How to create a generator using templates. * How Rails searches for generators before invoking them. +* How Rails internally generates Rails code from the templates. * How to customize your scaffold by creating new generators. * How to customize your scaffold by changing generator templates. * How to use fallbacks to avoid overwriting a huge set of generators. @@ -340,6 +341,20 @@ end If you generate another resource, you can see that we get exactly the same result! This is useful if you want to customize your scaffold templates and/or layout by just creating `edit.html.erb`, `index.html.erb` and so on inside `lib/templates/erb/scaffold`. +Many scaffold templates in Rails are written in ERB tags which needs to be escaped, so that the output is a valid ERB code, that can be used correctly in Rails app. + +The following code in one of the generator file, + +```ruby +<%%= stylesheet_include_tag :application %> +``` + +when passed through the generator, would generate the following output. + +```ruby +<%= stylesheet_include_tag :application %> +``` + Adding Generators Fallbacks --------------------------- -- cgit v1.2.3 From a1ddde15ae0d612ff2973de9cf768ed701b594e8 Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Thu, 14 Aug 2014 09:35:32 +0200 Subject: remove deprecated `MissingHelperError` proxy. The error was moved outside of the `ClassMethods` module. --- guides/source/4_2_release_notes.md | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'guides/source') diff --git a/guides/source/4_2_release_notes.md b/guides/source/4_2_release_notes.md index a39dd9ace0..cb451a66c4 100644 --- a/guides/source/4_2_release_notes.md +++ b/guides/source/4_2_release_notes.md @@ -94,6 +94,11 @@ Action Pack Please refer to the [Changelog][action-pack] for detailed changes. +### Removals + +* Removed deprecated `AbstractController::Helpers::ClassMethods::MissingHelperError` + in favor of `AbstractController::Helpers::MissingHelperError`. + ### Deprecations * Deprecated support for setting the `:to` option of a router to a symbol or a -- cgit v1.2.3 From 37cae67e4ef50c8e763eaa7f059048732aa02e7b Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Thu, 14 Aug 2014 10:00:22 +0200 Subject: =?UTF-8?q?=E0=B2=A0=5F=E0=B2=A0=20now=20that=20the=20commit=20sha?= =?UTF-8?q?=20is=20known=20I=20can=20add=20it=20to=20the=20relese=20guide.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- guides/source/4_2_release_notes.md | 1 + 1 file changed, 1 insertion(+) (limited to 'guides/source') diff --git a/guides/source/4_2_release_notes.md b/guides/source/4_2_release_notes.md index cb451a66c4..e8d1dc361a 100644 --- a/guides/source/4_2_release_notes.md +++ b/guides/source/4_2_release_notes.md @@ -98,6 +98,7 @@ Please refer to the [Changelog][action-pack] for detailed changes. * Removed deprecated `AbstractController::Helpers::ClassMethods::MissingHelperError` in favor of `AbstractController::Helpers::MissingHelperError`. + ([Commit](https://github.com/rails/rails/commit/a1ddde15ae0d612ff2973de9cf768ed701b594e8)) ### Deprecations -- cgit v1.2.3 From 967a2ff9989bbf36d283ee447cfcc41750e8b025 Mon Sep 17 00:00:00 2001 From: Arun Agrawal Date: Fri, 15 Aug 2014 15:43:21 +0200 Subject: Fixed link for in_place_editor [ci skip] closes #16512 --- guides/source/security.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'guides/source') diff --git a/guides/source/security.md b/guides/source/security.md index ebfcc5bdd0..d27ec5423f 100644 --- a/guides/source/security.md +++ b/guides/source/security.md @@ -847,7 +847,7 @@ It is recommended to _use RedCloth in combination with a whitelist input filter_ NOTE: _The same security precautions have to be taken for Ajax actions as for "normal" ones. There is at least one exception, however: The output has to be escaped in the controller already, if the action doesn't render a view._ -If you use the [in_place_editor plugin](http://dev.rubyonrails.org/browser/plugins/in_place_editing), or actions that return a string, rather than rendering a view, _you have to escape the return value in the action_. Otherwise, if the return value contains a XSS string, the malicious code will be executed upon return to the browser. Escape any input value using the h() method. +If you use the [in_place_editor plugin](https://github.com/rails/in_place_editing), or actions that return a string, rather than rendering a view, _you have to escape the return value in the action_. Otherwise, if the return value contains a XSS string, the malicious code will be executed upon return to the browser. Escape any input value using the h() method. ### Command Line Injection -- cgit v1.2.3 From a59b9e2284353dcf8cf6d77b97aaa4255563c807 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Fri, 15 Aug 2014 10:43:55 -0300 Subject: Point to rubygems instead of Rails GitHub. [ci skip] The rails repository is not the official plugin anymore --- guides/source/security.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'guides/source') diff --git a/guides/source/security.md b/guides/source/security.md index d27ec5423f..f1db81458c 100644 --- a/guides/source/security.md +++ b/guides/source/security.md @@ -847,7 +847,7 @@ It is recommended to _use RedCloth in combination with a whitelist input filter_ NOTE: _The same security precautions have to be taken for Ajax actions as for "normal" ones. There is at least one exception, however: The output has to be escaped in the controller already, if the action doesn't render a view._ -If you use the [in_place_editor plugin](https://github.com/rails/in_place_editing), or actions that return a string, rather than rendering a view, _you have to escape the return value in the action_. Otherwise, if the return value contains a XSS string, the malicious code will be executed upon return to the browser. Escape any input value using the h() method. +If you use the [in_place_editor plugin](https://rubygems.org/gems/in_place_editing), or actions that return a string, rather than rendering a view, _you have to escape the return value in the action_. Otherwise, if the return value contains a XSS string, the malicious code will be executed upon return to the browser. Escape any input value using the h() method. ### Command Line Injection -- cgit v1.2.3 From 94ae25ecd5635f7f97a2e53afa8e3f82c408435d Mon Sep 17 00:00:00 2001 From: Cristian Bica Date: Fri, 15 Aug 2014 23:32:08 +0300 Subject: ActiveJob: Reworked queue_base_name as default_queue_name + Allow configure ActiveJob from app.config.active_job --- guides/source/active_job_basics.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'guides/source') diff --git a/guides/source/active_job_basics.md b/guides/source/active_job_basics.md index 4442753370..c100448a48 100644 --- a/guides/source/active_job_basics.md +++ b/guides/source/active_job_basics.md @@ -224,7 +224,7 @@ You can easy change your adapter: ```ruby # be sure to have the adapter gem in your Gemfile and follow the adapter specific # installation and deployment instructions -YourApp::Application.config.active_job.adapter = :sidekiq +YourApp::Application.config.active_job.queue_adapter = :sidekiq ``` Queues -- cgit v1.2.3 From cf75464d26687b876335d116e0ce69d95fa3eb3b Mon Sep 17 00:00:00 2001 From: Cristian Bica Date: Sat, 16 Aug 2014 15:28:09 +0300 Subject: ActiveJob guides improvements [ci skip] --- guides/source/active_job_basics.md | 137 ++++++++----------------------------- 1 file changed, 29 insertions(+), 108 deletions(-) (limited to 'guides/source') diff --git a/guides/source/active_job_basics.md b/guides/source/active_job_basics.md index c100448a48..ae5d21d546 100644 --- a/guides/source/active_job_basics.md +++ b/guides/source/active_job_basics.md @@ -27,9 +27,9 @@ The Purpose of the Active Job The main point is to ensure that all Rails apps will have a job infrastructure in place, even if it's in the form of an "immediate runner". We can then have framework features and other gems build on top of that, without having to -worry about API differences between Delayed Job and Resque. Picking your -queuing backend becomes more of an operational concern, then. And you'll -be able to switch between them without having to rewrite your jobs. +worry about API differences between various job runners such as Delayed Job +and Resque. Picking your queuing backend becomes more of an operational concern, +then. And you'll be able to switch between them without having to rewrite your jobs. Creating a Job @@ -112,110 +112,18 @@ Active Job has adapters for the following queueing backends: #### Backends Features - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 AsyncQueuesDelayedPrioritiesTimeoutRetries
BackburnerYesYesYesYesJobGlobal
Delayed JobYesYesYesJobGlobalGlobal
QueYesYesYesJobNoJob
Queue ClassicYesYesGemNoNoNo
ResqueYesYesGemQueueGlobal?
SidekiqYesYesYesQueueNoJob
SneakersYesYesNoQueueQueueNo
Sucker PunchYesYesYesNoNoNo
Active JobYesYesWIPNoNoNo
Active Job InlineNoYesN/AN/AN/AN/A
- +| | Async | Queues | Delayed | Priorities | Timeout | Retries | +|-----------------------|-------|---------|---------|-------------|---------|---------| +| **Backburner** | Yes | Yes | Yes | Yes | Job | Global | +| **Delayed Job** | Yes | Yes | Yes | Job | Global | Global | +| **Que** | Yes | Yes | Yes | Job | No | Job | +| **Queue Classic** | Yes | Yes | Gem | No | No | No | +| **Resque** | Yes | Yes | Gem | Queue | Global | ? | +| **Sidekiq** | Yes | Yes | Yes | Queue | No | Job | +| **Sneakers** | Yes | Yes | No | Queue | Queue | No | +| **Sucker Punch** | Yes | Yes | Yes | No | No | No | +| **Active Job** | Yes | Yes | WIP | No | No | No | +| **Active Job Inline** | No | Yes | N/A | N/A | N/A | N/A | ### Change Backends @@ -235,7 +143,7 @@ to run on a specific queue: ```ruby class GuestsCleanupJob < ActiveJob::Base - queue_as :low_prio + queue_as :low_priority #.... end ``` @@ -279,7 +187,20 @@ class GuestsCleanupJob < ActiveJob::Base # Do something later end end +``` + +ActionMailer +------------ +One of the most common jobs in a modern web application is sending emails outside +of the request-response cycle, so the user doesn't have to wait on it. Active Job +is integrated with Action Mailer so you can easily send emails async: + +```ruby +# Instead of the classic +UserMailer.welcome(@user).deliver +# use #deliver later to send the email async +UserMailer.welcome(@user).deliver_later ``` GlobalID -- cgit v1.2.3 From 5e0f9e40a3523940a23b644e93231385d1760c26 Mon Sep 17 00:00:00 2001 From: Tom Kadwill Date: Sat, 16 Aug 2014 17:10:40 +0100 Subject: [ci skip] re-worded section on CookieStore to make it more readable. --- guides/source/security.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'guides/source') diff --git a/guides/source/security.md b/guides/source/security.md index f1db81458c..125dd82666 100644 --- a/guides/source/security.md +++ b/guides/source/security.md @@ -118,9 +118,9 @@ It works like this: * A user receives credits, the amount is stored in a session (which is a bad idea anyway, but we'll do this for demonstration purposes). * The user buys something. -* Their new, lower credit will be stored in the session. -* The dark side of the user forces them to take the cookie from the first step (which they copied) and replace the current cookie in the browser. -* The user has their credit back. +* The new adjusted credit value is stored in the session. +* The user takes the cookie from the first step (which they previously copied) and replaces the current cookie in the browser. +* The user has their original credit back. Including a nonce (a random value) in the session solves replay attacks. A nonce is valid only once, and the server has to keep track of all the valid nonces. It gets even more complicated if you have several application servers (mongrels). Storing nonces in a database table would defeat the entire purpose of CookieStore (avoiding accessing the database). -- cgit v1.2.3 From 2ba1670f378242438665b43819e41b31ca856bcc Mon Sep 17 00:00:00 2001 From: "yuuji.yaginuma" Date: Sun, 17 Aug 2014 22:42:03 +0900 Subject: [ci skip] Fix sample code in Layouts and Rendering guide --- guides/source/layouts_and_rendering.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'guides/source') diff --git a/guides/source/layouts_and_rendering.md b/guides/source/layouts_and_rendering.md index f00f7bca1b..8b37b92139 100644 --- a/guides/source/layouts_and_rendering.md +++ b/guides/source/layouts_and_rendering.md @@ -903,7 +903,7 @@ You can also specify multiple videos to play by passing an array of videos to th This will produce: ```erb - + ``` #### Linking to Audio Files with the `audio_tag` -- cgit v1.2.3 From 57f5b00ba4f675873d5bab3bc88dae55d4fe7857 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sat, 16 Aug 2014 17:04:26 -0400 Subject: Remove more references to respond_with --- guides/source/caching_with_rails.md | 7 ++++++- guides/source/contributing_to_ruby_on_rails.md | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) (limited to 'guides/source') diff --git a/guides/source/caching_with_rails.md b/guides/source/caching_with_rails.md index 0902e347e2..d0f3a596fe 100644 --- a/guides/source/caching_with_rails.md +++ b/guides/source/caching_with_rails.md @@ -353,7 +353,12 @@ Instead of an options hash, you can also simply pass in a model, Rails will use class ProductsController < ApplicationController def show @product = Product.find(params[:id]) - respond_with(@product) if stale?(@product) + + if stale?(@product) + respond_to do |wants| + # ... normal response processing + end + end end end ``` diff --git a/guides/source/contributing_to_ruby_on_rails.md b/guides/source/contributing_to_ruby_on_rails.md index 0b05725623..8bc4b10591 100644 --- a/guides/source/contributing_to_ruby_on_rails.md +++ b/guides/source/contributing_to_ruby_on_rails.md @@ -397,7 +397,7 @@ inside, just indent it with 4 spaces: class ArticlesController def index - respond_with Article.limit(10) + render json: Article.limit(10) end end -- cgit v1.2.3