<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Schen35</id>
	<title>Expertiza_Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.expertiza.ncsu.edu/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Schen35"/>
	<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Special:Contributions/Schen35"/>
	<updated>2026-05-16T09:55:12Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016_E1624_Compose_functional_tests_for_Suggest_topics_of_student_functionality&amp;diff=102270</id>
		<title>CSC/ECE 517 Spring 2016 E1624 Compose functional tests for Suggest topics of student functionality</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016_E1624_Compose_functional_tests_for_Suggest_topics_of_student_functionality&amp;diff=102270"/>
		<updated>2016-04-15T00:48:18Z</updated>

		<summary type="html">&lt;p&gt;Schen35: /* Capybara Based Test Design */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This wiki page is for the description of changes made under E1624 Final project design assignment for Spring 2016, CSC/ECE 517. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
===What it does===&lt;br /&gt;
Suggested topic functionality is designed for students to suggest topics they interested in. An assignment can be set up to allow students to suggest topics at the discretion of the instructor. Then, students can suggest topics, and add related descriptions. The proposer can state that (s)he wants to work on the suggested topic.  When the instructor approves a suggested topic, if the proposer wanted to work on it, it is assigned to the proposer. Otherwise, it is placed on the signup sheet for anyone to reserve. The purpose of suggested topic is to make the writing assignment more personalized.&lt;br /&gt;
===What’s wrong with it===&lt;br /&gt;
Currently, there is no functional test for student's topic suggestion function.&lt;br /&gt;
&lt;br /&gt;
===What needs to be done===&lt;br /&gt;
*Understand the flow of the suggested topic function.&lt;br /&gt;
*Do not use the development DB, otherwise the tests will not pass on TravisCI.&lt;br /&gt;
*Create RSpec file in /spec/features/ folder.&lt;br /&gt;
*Use fixtures to create the assignment record  and any other records such as participant records in test DB.  &lt;br /&gt;
*Use Capybara to write functional tests for these three scenarios.&lt;br /&gt;
**One team is on the waitlist. They sent a suggestion for new topic and they want to choose their suggested topic. After their suggested topic is approved, they should leave the waitlist and hold their suggested topic;&lt;br /&gt;
**One team is holding a topic. They sent a suggestion for new topic and they want to choose their suggested topic. After their suggested topic is approved and they choose to switch to suggested topic, they will hold suggested topic and their old topic will be released. And if another team is in waitlist of that old topic, that team should hold the old topic now;&lt;br /&gt;
**One team is holding a topic. They sent a suggestion for new topic and they do not want to work on their suggested topic. After their suggested topic is approved and they choose to public suggested topic, they will still hold their old topic. Their suggested topic will be added in sign-up sheet.&lt;br /&gt;
*Create multiple tests to check valid and invalid cases.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Capybara Based Test Design==&lt;br /&gt;
To test the functionality of student's suggest topic, we plan to use Capybara to write the test code for each step listed below, covering all three scenarios. &lt;br /&gt;
(We wish to add some UI screenshots for each step, for better illustration, but seems the file upload function of this expertiza wiki is available for student user level so far.)&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
In the project, we assume that an instructor account ''instructor6'' and a student account ''student11'' is already created.&lt;br /&gt;
===Flow of suggest topic function===&lt;br /&gt;
====Step 1 Use instructor to add a course with topic suggestion enabled====&lt;br /&gt;
First step is to login with the instructor account (''instructor6'' here), and then create a course with the '''new public assignment''' function.&lt;br /&gt;
We will create the assignment and edit the topic feature to '''enable the topic suggestions''' function for student, where in the UI, it means select the &amp;quot;Allow topic suggestions from students?&amp;quot; checkbox.&lt;br /&gt;
Also, to test the second and third scenarios that the student already hold a topic, we will '''add a new topic''' with 1 slot for testing the later two scenarios.&lt;br /&gt;
After all above are done, the course just created can be saved to the test database.&lt;br /&gt;
&lt;br /&gt;
In this step, we need to write two functional tests: one is to check whether the course can be added and does the added one have topic suggestion enabled, the other one is to check whether the topic is added by instructor with 1 slot.&lt;br /&gt;
[[File:Csc517-final-design-yj-2.png]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Step 2 Add student to the course====&lt;br /&gt;
After the course is added, the student (''student11'' for this test project) will be '''added as participants''' to the course. &lt;br /&gt;
For UI test, it should be entering &amp;quot;student11&amp;quot; as the participant name with the &amp;quot;participant&amp;quot; choice selected then click &amp;quot;add&amp;quot; button.&lt;br /&gt;
&lt;br /&gt;
In this step, we need to test whether the student, named ''student11'', is added to the course correctly.&lt;br /&gt;
&lt;br /&gt;
[[File:Csc517-final-design-yj-3.png]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Step 3.1 Use student to suggest a new topic without holding any topic====&lt;br /&gt;
For the first scenario, which the student does not holding any topic, we should test after login with ''student11'' account and choose the assignment just created by ''instructor6''. &lt;br /&gt;
Then, we will '''suggest a topic''' with the choice of &amp;quot;Yes&amp;quot; for the question '''Do you wish to work on this topic?''' and '''submit''' the new topic suggestion.&lt;br /&gt;
&lt;br /&gt;
In this step, we need to write the function test to check whether the new topic is added and the student is willing to work on this topic if it is approved.&lt;br /&gt;
&lt;br /&gt;
[[File:Csc517-final-design-yj-41.png]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Step 3.2 Use student to suggest a new topic holding a topic and wish to switch ====&lt;br /&gt;
For the second scenario, we should first let the student login and '''Signup''' the only topic created by the instructor in the Signup sheet. &lt;br /&gt;
Then do the same thing as depict in Step 3.1. &lt;br /&gt;
&lt;br /&gt;
In this step, we need to write two function tests: one is to check whether the student is already on the student list the topic, and the other one is to check whether the topic's available slots of the topic is 1 and the max number of slots is 2.&lt;br /&gt;
[[File:Csc517-final-design-yj-5.png]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Step 3.3 Use student to suggest a new topic holding a topic and do '''NOT''' wish to switch ====&lt;br /&gt;
For the third scenario, we should do the same thing as introduced in Step 3.2, but the only difference is to select the choice of &amp;quot;No&amp;quot; for the question '''Do you wish to work on this topic?'''.&lt;br /&gt;
&lt;br /&gt;
In this step, we also need to write two function tests: one is to check whether the student is already on the student list the topic, and the other one is to check whether the topic's available slots of the topic is 0 now and the max number of slots is 2.&lt;br /&gt;
&lt;br /&gt;
[[File:Csc517-final-design-yj-6.png]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Step 4 Approve/Reject the suggested topic====&lt;br /&gt;
The approve/reject functions should also be tested by using the ''instructor6'' account. After login the instructor account, use the assignment '''view suggestions''' function to '''view''' and then '''approve/reject''' the new suggested topic. &lt;br /&gt;
&lt;br /&gt;
In this step, we need to write two function tests: one is to check whether the '''Signup sheet''' does have/not have the suggested topic if it is approved/rejected, the other one is to check whether in the student '''suggest new topics''', the topic suggested by ''student11'' is indeed approved/rejected accordingly. &lt;br /&gt;
&lt;br /&gt;
Note that, this step is the final test step for the case of suggested topic is rejected. The following 3 possible step 5 is for the case the suggested topic is approved.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Csc517-final-design-yj-9.png]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Step 5.1 Student did not holding any topic before suggestion====&lt;br /&gt;
In addition to step 4, since the choice is &amp;quot;Yes&amp;quot; for willing to work on this topic when the student suggested, we will also need to test whether the student is already on the list of the new suggested topic and the number of available slot of the new suggested topic is 0.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Step 5.2 Student held a topic but '''NOT''' wish to switch to new suggested topic====&lt;br /&gt;
For the student was holding a topic and do not want to switch, we need to write two tests: one is to check whether the student is still enrolled on the previous holding topic, and the other one is to confirm that the available slot of the new topic suggested topic is 1.&lt;br /&gt;
&lt;br /&gt;
[[File:Csc517-final-design-yj-7.png]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Step 5.3 Student held a topic and wish to switch to new suggested topic====&lt;br /&gt;
For the student was holding a topic, coupled with the test written for Step 5.1, there is another test need to be written to check whether the slot of the previous held topic is increased by one.&lt;br /&gt;
&lt;br /&gt;
[[File:Csc517-final-design-yj-8.png]]&lt;/div&gt;</summary>
		<author><name>Schen35</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016_E1624_Compose_functional_tests_for_Suggest_topics_of_student_functionality&amp;diff=102269</id>
		<title>CSC/ECE 517 Spring 2016 E1624 Compose functional tests for Suggest topics of student functionality</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=CSC/ECE_517_Spring_2016_E1624_Compose_functional_tests_for_Suggest_topics_of_student_functionality&amp;diff=102269"/>
		<updated>2016-04-15T00:44:48Z</updated>

		<summary type="html">&lt;p&gt;Schen35: /* Design of solution */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This wiki page is for the description of changes made under E1624 Final project design assignment for Spring 2016, CSC/ECE 517. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Problem Statement==&lt;br /&gt;
&lt;br /&gt;
===What it does===&lt;br /&gt;
Suggested topic functionality is designed for students to suggest topics they interested in. An assignment can be set up to allow students to suggest topics at the discretion of the instructor. Then, students can suggest topics, and add related descriptions. The proposer can state that (s)he wants to work on the suggested topic.  When the instructor approves a suggested topic, if the proposer wanted to work on it, it is assigned to the proposer. Otherwise, it is placed on the signup sheet for anyone to reserve. The purpose of suggested topic is to make the writing assignment more personalized.&lt;br /&gt;
===What’s wrong with it===&lt;br /&gt;
Currently, there is no functional test for student's topic suggestion function.&lt;br /&gt;
&lt;br /&gt;
===What needs to be done===&lt;br /&gt;
*Understand the flow of the suggested topic function.&lt;br /&gt;
*Do not use the development DB, otherwise the tests will not pass on TravisCI.&lt;br /&gt;
*Create RSpec file in /spec/features/ folder.&lt;br /&gt;
*Use fixtures to create the assignment record  and any other records such as participant records in test DB.  &lt;br /&gt;
*Use Capybara to write functional tests for these three scenarios.&lt;br /&gt;
**One team is on the waitlist. They sent a suggestion for new topic and they want to choose their suggested topic. After their suggested topic is approved, they should leave the waitlist and hold their suggested topic;&lt;br /&gt;
**One team is holding a topic. They sent a suggestion for new topic and they want to choose their suggested topic. After their suggested topic is approved and they choose to switch to suggested topic, they will hold suggested topic and their old topic will be released. And if another team is in waitlist of that old topic, that team should hold the old topic now;&lt;br /&gt;
**One team is holding a topic. They sent a suggestion for new topic and they do not want to work on their suggested topic. After their suggested topic is approved and they choose to public suggested topic, they will still hold their old topic. Their suggested topic will be added in sign-up sheet.&lt;br /&gt;
*Create multiple tests to check valid and invalid cases.&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==Capybara Based Test Design==&lt;br /&gt;
To test the functionality of student's suggest topic, we plan to use Capybara to write the test code for each step listed below, covering all three scenarios.&lt;br /&gt;
(We wish to add some UI screenshots for each step, for better illustration, but seems the file upload function of this expertiza wiki is available for student user level so far.)&lt;br /&gt;
&lt;br /&gt;
===Prerequisites===&lt;br /&gt;
In the project, we assume that an instructor account ''instructor6'' and a student account ''student11'' is already created.&lt;br /&gt;
===Flow of suggest topic function===&lt;br /&gt;
====Step 1 Use instructor to add a course with topic suggestion enabled====&lt;br /&gt;
First step is to login with the instructor account (''instructor6'' here), and then create a course with the '''new public assignment''' function.&lt;br /&gt;
We will create the assignment and edit the topic feature to '''enable the topic suggestions''' function for student, where in the UI, it means select the &amp;quot;Allow topic suggestions from students?&amp;quot; checkbox.&lt;br /&gt;
Also, to test the second and third scenarios that the student already hold a topic, we will '''add a new topic''' with 1 slot for testing the later two scenarios.&lt;br /&gt;
After all above are done, the course just created can be saved to the test database.&lt;br /&gt;
&lt;br /&gt;
In this step, we need to write two functional tests: one is to check whether the course can be added and does the added one have topic suggestion enabled, the other one is to check whether the topic is added by instructor with 1 slot.&lt;br /&gt;
[[File:Csc517-final-design-yj-2.png]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Step 2 Add student to the course====&lt;br /&gt;
After the course is added, the student (''student11'' for this test project) will be '''added as participants''' to the course. &lt;br /&gt;
For UI test, it should be entering &amp;quot;student11&amp;quot; as the participant name with the &amp;quot;participant&amp;quot; choice selected then click &amp;quot;add&amp;quot; button.&lt;br /&gt;
&lt;br /&gt;
In this step, we need to test whether the student, named ''student11'', is added to the course correctly.&lt;br /&gt;
&lt;br /&gt;
[[File:Csc517-final-design-yj-3.png]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Step 3.1 Use student to suggest a new topic without holding any topic====&lt;br /&gt;
For the first scenario, which the student does not holding any topic, we should test after login with ''student11'' account and choose the assignment just created by ''instructor6''. &lt;br /&gt;
Then, we will '''suggest a topic''' with the choice of &amp;quot;Yes&amp;quot; for the question '''Do you wish to work on this topic?''' and '''submit''' the new topic suggestion.&lt;br /&gt;
&lt;br /&gt;
In this step, we need to write the function test to check whether the new topic is added and the student is willing to work on this topic if it is approved.&lt;br /&gt;
&lt;br /&gt;
[[File:Csc517-final-design-yj-41.png]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Step 3.2 Use student to suggest a new topic holding a topic and wish to switch ====&lt;br /&gt;
For the second scenario, we should first let the student login and '''Signup''' the only topic created by the instructor in the Signup sheet. &lt;br /&gt;
Then do the same thing as depict in Step 3.1. &lt;br /&gt;
&lt;br /&gt;
In this step, we need to write two function tests: one is to check whether the student is already on the student list the topic, and the other one is to check whether the topic's available slots of the topic is 1 and the max number of slots is 2.&lt;br /&gt;
[[File:Csc517-final-design-yj-5.png]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Step 3.3 Use student to suggest a new topic holding a topic and do '''NOT''' wish to switch ====&lt;br /&gt;
For the third scenario, we should do the same thing as introduced in Step 3.2, but the only difference is to select the choice of &amp;quot;No&amp;quot; for the question '''Do you wish to work on this topic?'''.&lt;br /&gt;
&lt;br /&gt;
In this step, we also need to write two function tests: one is to check whether the student is already on the student list the topic, and the other one is to check whether the topic's available slots of the topic is 0 now and the max number of slots is 2.&lt;br /&gt;
&lt;br /&gt;
[[File:Csc517-final-design-yj-6.png]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Step 4 Approve/Reject the suggested topic====&lt;br /&gt;
The approve/reject functions should also be tested by using the ''instructor6'' account. After login the instructor account, use the assignment '''view suggestions''' function to '''view''' and then '''approve/reject''' the new suggested topic. &lt;br /&gt;
&lt;br /&gt;
In this step, we need to write two function tests: one is to check whether the '''Signup sheet''' does have/not have the suggested topic if it is approved/rejected, the other one is to check whether in the student '''suggest new topics''', the topic suggested by ''student11'' is indeed approved/rejected accordingly. &lt;br /&gt;
&lt;br /&gt;
Note that, this step is the final test step for the case of suggested topic is rejected. The following 3 possible step 5 is for the case the suggested topic is approved.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[File:Csc517-final-design-yj-9.png]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Step 5.1 Student did not holding any topic before suggestion====&lt;br /&gt;
In addition to step 4, since the choice is &amp;quot;Yes&amp;quot; for willing to work on this topic when the student suggested, we will also need to test whether the student is already on the list of the new suggested topic and the number of available slot of the new suggested topic is 0.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Step 5.2 Student held a topic but '''NOT''' wish to switch to new suggested topic====&lt;br /&gt;
For the student was holding a topic and do not want to switch, we need to write two tests: one is to check whether the student is still enrolled on the previous holding topic, and the other one is to confirm that the available slot of the new topic suggested topic is 1.&lt;br /&gt;
&lt;br /&gt;
[[File:Csc517-final-design-yj-7.png]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Step 5.3 Student held a topic and wish to switch to new suggested topic====&lt;br /&gt;
For the student was holding a topic, coupled with the test written for Step 5.1, there is another test need to be written to check whether the slot of the previous held topic is increased by one.&lt;br /&gt;
&lt;br /&gt;
[[File:Csc517-final-design-yj-8.png]]&lt;/div&gt;</summary>
		<author><name>Schen35</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100768</id>
		<title>Active Job</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100768"/>
		<updated>2016-02-06T03:28:04Z</updated>

		<summary type="html">&lt;p&gt;Schen35: /* Background Mail Sender */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
Active Job is a framework that helps developers writing codes and run them on the background automatically under different scenarios. It’s an interface that adapts different queueing backends like [https://github.com/nesquena/backburner Backburner], [https://github.com/collectiveidea/delayed_job Delayed Job], [https://github.com/bkeepers/qu Qu] and so on. Jobs can vary from schedule newsletter, follow-up emails to database housekeeping. &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” ]&amp;lt;/ref&amp;gt; Overall, Active Job is a interface which you can work with common queues.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
====Ruby On Rails versions support Active Job====&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;float:left;&amp;quot;&lt;br /&gt;
|+ Version history&lt;br /&gt;
|-&lt;br /&gt;
! Version !! Date&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || 2014/12/19&lt;br /&gt;
|-&lt;br /&gt;
| 4.2.5 || 2015/11/13&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
====Active Job adapters&amp;lt;ref name=&amp;quot;Active-Job-Adapter&amp;quot;&amp;gt;Active Job Adapter[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters.html&amp;quot;Active Job Adapter&amp;quot; ]&amp;lt;/ref&amp;gt;====&lt;br /&gt;
List of queueing backends Active Job support:&lt;br /&gt;
*Backburner[https://github.com/nesquena/backburner]&lt;br /&gt;
*Delayed Job[https://github.com/collectiveidea/delayed_job]&lt;br /&gt;
*Qu[https://github.com/bkeepers/qu]&lt;br /&gt;
*Que[https://github.com/chanks/que]&lt;br /&gt;
*queue_classic[https://github.com/QueueClassic/queue_classic]&lt;br /&gt;
*Resque 1.x[https://github.com/resque/resque]&lt;br /&gt;
*Sidekiq[http://sidekiq.org/]&lt;br /&gt;
*Sneakers[https://github.com/jondot/sneakers]&lt;br /&gt;
*Sucker Punch[https://github.com/brandonhilkert/sucker_punch]&lt;br /&gt;
*Active Job Async Job&lt;br /&gt;
*Active Job Inline[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters/InlineAdapter.html]&lt;br /&gt;
&lt;br /&gt;
=='''How to use &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/active_job_basics.html&amp;lt;/ref&amp;gt;'''==&lt;br /&gt;
We will introduce how to creating a job and and how to add the job into a queue. &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
=== Download ===&lt;br /&gt;
With RubyGems you can install Active Job:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gem install activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://github.com/rails/rails/tree/master/activejob Souce code of Active Job] available on GitHub, as part of Rails.&amp;lt;ref&amp;gt; GitHub [https://github.com/rails/rails/tree/master/activejob “Active Job -- Make work happen later” 2015] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Create a Job ===&lt;br /&gt;
In Active Job, a process 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. By doing the following, we created a Job called “update_wiki”.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki&lt;br /&gt;
  invoke  test_unit&lt;br /&gt;
  create  test/jobs/update_wiki_job_test.rb&lt;br /&gt;
  create  app/jobs/update_wiki_job.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Active Job provides the ability to run your Job on a specific queue by creating a job:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki --queue urgent&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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:&amp;lt;ref&amp;gt; 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] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ApplicationJob &amp;lt; ActiveJob::Base&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 4.2 a Job class defined a perform method and set a “queue_as” value:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :default&lt;br /&gt;
 &lt;br /&gt;
  def perform(*wiki)&lt;br /&gt;
   # Do something later&lt;br /&gt;
  wiki.update_contents&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There is a perform method to be called when the Job was first enqueued. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Adding a Job to the queue ===&lt;br /&gt;
If you wish your Job be processed as soon as the queuing system is free, you can enqueue a Job like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.perform_later wiki&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can add a Job be performed tomorrow at noon:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait_until: Date.tomorrow.noon).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you want your Job be performed a week from now, most of the queueing backends ( Sidekiq&lt;br /&gt;
, Delayed Job, etc. ) allow you to set a delay time.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait: 1.week).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Execution of Job ===&lt;br /&gt;
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]).&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt; Without setting any adapter, the job would be performed immediately.&amp;lt;br&amp;gt;&lt;br /&gt;
Queueing backend can be set at: /config/application.rb, in this example we use the Sidekiq.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    # Be sure to have the adapter's gem in your Gemfile and follow&lt;br /&gt;
    # the adapter's specific installation and deployment instructions.&lt;br /&gt;
    config.active_job.queue_adapter = :sidekiq&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Play with Queues ===&lt;br /&gt;
Active Job allows to schedule the job to be processed on a specific queue, this became helpful as common adapters support multiple queues.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Queue name can be prefixed for all jobs using config.active_job.queue_name_prefixin application.rb:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# config/application.rb&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    config.active_job.queue_name_prefix = Rails.env&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# app/jobs/update_wiki_job.rb&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# Now your job will run on queue production_low_priority on your&lt;br /&gt;
# production environment and on staging_low_priority on your staging&lt;br /&gt;
# environment&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can control a job to run on a queue you like, by passing a :queue option to #set :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
MyJob.set(queue: :another_queue).perform_later(record)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the code snippets are referred from the source&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
In this section, a example is provided to demonstrate how to use ActiveJob in practice.&lt;br /&gt;
=== Background Mail Sender ===&lt;br /&gt;
You can send emails asynchronously with Action Mailer which Active Job is already integrated in.&amp;lt;ref name= &amp;quot;Action-Mailer-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/action_mailer_basics.html “Action Mailer Basics” 2014]&amp;lt;/ref&amp;gt; Here in this example, we try to use Active Job with Action Mailer.&amp;lt;br&amp;gt;&lt;br /&gt;
==== 1. Getting started with Rails&amp;lt;ref name= &amp;quot;Welcome_to_Rails&amp;quot;&amp;gt;GitHub [https://github.com/rails/rails “Welcome to Rails” 2015]&amp;lt;/ref&amp;gt; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Install Rails if you haven’t done it yet:&lt;br /&gt;
$ gem install rails&lt;br /&gt;
&lt;br /&gt;
# Create a new Rails application, “myapp_activejob”&lt;br /&gt;
# is the name of the application:&lt;br /&gt;
$ rails new myapp_activejob&lt;br /&gt;
&lt;br /&gt;
# Change your directory to myapp_activejob&lt;br /&gt;
$ cd myapp_activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Edit the Gemfile in the folder, add following into it:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'responders'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Run “ $ bundle update “ and make sure “$ rails server” works.&lt;br /&gt;
&lt;br /&gt;
==== 2. Resque setup&amp;lt;ref name= &amp;quot;Download_Redis&amp;quot;&amp;gt;Redis [http://redis.io/download “Download Redis” 2016]&amp;lt;/ref&amp;gt;====&lt;br /&gt;
We will use [https://github.com/resque/resque Resque] as the enqueuing backend. You need to install [http://redis.io/ Redis] before you can run Resque. You can get Redis using [http://brew.sh/ Homebrew]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ brew install redis&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can download, extract and compile Redis using:&amp;lt;ref name= &amp;quot;Download_Redis&amp;quot;&amp;gt;Redis [http://redis.io/download “Download Redis” 2016]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ wget http://download.redis.io/releases/redis-3.0.7.tar.gz&lt;br /&gt;
$ tar xzf redis-3.0.7.tar.gz&lt;br /&gt;
$ cd redis-3.0.7&lt;br /&gt;
$ make&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Redis does not officially support Microsoft [https://en.wikipedia.org/wiki/Microsoft_Windows Windows] platform, but you can find  [https://github.com/MSOpenTech/redis Redis on Windows] by Microsoft Open Tech group.&amp;lt;br&amp;gt;&lt;br /&gt;
Next get Resque be installed. To use resque with Active Job, we also need resque-scheduler.&lt;br /&gt;
Add the following into Gemfile, and run “$ bundle install”.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'resque'&lt;br /&gt;
gem 'resque-scheduler'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After the installation, create a Resque configuration file resque.rb in config/initializers/&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/initializers/resque.rb&lt;br /&gt;
&lt;br /&gt;
Resque.redis = Redis.new(host: 'localhost', post: 6379)&lt;br /&gt;
Resque.after_fork = Proc.new { ActiveRecord::Base.establish_connection }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As the Resque tasks and Resque Scheduler rake tasks are needed in this example, we need to create a resque.rake file in lib/tasks/:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#lib/tasks/resque.rake&lt;br /&gt;
&lt;br /&gt;
require 'resque/tasks'&lt;br /&gt;
require 'resque/scheduler/tasks'&lt;br /&gt;
&lt;br /&gt;
namespace :resque do&lt;br /&gt;
  task setup: :environment do&lt;br /&gt;
    ENV['TERM_CHILD'] ||= '1'&lt;br /&gt;
    ENV['QUEUE'] ||= '*'&lt;br /&gt;
    require 'resque'&lt;br /&gt;
    require 'resque-scheduler'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== 3. Creating a Mailer&amp;lt;ref name= &amp;quot;Action-Mailer-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/action_mailer_basics.html “Action Mailer Basics” 2014]&amp;lt;/ref&amp;gt; ====&lt;br /&gt;
By doing the things above, we already have our Rails project and the Resque for queuing get set. Next we will create a Mailer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate mailer user_mailer&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a method and a view for sending the email.&amp;lt;br&amp;gt;&lt;br /&gt;
In app/mailer/user_mailer.rb:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/mailers/user_mailer.rb&lt;br /&gt;
&lt;br /&gt;
class UserMailer &amp;lt; ActionMailer::Base&lt;br /&gt;
  default from: 'from@example.com'&lt;br /&gt;
&lt;br /&gt;
  def test_email(email)&lt;br /&gt;
    mail(&lt;br /&gt;
      to: email,&lt;br /&gt;
      subject: 'We are testing Active Job!'&lt;br /&gt;
    )&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In app/views/user_mailer/test_email.text&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/views/user_mailer/test_email.text&lt;br /&gt;
&lt;br /&gt;
Hey, we are testing Active Job!&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== 4. Active Job&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt;====&lt;br /&gt;
Create a configuration file active_job.rb in config/initializers/ , to set Resque as queue_adapter.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/initializers/active_job.rb&lt;br /&gt;
&lt;br /&gt;
ActiveJob::Base.queue_adapter = :resque&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a Job with generator.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job test_email&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the generated file app/jobs/test_email_job.rb, define the perform method and set it’s queue_as.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/jobs/test_email_job.rb&lt;br /&gt;
&lt;br /&gt;
class TestEmailJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :email&lt;br /&gt;
&lt;br /&gt;
  def perform(email)&lt;br /&gt;
    UserMailer.test_email(email).deliver_now&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can use a UserController to put the job into the queue for later execution. Here we set the email to be sent one minute later for test.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job test_email&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/controllers/users_controller.rb&lt;br /&gt;
&lt;br /&gt;
class UsersController &amp;lt; ApplicationController&lt;br /&gt;
  def new&lt;br /&gt;
    @user = User.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def create&lt;br /&gt;
    @user = User.create(user_params)&lt;br /&gt;
    TestEmailJob.new(@user.email).deliver_later!(wait: 1.minute)&lt;br /&gt;
    # redirect somewhere&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Routes and view need to set to make it work.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/routes.rb&lt;br /&gt;
&lt;br /&gt;
Rails.application.routes.draw do&lt;br /&gt;
  resources :users, only: [:new, :create]&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/views/users/new.html.erb&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= form_for @user do |f| %&amp;gt;&lt;br /&gt;
  &amp;lt;%= f.email_field :email %&amp;gt;&lt;br /&gt;
  &amp;lt;%= f.submit %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== 5. Try execution ====&lt;br /&gt;
Before we try the Mailer, we need MailCatcher for the test. Add following into Gemfile and “$ bundle install”&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'mailcatcher'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To set the environment, add following things into config/environments/development.rb.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/environments/development.rb&lt;br /&gt;
&lt;br /&gt;
Rails.application.configure do&lt;br /&gt;
  ...&lt;br /&gt;
  config.action_mailer.delivery_method = :smtp&lt;br /&gt;
  config.action_mailer.smtp_settings = { address: &amp;quot;localhost&amp;quot;, port: 1025 }&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run it!&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Start mailcatcher&lt;br /&gt;
$ mailcatcher&lt;br /&gt;
&lt;br /&gt;
# Start redis-server&lt;br /&gt;
$ redis-server&lt;br /&gt;
&lt;br /&gt;
# Start resque:work&lt;br /&gt;
$ bundle exec rake resque:work&lt;br /&gt;
&lt;br /&gt;
# Start resque:scheduler&lt;br /&gt;
$ rake environment resque:scheduler&lt;br /&gt;
&lt;br /&gt;
# Finally start rails server&lt;br /&gt;
$ rails server&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Open the browser, visit localhost:3000/users/new and sign up as a new user. One minute later you can see following in Resque scheduler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resque-scheduler: [INFO] 2016-02-05T22:53:15+09:00: Processing Delayed Items&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
And in MailCatcher:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
==&amp;gt; SMTP: Received message from '&amp;lt;from@example.com&amp;gt;' (315 bytes)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=='''References'''==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Schen35</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100766</id>
		<title>Active Job</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100766"/>
		<updated>2016-02-06T03:17:49Z</updated>

		<summary type="html">&lt;p&gt;Schen35: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
Active Job is a framework that helps developers writing codes and run them on the background automatically under different scenarios. It’s an interface that adapts different queueing backends like [https://github.com/nesquena/backburner Backburner], [https://github.com/collectiveidea/delayed_job Delayed Job], [https://github.com/bkeepers/qu Qu] and so on. Jobs can vary from schedule newsletter, follow-up emails to database housekeeping. &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” ]&amp;lt;/ref&amp;gt; Overall, Active Job is a interface which you can work with common queues.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
====Ruby On Rails versions support Active Job====&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;float:left;&amp;quot;&lt;br /&gt;
|+ Version history&lt;br /&gt;
|-&lt;br /&gt;
! Version !! Date&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || 2014/12/19&lt;br /&gt;
|-&lt;br /&gt;
| 4.2.5 || 2015/11/13&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
====Active Job adapters&amp;lt;ref name=&amp;quot;Active-Job-Adapter&amp;quot;&amp;gt;Active Job Adapter[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters.html&amp;quot;Active Job Adapter&amp;quot; ]&amp;lt;/ref&amp;gt;====&lt;br /&gt;
List of queueing backends Active Job support:&lt;br /&gt;
*Backburner[https://github.com/nesquena/backburner]&lt;br /&gt;
*Delayed Job[https://github.com/collectiveidea/delayed_job]&lt;br /&gt;
*Qu[https://github.com/bkeepers/qu]&lt;br /&gt;
*Que[https://github.com/chanks/que]&lt;br /&gt;
*queue_classic[https://github.com/QueueClassic/queue_classic]&lt;br /&gt;
*Resque 1.x[https://github.com/resque/resque]&lt;br /&gt;
*Sidekiq[http://sidekiq.org/]&lt;br /&gt;
*Sneakers[https://github.com/jondot/sneakers]&lt;br /&gt;
*Sucker Punch[https://github.com/brandonhilkert/sucker_punch]&lt;br /&gt;
*Active Job Async Job&lt;br /&gt;
*Active Job Inline[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters/InlineAdapter.html]&lt;br /&gt;
&lt;br /&gt;
=='''How to use &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/active_job_basics.html&amp;lt;/ref&amp;gt;'''==&lt;br /&gt;
We will introduce how to creating a job and and how to add the job into a queue. &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
=== Download ===&lt;br /&gt;
With RubyGems you can install Active Job:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gem install activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://github.com/rails/rails/tree/master/activejob Souce code of Active Job] available on GitHub, as part of Rails.&amp;lt;ref&amp;gt; GitHub [https://github.com/rails/rails/tree/master/activejob “Active Job -- Make work happen later” 2015] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Create a Job ===&lt;br /&gt;
In Active Job, a process 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. By doing the following, we created a Job called “update_wiki”.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki&lt;br /&gt;
  invoke  test_unit&lt;br /&gt;
  create  test/jobs/update_wiki_job_test.rb&lt;br /&gt;
  create  app/jobs/update_wiki_job.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Active Job provides the ability to run your Job on a specific queue by creating a job:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki --queue urgent&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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:&amp;lt;ref&amp;gt; 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] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ApplicationJob &amp;lt; ActiveJob::Base&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 4.2 a Job class defined a perform method and set a “queue_as” value:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :default&lt;br /&gt;
 &lt;br /&gt;
  def perform(*wiki)&lt;br /&gt;
   # Do something later&lt;br /&gt;
  wiki.update_contents&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There is a perform method to be called when the Job was first enqueued. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Adding a Job to the queue ===&lt;br /&gt;
If you wish your Job be processed as soon as the queuing system is free, you can enqueue a Job like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.perform_later wiki&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can add a Job be performed tomorrow at noon:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait_until: Date.tomorrow.noon).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you want your Job be performed a week from now, most of the queueing backends ( Sidekiq&lt;br /&gt;
, Delayed Job, etc. ) allow you to set a delay time.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait: 1.week).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Execution of Job ===&lt;br /&gt;
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]).&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt; Without setting any adapter, the job would be performed immediately.&amp;lt;br&amp;gt;&lt;br /&gt;
Queueing backend can be set at: /config/application.rb, in this example we use the Sidekiq.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    # Be sure to have the adapter's gem in your Gemfile and follow&lt;br /&gt;
    # the adapter's specific installation and deployment instructions.&lt;br /&gt;
    config.active_job.queue_adapter = :sidekiq&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Play with Queues ===&lt;br /&gt;
Active Job allows to schedule the job to be processed on a specific queue, this became helpful as common adapters support multiple queues.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Queue name can be prefixed for all jobs using config.active_job.queue_name_prefixin application.rb:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# config/application.rb&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    config.active_job.queue_name_prefix = Rails.env&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# app/jobs/update_wiki_job.rb&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# Now your job will run on queue production_low_priority on your&lt;br /&gt;
# production environment and on staging_low_priority on your staging&lt;br /&gt;
# environment&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can control a job to run on a queue you like, by passing a :queue option to #set :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
MyJob.set(queue: :another_queue).perform_later(record)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the code snippets are referred from the source&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
In this section, a example is provided to demonstrate how to use ActiveJob in practice.&lt;br /&gt;
=== Background Mail Sender ===&lt;br /&gt;
You can send emails asynchronously with Action Mailer which Active Job is already integrated in&amp;lt;ref name= &amp;quot;Action-Mailer-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/action_mailer_basics.html “Action Mailer Basics” 2014]&amp;lt;/ref&amp;gt;. Here in this example, we try to use Active Job with Action Mailer.&amp;lt;br&amp;gt;&lt;br /&gt;
==== 1. Getting started with Rails&amp;lt;ref name= &amp;quot;Welcome_to_Rails&amp;quot;&amp;gt;GitHub [https://github.com/rails/rails “Welcome to Rails” 2015]&amp;lt;/ref&amp;gt; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Install Rails if you haven’t done it yet:&lt;br /&gt;
$ gem install rails&lt;br /&gt;
&lt;br /&gt;
# Create a new Rails application, “myapp_activejob”&lt;br /&gt;
# is the name of the application:&lt;br /&gt;
$ rails new myapp_activejob&lt;br /&gt;
&lt;br /&gt;
# Change your directory to myapp_activejob&lt;br /&gt;
$ cd myapp_activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Edit the Gemfile in the folder, add following into it:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'responders'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Run “ $ bundle update “ and make sure “$ rails server” works.&lt;br /&gt;
&lt;br /&gt;
==== 2. Resque setup&amp;lt;ref name= &amp;quot;Download_Redis&amp;quot;&amp;gt;Redis [http://redis.io/download “Download Redis” 2016]&amp;lt;/ref&amp;gt;====&lt;br /&gt;
We will use [https://github.com/resque/resque Resque] as the enqueuing backend. You need to install [http://redis.io/ Redis] before you can run Resque. You can get Redis using [http://brew.sh/ Homebrew]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ brew install redis&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can download, extract and compile Redis using:&amp;lt;ref name= &amp;quot;Download_Redis&amp;quot;&amp;gt;Redis [http://redis.io/download “Download Redis” 2016]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ wget http://download.redis.io/releases/redis-3.0.7.tar.gz&lt;br /&gt;
$ tar xzf redis-3.0.7.tar.gz&lt;br /&gt;
$ cd redis-3.0.7&lt;br /&gt;
$ make&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Redis does not officially support Microsoft [https://en.wikipedia.org/wiki/Microsoft_Windows Windows] platform, but you can find  [https://github.com/MSOpenTech/redis Redis on Windows] by Microsoft Open Tech group.&amp;lt;br&amp;gt;&lt;br /&gt;
Next get Resque be installed. To use resque with Active Job, we also need resque-scheduler.&lt;br /&gt;
Add the following into Gemfile, and run “$ bundle install”.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'resque'&lt;br /&gt;
gem 'resque-scheduler'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After the installation, create a Resque configuration file resque.rb in config/initializers/&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/initializers/resque.rb&lt;br /&gt;
&lt;br /&gt;
Resque.redis = Redis.new(host: 'localhost', post: 6379)&lt;br /&gt;
Resque.after_fork = Proc.new { ActiveRecord::Base.establish_connection }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As the Resque tasks and Resque Scheduler rake tasks are needed in this example, we need to create a resque.rake file in lib/tasks/:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#lib/tasks/resque.rake&lt;br /&gt;
&lt;br /&gt;
require 'resque/tasks'&lt;br /&gt;
require 'resque/scheduler/tasks'&lt;br /&gt;
&lt;br /&gt;
namespace :resque do&lt;br /&gt;
  task setup: :environment do&lt;br /&gt;
    ENV['TERM_CHILD'] ||= '1'&lt;br /&gt;
    ENV['QUEUE'] ||= '*'&lt;br /&gt;
    require 'resque'&lt;br /&gt;
    require 'resque-scheduler'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== 3. Creating a Mailer&amp;lt;ref name= &amp;quot;Action-Mailer-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/action_mailer_basics.html “Action Mailer Basics” 2014]&amp;lt;/ref&amp;gt; ====&lt;br /&gt;
By doing the things above, we already have our Rails project and the Resque for queuing get set. Next we will create a Mailer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate mailer user_mailer&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a method and a view for sending the email.&amp;lt;br&amp;gt;&lt;br /&gt;
In app/mailer/user_mailer.rb:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/mailers/user_mailer.rb&lt;br /&gt;
&lt;br /&gt;
class UserMailer &amp;lt; ActionMailer::Base&lt;br /&gt;
  default from: 'from@example.com'&lt;br /&gt;
&lt;br /&gt;
  def test_email(email)&lt;br /&gt;
    mail(&lt;br /&gt;
      to: email,&lt;br /&gt;
      subject: 'We are testing Active Job!'&lt;br /&gt;
    )&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In app/views/user_mailer/test_email.text&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/views/user_mailer/test_email.text&lt;br /&gt;
&lt;br /&gt;
Hey, we are testing Active Job!&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== 4. Active Job&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt;====&lt;br /&gt;
Create a configuration file active_job.rb in config/initializers/ , to set Resque as queue_adapter.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/initializers/active_job.rb&lt;br /&gt;
&lt;br /&gt;
ActiveJob::Base.queue_adapter = :resque&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a Job with generator.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job test_email&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the generated file app/jobs/test_email_job.rb, define the perform method and set it’s queue_as.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/jobs/test_email_job.rb&lt;br /&gt;
&lt;br /&gt;
class TestEmailJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :email&lt;br /&gt;
&lt;br /&gt;
  def perform(email)&lt;br /&gt;
    UserMailer.test_email(email).deliver_now&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can use a UserController to put the job into the queue for later execution. Here we set the email to be sent one minute later for test.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job test_email&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/controllers/users_controller.rb&lt;br /&gt;
&lt;br /&gt;
class UsersController &amp;lt; ApplicationController&lt;br /&gt;
  def new&lt;br /&gt;
    @user = User.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def create&lt;br /&gt;
    @user = User.create(user_params)&lt;br /&gt;
    TestEmailJob.new(@user.email).deliver_later!(wait: 1.minute)&lt;br /&gt;
    # redirect somewhere&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Routes and view need to set to make it work.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/routes.rb&lt;br /&gt;
&lt;br /&gt;
Rails.application.routes.draw do&lt;br /&gt;
  resources :users, only: [:new, :create]&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/views/users/new.html.erb&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= form_for @user do |f| %&amp;gt;&lt;br /&gt;
  &amp;lt;%= f.email_field :email %&amp;gt;&lt;br /&gt;
  &amp;lt;%= f.submit %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== 5. Try execution ====&lt;br /&gt;
Before we try the Mailer, we need MailCatcher for the test. Add following into Gemfile and “$ bundle install”&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'mailcatcher'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To set the environment, add following things into config/environments/development.rb.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/environments/development.rb&lt;br /&gt;
&lt;br /&gt;
Rails.application.configure do&lt;br /&gt;
  ...&lt;br /&gt;
  config.action_mailer.delivery_method = :smtp&lt;br /&gt;
  config.action_mailer.smtp_settings = { address: &amp;quot;localhost&amp;quot;, port: 1025 }&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run it!&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Start mailcatcher&lt;br /&gt;
$ mailcatcher&lt;br /&gt;
&lt;br /&gt;
# Start redis-server&lt;br /&gt;
$ redis-server&lt;br /&gt;
&lt;br /&gt;
# Start resque:work&lt;br /&gt;
$ bundle exec rake resque:work&lt;br /&gt;
&lt;br /&gt;
# Start resque:scheduler&lt;br /&gt;
$ rake environment resque:scheduler&lt;br /&gt;
&lt;br /&gt;
# Finally start rails server&lt;br /&gt;
$ rails server&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Open the browser, visit localhost:3000/users/new and sign up as a new user. One minute later you can see following in Resque scheduler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resque-scheduler: [INFO] 2016-02-05T22:53:15+09:00: Processing Delayed Items&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
And in MailCatcher:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
==&amp;gt; SMTP: Received message from '&amp;lt;from@example.com&amp;gt;' (315 bytes)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=='''References'''==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Schen35</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100764</id>
		<title>Active Job</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100764"/>
		<updated>2016-02-06T03:15:53Z</updated>

		<summary type="html">&lt;p&gt;Schen35: /* 2. Resque setupRedis “Download Redis” 2016 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
Active Job is a framework that helps developers writing codes and run them on the background automatically under different scenarios. It’s an interface that adapts different queueing backends like Backburner[https://github.com/nesquena/backburner], Delayed Job[https://github.com/collectiveidea/delayed_job], Qu[https://github.com/bkeepers/qu] and so on. Jobs can vary from schedule newsletter, follow-up emails to database housekeeping. &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” ]&amp;lt;/ref&amp;gt; Overall, Active Job is a interface which you can work with common queues.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
====Ruby On Rails versions support Active Job====&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;float:left;&amp;quot;&lt;br /&gt;
|+ Version history&lt;br /&gt;
|-&lt;br /&gt;
! Version !! Date&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || 2014/12/19&lt;br /&gt;
|-&lt;br /&gt;
| 4.2.5 || 2015/11/13&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
====Active Job adapters&amp;lt;ref name=&amp;quot;Active-Job-Adapter&amp;quot;&amp;gt;Active Job Adapter[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters.html&amp;quot;Active Job Adapter&amp;quot; ]&amp;lt;/ref&amp;gt;====&lt;br /&gt;
List of queueing backends Active Job support:&lt;br /&gt;
*Backburner[https://github.com/nesquena/backburner]&lt;br /&gt;
*Delayed Job[https://github.com/collectiveidea/delayed_job]&lt;br /&gt;
*Qu[https://github.com/bkeepers/qu]&lt;br /&gt;
*Que[https://github.com/chanks/que]&lt;br /&gt;
*queue_classic[https://github.com/QueueClassic/queue_classic]&lt;br /&gt;
*Resque 1.x[https://github.com/resque/resque]&lt;br /&gt;
*Sidekiq[http://sidekiq.org/]&lt;br /&gt;
*Sneakers[https://github.com/jondot/sneakers]&lt;br /&gt;
*Sucker Punch[https://github.com/brandonhilkert/sucker_punch]&lt;br /&gt;
*Active Job Async Job&lt;br /&gt;
*Active Job Inline[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters/InlineAdapter.html]&lt;br /&gt;
&lt;br /&gt;
=='''How to use &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/active_job_basics.html&amp;lt;/ref&amp;gt;'''==&lt;br /&gt;
We will introduce how to creating a job and and how to add the job into a queue. &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
=== Download ===&lt;br /&gt;
With RubyGems you can install Active Job:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gem install activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://github.com/rails/rails/tree/master/activejob Souce code of Active Job] available on GitHub, as part of Rails.&amp;lt;ref&amp;gt; GitHub [https://github.com/rails/rails/tree/master/activejob “Active Job -- Make work happen later” 2015] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Create a Job ===&lt;br /&gt;
In Active Job, a process 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. By doing the following, we created a Job called “update_wiki”.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki&lt;br /&gt;
  invoke  test_unit&lt;br /&gt;
  create  test/jobs/update_wiki_job_test.rb&lt;br /&gt;
  create  app/jobs/update_wiki_job.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Active Job provides the ability to run your Job on a specific queue by creating a job:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki --queue urgent&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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:&amp;lt;ref&amp;gt; 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] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ApplicationJob &amp;lt; ActiveJob::Base&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 4.2 a Job class defined a perform method and set a “queue_as” value:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :default&lt;br /&gt;
 &lt;br /&gt;
  def perform(*wiki)&lt;br /&gt;
   # Do something later&lt;br /&gt;
  wiki.update_contents&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There is a perform method to be called when the Job was first enqueued. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Adding a Job to the queue ===&lt;br /&gt;
If you wish your Job be processed as soon as the queuing system is free, you can enqueue a Job like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.perform_later wiki&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can add a Job be performed tomorrow at noon:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait_until: Date.tomorrow.noon).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you want your Job be performed a week from now, most of the queueing backends ( Sidekiq&lt;br /&gt;
, Delayed Job, etc. ) allow you to set a delay time.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait: 1.week).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Execution of Job ===&lt;br /&gt;
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]).&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt; Without setting any adapter, the job would be performed immediately.&amp;lt;br&amp;gt;&lt;br /&gt;
Queueing backend can be set at: /config/application.rb, in this example we use the Sidekiq.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    # Be sure to have the adapter's gem in your Gemfile and follow&lt;br /&gt;
    # the adapter's specific installation and deployment instructions.&lt;br /&gt;
    config.active_job.queue_adapter = :sidekiq&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Play with Queues ===&lt;br /&gt;
Active Job allows to schedule the job to be processed on a specific queue, this became helpful as common adapters support multiple queues.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Queue name can be prefixed for all jobs using config.active_job.queue_name_prefixin application.rb:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# config/application.rb&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    config.active_job.queue_name_prefix = Rails.env&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# app/jobs/update_wiki_job.rb&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# Now your job will run on queue production_low_priority on your&lt;br /&gt;
# production environment and on staging_low_priority on your staging&lt;br /&gt;
# environment&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can control a job to run on a queue you like, by passing a :queue option to #set :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
MyJob.set(queue: :another_queue).perform_later(record)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the code snippets are referred from the source&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
In this section, a example is provided to demonstrate how to use ActiveJob in practice.&lt;br /&gt;
=== Background Mail Sender ===&lt;br /&gt;
You can send emails asynchronously with Action Mailer which Active Job is already integrated in&amp;lt;ref name= &amp;quot;Action-Mailer-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/action_mailer_basics.html “Action Mailer Basics” 2014]&amp;lt;/ref&amp;gt;. Here in this example, we try to use Active Job with Action Mailer.&amp;lt;br&amp;gt;&lt;br /&gt;
==== 1. Getting started with Rails&amp;lt;ref name= &amp;quot;Welcome_to_Rails&amp;quot;&amp;gt;GitHub [https://github.com/rails/rails “Welcome to Rails” 2015]&amp;lt;/ref&amp;gt; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Install Rails if you haven’t done it yet:&lt;br /&gt;
$ gem install rails&lt;br /&gt;
&lt;br /&gt;
# Create a new Rails application, “myapp_activejob”&lt;br /&gt;
# is the name of the application:&lt;br /&gt;
$ rails new myapp_activejob&lt;br /&gt;
&lt;br /&gt;
# Change your directory to myapp_activejob&lt;br /&gt;
$ cd myapp_activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Edit the Gemfile in the folder, add following into it:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'responders'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Run “ $ bundle update “ and make sure “$ rails server” works.&lt;br /&gt;
&lt;br /&gt;
==== 2. Resque setup&amp;lt;ref name= &amp;quot;Download_Redis&amp;quot;&amp;gt;Redis [http://redis.io/download “Download Redis” 2016]&amp;lt;/ref&amp;gt;====&lt;br /&gt;
We will use [https://github.com/resque/resque Resque] as the enqueuing backend. You need to install [http://redis.io/ Redis] before you can run Resque. You can get Redis using [http://brew.sh/ Homebrew]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ brew install redis&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can download, extract and compile Redis using:&amp;lt;ref name= &amp;quot;Download_Redis&amp;quot;&amp;gt;Redis [http://redis.io/download “Download Redis” 2016]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ wget http://download.redis.io/releases/redis-3.0.7.tar.gz&lt;br /&gt;
$ tar xzf redis-3.0.7.tar.gz&lt;br /&gt;
$ cd redis-3.0.7&lt;br /&gt;
$ make&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Redis does not officially support Microsoft [https://en.wikipedia.org/wiki/Microsoft_Windows Windows] platform, but you can find  [https://github.com/MSOpenTech/redis Redis on Windows] by Microsoft Open Tech group.&amp;lt;br&amp;gt;&lt;br /&gt;
Next get Resque be installed. To use resque with Active Job, we also need resque-scheduler.&lt;br /&gt;
Add the following into Gemfile, and run “$ bundle install”.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'resque'&lt;br /&gt;
gem 'resque-scheduler'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After the installation, create a Resque configuration file resque.rb in config/initializers/&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/initializers/resque.rb&lt;br /&gt;
&lt;br /&gt;
Resque.redis = Redis.new(host: 'localhost', post: 6379)&lt;br /&gt;
Resque.after_fork = Proc.new { ActiveRecord::Base.establish_connection }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As the Resque tasks and Resque Scheduler rake tasks are needed in this example, we need to create a resque.rake file in lib/tasks/:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#lib/tasks/resque.rake&lt;br /&gt;
&lt;br /&gt;
require 'resque/tasks'&lt;br /&gt;
require 'resque/scheduler/tasks'&lt;br /&gt;
&lt;br /&gt;
namespace :resque do&lt;br /&gt;
  task setup: :environment do&lt;br /&gt;
    ENV['TERM_CHILD'] ||= '1'&lt;br /&gt;
    ENV['QUEUE'] ||= '*'&lt;br /&gt;
    require 'resque'&lt;br /&gt;
    require 'resque-scheduler'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== 3. Creating a Mailer&amp;lt;ref name= &amp;quot;Action-Mailer-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/action_mailer_basics.html “Action Mailer Basics” 2014]&amp;lt;/ref&amp;gt; ====&lt;br /&gt;
By doing the things above, we already have our Rails project and the Resque for queuing get set. Next we will create a Mailer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate mailer user_mailer&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a method and a view for sending the email.&amp;lt;br&amp;gt;&lt;br /&gt;
In app/mailer/user_mailer.rb:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/mailers/user_mailer.rb&lt;br /&gt;
&lt;br /&gt;
class UserMailer &amp;lt; ActionMailer::Base&lt;br /&gt;
  default from: 'from@example.com'&lt;br /&gt;
&lt;br /&gt;
  def test_email(email)&lt;br /&gt;
    mail(&lt;br /&gt;
      to: email,&lt;br /&gt;
      subject: 'We are testing Active Job!'&lt;br /&gt;
    )&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In app/views/user_mailer/test_email.text&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/views/user_mailer/test_email.text&lt;br /&gt;
&lt;br /&gt;
Hey, we are testing Active Job!&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== 4. Active Job&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt;====&lt;br /&gt;
Create a configuration file active_job.rb in config/initializers/ , to set Resque as queue_adapter.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/initializers/active_job.rb&lt;br /&gt;
&lt;br /&gt;
ActiveJob::Base.queue_adapter = :resque&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a Job with generator.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job test_email&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the generated file app/jobs/test_email_job.rb, define the perform method and set it’s queue_as.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/jobs/test_email_job.rb&lt;br /&gt;
&lt;br /&gt;
class TestEmailJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :email&lt;br /&gt;
&lt;br /&gt;
  def perform(email)&lt;br /&gt;
    UserMailer.test_email(email).deliver_now&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can use a UserController to put the job into the queue for later execution. Here we set the email to be sent one minute later for test.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job test_email&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/controllers/users_controller.rb&lt;br /&gt;
&lt;br /&gt;
class UsersController &amp;lt; ApplicationController&lt;br /&gt;
  def new&lt;br /&gt;
    @user = User.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def create&lt;br /&gt;
    @user = User.create(user_params)&lt;br /&gt;
    TestEmailJob.new(@user.email).deliver_later!(wait: 1.minute)&lt;br /&gt;
    # redirect somewhere&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Routes and view need to set to make it work.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/routes.rb&lt;br /&gt;
&lt;br /&gt;
Rails.application.routes.draw do&lt;br /&gt;
  resources :users, only: [:new, :create]&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/views/users/new.html.erb&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= form_for @user do |f| %&amp;gt;&lt;br /&gt;
  &amp;lt;%= f.email_field :email %&amp;gt;&lt;br /&gt;
  &amp;lt;%= f.submit %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== 5. Try execution ====&lt;br /&gt;
Before we try the Mailer, we need MailCatcher for the test. Add following into Gemfile and “$ bundle install”&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'mailcatcher'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To set the environment, add following things into config/environments/development.rb.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/environments/development.rb&lt;br /&gt;
&lt;br /&gt;
Rails.application.configure do&lt;br /&gt;
  ...&lt;br /&gt;
  config.action_mailer.delivery_method = :smtp&lt;br /&gt;
  config.action_mailer.smtp_settings = { address: &amp;quot;localhost&amp;quot;, port: 1025 }&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run it!&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Start mailcatcher&lt;br /&gt;
$ mailcatcher&lt;br /&gt;
&lt;br /&gt;
# Start redis-server&lt;br /&gt;
$ redis-server&lt;br /&gt;
&lt;br /&gt;
# Start resque:work&lt;br /&gt;
$ bundle exec rake resque:work&lt;br /&gt;
&lt;br /&gt;
# Start resque:scheduler&lt;br /&gt;
$ rake environment resque:scheduler&lt;br /&gt;
&lt;br /&gt;
# Finally start rails server&lt;br /&gt;
$ rails server&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Open the browser, visit localhost:3000/users/new and sign up as a new user. One minute later you can see following in Resque scheduler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resque-scheduler: [INFO] 2016-02-05T22:53:15+09:00: Processing Delayed Items&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
And in MailCatcher:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
==&amp;gt; SMTP: Received message from '&amp;lt;from@example.com&amp;gt;' (315 bytes)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=='''References'''==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Schen35</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100748</id>
		<title>Active Job</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100748"/>
		<updated>2016-02-05T22:29:19Z</updated>

		<summary type="html">&lt;p&gt;Schen35: /* 1. Getting started with Rails:GitHub “Welcome to Rails” 2015 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
Active Job is a framework that helps 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 an interface that adapts different queueing backends like Backburner[https://github.com/nesquena/backburner], Delayed Job[https://github.com/collectiveidea/delayed_job], Qu[https://github.com/bkeepers/qu] and so on. &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” ]&amp;lt;/ref&amp;gt; Overall, Active Job is a interface which you can work with common queues.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
====Ruby On Rails versions support Active Job====&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;float:left;&amp;quot;&lt;br /&gt;
|+ Version history&lt;br /&gt;
|-&lt;br /&gt;
! Version !! Date&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || 2014/12/19&lt;br /&gt;
|-&lt;br /&gt;
| 4.2.5 || 2015/11/13&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
====Active Job adapters&amp;lt;ref name=&amp;quot;Active-Job-Adapter&amp;quot;&amp;gt;Active Job Adapter[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters.html&amp;quot;Active Job Adapter&amp;quot; ]&amp;lt;/ref&amp;gt;====&lt;br /&gt;
*Backburner[https://github.com/nesquena/backburner]&lt;br /&gt;
*Delayed Job[https://github.com/collectiveidea/delayed_job]&lt;br /&gt;
*Qu[https://github.com/bkeepers/qu]&lt;br /&gt;
*Que[https://github.com/chanks/que]&lt;br /&gt;
*queue_classic[https://github.com/QueueClassic/queue_classic]&lt;br /&gt;
*Resque 1.x[https://github.com/resque/resque]&lt;br /&gt;
*Sidekiq[http://sidekiq.org/]&lt;br /&gt;
*Sneakers[https://github.com/jondot/sneakers]&lt;br /&gt;
*Sucker Punch[https://github.com/brandonhilkert/sucker_punch]&lt;br /&gt;
*Active Job Async Job&lt;br /&gt;
*Active Job Inline[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters/InlineAdapter.html]&lt;br /&gt;
&lt;br /&gt;
=='''How to use &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/active_job_basics.html&amp;lt;/ref&amp;gt;'''==&lt;br /&gt;
We will introduce how to creating a job and and how to add the job into a queue. &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
=== Download ===&lt;br /&gt;
With RubyGems you can install the Active Job into your computer:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gem install activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://github.com/rails/rails/tree/master/activejob Souce code of Active Job] available on GitHub, as part of Rails.&amp;lt;ref&amp;gt; GitHub [https://github.com/rails/rails/tree/master/activejob “Active Job -- Make work happen later” 2015] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Create a Job ===&lt;br /&gt;
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.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki&lt;br /&gt;
  invoke  test_unit&lt;br /&gt;
  create  test/jobs/update_wiki_job_test.rb&lt;br /&gt;
  create  app/jobs/update_wiki_job.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Active Job provides the ability to run your Job on a specific queue by create a job as following:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki --queue urgent&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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:&amp;lt;ref&amp;gt; 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] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ApplicationJob &amp;lt; ActiveJob::Base&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 4.2 a Job class defined a perform method and set a “queue_as” value:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :default&lt;br /&gt;
 &lt;br /&gt;
  def perform(*wiki)&lt;br /&gt;
   # Do something later&lt;br /&gt;
  wiki.update_contents&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There is a perform method to be called when the job was first enqueued. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Adding a Job to the queue ===&lt;br /&gt;
If you wish your Job be processed as soon as the queuing system is free, you can enqueue a Job like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.perform_later wiki&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can add a Job be performed tomorrow at noon:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait_until: Date.tomorrow.noon).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you want your Job be performed a week from now, most of the queueing backends ( Sidekiq&lt;br /&gt;
, Delayed Job, etc. ) allow you to set a delay time.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait: 1.week).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Execution of Job ===&lt;br /&gt;
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]).&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt; Without setting any adapter, the job would be performed immediately.&amp;lt;br&amp;gt;&lt;br /&gt;
Queueing backend can be set at: /config/application.rb, in this example we use the Sidekiq.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    # Be sure to have the adapter's gem in your Gemfile and follow&lt;br /&gt;
    # the adapter's specific installation and deployment instructions.&lt;br /&gt;
    config.active_job.queue_adapter = :sidekiq&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Play with Queues ===&lt;br /&gt;
Active Job allows to schedule the job to be processed on a specific queue, this became helpful as common adapters support multiple queues.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Queue name can be prefixed for all jobs using config.active_job.queue_name_prefixin application.rb:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# config/application.rb&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    config.active_job.queue_name_prefix = Rails.env&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# app/jobs/update_wiki_job.rb&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# Now your job will run on queue production_low_priority on your&lt;br /&gt;
# production environment and on staging_low_priority on your staging&lt;br /&gt;
# environment&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can control a job to run on a queue you like, by passing a :queue option to #set :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
MyJob.set(queue: :another_queue).perform_later(record)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the code snippets are referred from the source&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
In this section, some examples are provided to demonstrate how to use ActiveJob in practices.&lt;br /&gt;
=== Background Mail Sender ===&lt;br /&gt;
You can send emails asynchronously with Action Mailer which Active Job is already integrated in&amp;lt;ref name= &amp;quot;Action-Mailer-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/action_mailer_basics.html “Action Mailer Basics” 2014]&amp;lt;/ref&amp;gt;. Here in this example, we try to use Active Job with Action Mailer.&amp;lt;br&amp;gt;&lt;br /&gt;
==== 1. Getting started with Rails&amp;lt;ref name= &amp;quot;Welcome_to_Rails&amp;quot;&amp;gt;GitHub [https://github.com/rails/rails “Welcome to Rails” 2015]&amp;lt;/ref&amp;gt; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Install Rails if you haven’t done it yet:&lt;br /&gt;
$ gem install rails&lt;br /&gt;
&lt;br /&gt;
# Create a new Rails application, “myapp_activejob”&lt;br /&gt;
# is the name of the application:&lt;br /&gt;
$ rails new myapp_activejob&lt;br /&gt;
&lt;br /&gt;
# Change your directory to myapp_activejob&lt;br /&gt;
$ cd myapp_activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Edit the Gemfile in the folder, add following into it:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'responders'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Run “ $ bundle update “ and make sure “$ rails server” works.&lt;br /&gt;
&lt;br /&gt;
==== 2. Resque setup&amp;lt;ref name= &amp;quot;Download_Redis&amp;quot;&amp;gt;Redis [http://redis.io/download “Download Redis” 2016]&amp;lt;/ref&amp;gt;====&lt;br /&gt;
We will use [https://github.com/resque/resque Resque] as the enqueuing backend. You need to install [http://redis.io/ Redis] before you can run Resque. You can getting Redis with [http://brew.sh/ Homebrew]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ brew install redis&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can download, extract and compile Redis with:&amp;lt;ref name= &amp;quot;Download_Redis&amp;quot;&amp;gt;Redis [http://redis.io/download “Download Redis” 2016]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ wget http://download.redis.io/releases/redis-3.0.7.tar.gz&lt;br /&gt;
$ tar xzf redis-3.0.7.tar.gz&lt;br /&gt;
$ cd redis-3.0.7&lt;br /&gt;
$ make&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Redis does not officially support Windows, but you can find  [https://github.com/MSOpenTech/redis Redis on Windows] by Microsoft Open Tech group.&amp;lt;br&amp;gt;&lt;br /&gt;
Next get Resque be installed. To use resque with Active Job, we also need resque-scheduler.&lt;br /&gt;
Add the following into Gemfile, and run “$ bundle install”.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'resque'&lt;br /&gt;
gem 'resque-scheduler'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After the installation, create a Resque configuration file resque.rb in config/initializers/&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/initializers/resque.rb&lt;br /&gt;
&lt;br /&gt;
Resque.redis = Redis.new(host: 'localhost', post: 6379)&lt;br /&gt;
Resque.after_fork = Proc.new { ActiveRecord::Base.establish_connection }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As the Resque tasks and Resque Scheduler rake tasks are needed in this example, we need to create a resque.rake file in lib/tasks/:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#lib/tasks/resque.rake&lt;br /&gt;
&lt;br /&gt;
require 'resque/tasks'&lt;br /&gt;
require 'resque/scheduler/tasks'&lt;br /&gt;
&lt;br /&gt;
namespace :resque do&lt;br /&gt;
  task setup: :environment do&lt;br /&gt;
    ENV['TERM_CHILD'] ||= '1'&lt;br /&gt;
    ENV['QUEUE'] ||= '*'&lt;br /&gt;
    require 'resque'&lt;br /&gt;
    require 'resque-scheduler'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== 3. Creating a Mailer&amp;lt;ref name= &amp;quot;Action-Mailer-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/action_mailer_basics.html “Action Mailer Basics” 2014]&amp;lt;/ref&amp;gt; ====&lt;br /&gt;
By doing the things above, we already have our Rails project and the Resque for queuing get set. Next we will creat a Mailer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate mailer user_mailer&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a method and a view for sending email.&amp;lt;br&amp;gt;&lt;br /&gt;
In app/mailer/user_mailer.rb:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/mailers/user_mailer.rb&lt;br /&gt;
&lt;br /&gt;
class UserMailer &amp;lt; ActionMailer::Base&lt;br /&gt;
  default from: 'from@example.com'&lt;br /&gt;
&lt;br /&gt;
  def test_email(email)&lt;br /&gt;
    mail(&lt;br /&gt;
      to: email,&lt;br /&gt;
      subject: 'We are testing Active Job!'&lt;br /&gt;
    )&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In app/views/user_mailer/test_email.text&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/views/user_mailer/test_email.text&lt;br /&gt;
&lt;br /&gt;
Hey, we are testing Active Job!&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== 4. Active Job&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt;====&lt;br /&gt;
Create a configuration file active_job.rb in config/initializers/ , to set Resque as queue_adapter.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/initializers/active_job.rb&lt;br /&gt;
&lt;br /&gt;
ActiveJob::Base.queue_adapter = :resque&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a Job with generator.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job test_email&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the generated file app/jobs/test_email_job.rb, define the perform method and set it’s queue_as.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/jobs/test_email_job.rb&lt;br /&gt;
&lt;br /&gt;
class TestEmailJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :email&lt;br /&gt;
&lt;br /&gt;
  def perform(email)&lt;br /&gt;
    UserMailer.test_email(email).deliver_now&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can use a UserController to put the job into the queue for later execution. Here we set the email to be sent one minute later for test.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job test_email&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/controllers/users_controller.rb&lt;br /&gt;
&lt;br /&gt;
class UsersController &amp;lt; ApplicationController&lt;br /&gt;
  def new&lt;br /&gt;
    @user = User.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def create&lt;br /&gt;
    @user = User.create(user_params)&lt;br /&gt;
    TestEmailJob.new(@user.email).deliver_later!(wait: 1.minute)&lt;br /&gt;
    # redirect somewhere&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Routes and view need to set to make it work.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/routes.rb&lt;br /&gt;
&lt;br /&gt;
Rails.application.routes.draw do&lt;br /&gt;
  resources :users, only: [:new, :create]&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/views/users/new.html.erb&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= form_for @user do |f| %&amp;gt;&lt;br /&gt;
  &amp;lt;%= f.email_field :email %&amp;gt;&lt;br /&gt;
  &amp;lt;%= f.submit %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== 5. Try execution ====&lt;br /&gt;
Before we try the mailer, we need a “mailcatcher” for the test. Add following into Gemfile and “$ bundle install”&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'mailcatcher'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To set the environment, add following things into config/environments/development.rb.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/environments/development.rb&lt;br /&gt;
&lt;br /&gt;
Rails.application.configure do&lt;br /&gt;
  ...&lt;br /&gt;
  config.action_mailer.delivery_method = :smtp&lt;br /&gt;
  config.action_mailer.smtp_settings = { address: &amp;quot;localhost&amp;quot;, port: 1025 }&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run it!&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Start mailcatcher&lt;br /&gt;
$ mailcatcher&lt;br /&gt;
&lt;br /&gt;
# Start redis-server&lt;br /&gt;
$ redis-server&lt;br /&gt;
&lt;br /&gt;
# Start resque:work&lt;br /&gt;
$ bundle exec rake resque:work&lt;br /&gt;
&lt;br /&gt;
# Start resque:scheduler&lt;br /&gt;
$ rake environment resque:scheduler&lt;br /&gt;
&lt;br /&gt;
# Finally start rails server&lt;br /&gt;
$ rails server&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Open the browser, visit localhost:3000/users/new and sign up as a new user. One minute later you can see following in Resque scheduler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resque-scheduler: [INFO] 2016-02-05T22:53:15+09:00: Processing Delayed Items&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
And in mailcatcher:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
==&amp;gt; SMTP: Received message from '&amp;lt;from@example.com&amp;gt;' (315 bytes)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Yeah!&lt;br /&gt;
&lt;br /&gt;
=='''References'''==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Schen35</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100747</id>
		<title>Active Job</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100747"/>
		<updated>2016-02-05T22:28:59Z</updated>

		<summary type="html">&lt;p&gt;Schen35: /* Background Mail Sender */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
Active Job is a framework that helps 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 an interface that adapts different queueing backends like Backburner[https://github.com/nesquena/backburner], Delayed Job[https://github.com/collectiveidea/delayed_job], Qu[https://github.com/bkeepers/qu] and so on. &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” ]&amp;lt;/ref&amp;gt; Overall, Active Job is a interface which you can work with common queues.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
====Ruby On Rails versions support Active Job====&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;float:left;&amp;quot;&lt;br /&gt;
|+ Version history&lt;br /&gt;
|-&lt;br /&gt;
! Version !! Date&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || 2014/12/19&lt;br /&gt;
|-&lt;br /&gt;
| 4.2.5 || 2015/11/13&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
====Active Job adapters&amp;lt;ref name=&amp;quot;Active-Job-Adapter&amp;quot;&amp;gt;Active Job Adapter[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters.html&amp;quot;Active Job Adapter&amp;quot; ]&amp;lt;/ref&amp;gt;====&lt;br /&gt;
*Backburner[https://github.com/nesquena/backburner]&lt;br /&gt;
*Delayed Job[https://github.com/collectiveidea/delayed_job]&lt;br /&gt;
*Qu[https://github.com/bkeepers/qu]&lt;br /&gt;
*Que[https://github.com/chanks/que]&lt;br /&gt;
*queue_classic[https://github.com/QueueClassic/queue_classic]&lt;br /&gt;
*Resque 1.x[https://github.com/resque/resque]&lt;br /&gt;
*Sidekiq[http://sidekiq.org/]&lt;br /&gt;
*Sneakers[https://github.com/jondot/sneakers]&lt;br /&gt;
*Sucker Punch[https://github.com/brandonhilkert/sucker_punch]&lt;br /&gt;
*Active Job Async Job&lt;br /&gt;
*Active Job Inline[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters/InlineAdapter.html]&lt;br /&gt;
&lt;br /&gt;
=='''How to use &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/active_job_basics.html&amp;lt;/ref&amp;gt;'''==&lt;br /&gt;
We will introduce how to creating a job and and how to add the job into a queue. &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
=== Download ===&lt;br /&gt;
With RubyGems you can install the Active Job into your computer:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gem install activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://github.com/rails/rails/tree/master/activejob Souce code of Active Job] available on GitHub, as part of Rails.&amp;lt;ref&amp;gt; GitHub [https://github.com/rails/rails/tree/master/activejob “Active Job -- Make work happen later” 2015] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Create a Job ===&lt;br /&gt;
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.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki&lt;br /&gt;
  invoke  test_unit&lt;br /&gt;
  create  test/jobs/update_wiki_job_test.rb&lt;br /&gt;
  create  app/jobs/update_wiki_job.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Active Job provides the ability to run your Job on a specific queue by create a job as following:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki --queue urgent&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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:&amp;lt;ref&amp;gt; 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] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ApplicationJob &amp;lt; ActiveJob::Base&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 4.2 a Job class defined a perform method and set a “queue_as” value:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :default&lt;br /&gt;
 &lt;br /&gt;
  def perform(*wiki)&lt;br /&gt;
   # Do something later&lt;br /&gt;
  wiki.update_contents&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There is a perform method to be called when the job was first enqueued. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Adding a Job to the queue ===&lt;br /&gt;
If you wish your Job be processed as soon as the queuing system is free, you can enqueue a Job like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.perform_later wiki&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can add a Job be performed tomorrow at noon:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait_until: Date.tomorrow.noon).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you want your Job be performed a week from now, most of the queueing backends ( Sidekiq&lt;br /&gt;
, Delayed Job, etc. ) allow you to set a delay time.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait: 1.week).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Execution of Job ===&lt;br /&gt;
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]).&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt; Without setting any adapter, the job would be performed immediately.&amp;lt;br&amp;gt;&lt;br /&gt;
Queueing backend can be set at: /config/application.rb, in this example we use the Sidekiq.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    # Be sure to have the adapter's gem in your Gemfile and follow&lt;br /&gt;
    # the adapter's specific installation and deployment instructions.&lt;br /&gt;
    config.active_job.queue_adapter = :sidekiq&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Play with Queues ===&lt;br /&gt;
Active Job allows to schedule the job to be processed on a specific queue, this became helpful as common adapters support multiple queues.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Queue name can be prefixed for all jobs using config.active_job.queue_name_prefixin application.rb:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# config/application.rb&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    config.active_job.queue_name_prefix = Rails.env&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# app/jobs/update_wiki_job.rb&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# Now your job will run on queue production_low_priority on your&lt;br /&gt;
# production environment and on staging_low_priority on your staging&lt;br /&gt;
# environment&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can control a job to run on a queue you like, by passing a :queue option to #set :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
MyJob.set(queue: :another_queue).perform_later(record)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the code snippets are referred from the source&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
In this section, some examples are provided to demonstrate how to use ActiveJob in practices.&lt;br /&gt;
=== Background Mail Sender ===&lt;br /&gt;
You can send emails asynchronously with Action Mailer which Active Job is already integrated in&amp;lt;ref name= &amp;quot;Action-Mailer-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/action_mailer_basics.html “Action Mailer Basics” 2014]&amp;lt;/ref&amp;gt;. Here in this example, we try to use Active Job with Action Mailer.&amp;lt;br&amp;gt;&lt;br /&gt;
==== 1. Getting started with Rails:&amp;lt;ref name= &amp;quot;Welcome_to_Rails&amp;quot;&amp;gt;GitHub [https://github.com/rails/rails “Welcome to Rails” 2015]&amp;lt;/ref&amp;gt; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Install Rails if you haven’t done it yet:&lt;br /&gt;
$ gem install rails&lt;br /&gt;
&lt;br /&gt;
# Create a new Rails application, “myapp_activejob”&lt;br /&gt;
# is the name of the application:&lt;br /&gt;
$ rails new myapp_activejob&lt;br /&gt;
&lt;br /&gt;
# Change your directory to myapp_activejob&lt;br /&gt;
$ cd myapp_activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Edit the Gemfile in the folder, add following into it:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'responders'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Run “ $ bundle update “ and make sure “$ rails server” works.&lt;br /&gt;
&lt;br /&gt;
==== 2. Resque setup&amp;lt;ref name= &amp;quot;Download_Redis&amp;quot;&amp;gt;Redis [http://redis.io/download “Download Redis” 2016]&amp;lt;/ref&amp;gt;====&lt;br /&gt;
We will use [https://github.com/resque/resque Resque] as the enqueuing backend. You need to install [http://redis.io/ Redis] before you can run Resque. You can getting Redis with [http://brew.sh/ Homebrew]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ brew install redis&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can download, extract and compile Redis with:&amp;lt;ref name= &amp;quot;Download_Redis&amp;quot;&amp;gt;Redis [http://redis.io/download “Download Redis” 2016]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ wget http://download.redis.io/releases/redis-3.0.7.tar.gz&lt;br /&gt;
$ tar xzf redis-3.0.7.tar.gz&lt;br /&gt;
$ cd redis-3.0.7&lt;br /&gt;
$ make&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Redis does not officially support Windows, but you can find  [https://github.com/MSOpenTech/redis Redis on Windows] by Microsoft Open Tech group.&amp;lt;br&amp;gt;&lt;br /&gt;
Next get Resque be installed. To use resque with Active Job, we also need resque-scheduler.&lt;br /&gt;
Add the following into Gemfile, and run “$ bundle install”.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'resque'&lt;br /&gt;
gem 'resque-scheduler'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After the installation, create a Resque configuration file resque.rb in config/initializers/&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/initializers/resque.rb&lt;br /&gt;
&lt;br /&gt;
Resque.redis = Redis.new(host: 'localhost', post: 6379)&lt;br /&gt;
Resque.after_fork = Proc.new { ActiveRecord::Base.establish_connection }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As the Resque tasks and Resque Scheduler rake tasks are needed in this example, we need to create a resque.rake file in lib/tasks/:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#lib/tasks/resque.rake&lt;br /&gt;
&lt;br /&gt;
require 'resque/tasks'&lt;br /&gt;
require 'resque/scheduler/tasks'&lt;br /&gt;
&lt;br /&gt;
namespace :resque do&lt;br /&gt;
  task setup: :environment do&lt;br /&gt;
    ENV['TERM_CHILD'] ||= '1'&lt;br /&gt;
    ENV['QUEUE'] ||= '*'&lt;br /&gt;
    require 'resque'&lt;br /&gt;
    require 'resque-scheduler'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== 3. Creating a Mailer&amp;lt;ref name= &amp;quot;Action-Mailer-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/action_mailer_basics.html “Action Mailer Basics” 2014]&amp;lt;/ref&amp;gt; ====&lt;br /&gt;
By doing the things above, we already have our Rails project and the Resque for queuing get set. Next we will creat a Mailer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate mailer user_mailer&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a method and a view for sending email.&amp;lt;br&amp;gt;&lt;br /&gt;
In app/mailer/user_mailer.rb:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/mailers/user_mailer.rb&lt;br /&gt;
&lt;br /&gt;
class UserMailer &amp;lt; ActionMailer::Base&lt;br /&gt;
  default from: 'from@example.com'&lt;br /&gt;
&lt;br /&gt;
  def test_email(email)&lt;br /&gt;
    mail(&lt;br /&gt;
      to: email,&lt;br /&gt;
      subject: 'We are testing Active Job!'&lt;br /&gt;
    )&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In app/views/user_mailer/test_email.text&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/views/user_mailer/test_email.text&lt;br /&gt;
&lt;br /&gt;
Hey, we are testing Active Job!&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== 4. Active Job&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt;====&lt;br /&gt;
Create a configuration file active_job.rb in config/initializers/ , to set Resque as queue_adapter.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/initializers/active_job.rb&lt;br /&gt;
&lt;br /&gt;
ActiveJob::Base.queue_adapter = :resque&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a Job with generator.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job test_email&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the generated file app/jobs/test_email_job.rb, define the perform method and set it’s queue_as.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/jobs/test_email_job.rb&lt;br /&gt;
&lt;br /&gt;
class TestEmailJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :email&lt;br /&gt;
&lt;br /&gt;
  def perform(email)&lt;br /&gt;
    UserMailer.test_email(email).deliver_now&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can use a UserController to put the job into the queue for later execution. Here we set the email to be sent one minute later for test.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job test_email&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/controllers/users_controller.rb&lt;br /&gt;
&lt;br /&gt;
class UsersController &amp;lt; ApplicationController&lt;br /&gt;
  def new&lt;br /&gt;
    @user = User.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def create&lt;br /&gt;
    @user = User.create(user_params)&lt;br /&gt;
    TestEmailJob.new(@user.email).deliver_later!(wait: 1.minute)&lt;br /&gt;
    # redirect somewhere&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Routes and view need to set to make it work.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/routes.rb&lt;br /&gt;
&lt;br /&gt;
Rails.application.routes.draw do&lt;br /&gt;
  resources :users, only: [:new, :create]&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/views/users/new.html.erb&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= form_for @user do |f| %&amp;gt;&lt;br /&gt;
  &amp;lt;%= f.email_field :email %&amp;gt;&lt;br /&gt;
  &amp;lt;%= f.submit %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== 5. Try execution ====&lt;br /&gt;
Before we try the mailer, we need a “mailcatcher” for the test. Add following into Gemfile and “$ bundle install”&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'mailcatcher'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To set the environment, add following things into config/environments/development.rb.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/environments/development.rb&lt;br /&gt;
&lt;br /&gt;
Rails.application.configure do&lt;br /&gt;
  ...&lt;br /&gt;
  config.action_mailer.delivery_method = :smtp&lt;br /&gt;
  config.action_mailer.smtp_settings = { address: &amp;quot;localhost&amp;quot;, port: 1025 }&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run it!&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Start mailcatcher&lt;br /&gt;
$ mailcatcher&lt;br /&gt;
&lt;br /&gt;
# Start redis-server&lt;br /&gt;
$ redis-server&lt;br /&gt;
&lt;br /&gt;
# Start resque:work&lt;br /&gt;
$ bundle exec rake resque:work&lt;br /&gt;
&lt;br /&gt;
# Start resque:scheduler&lt;br /&gt;
$ rake environment resque:scheduler&lt;br /&gt;
&lt;br /&gt;
# Finally start rails server&lt;br /&gt;
$ rails server&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Open the browser, visit localhost:3000/users/new and sign up as a new user. One minute later you can see following in Resque scheduler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resque-scheduler: [INFO] 2016-02-05T22:53:15+09:00: Processing Delayed Items&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
And in mailcatcher:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
==&amp;gt; SMTP: Received message from '&amp;lt;from@example.com&amp;gt;' (315 bytes)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Yeah!&lt;br /&gt;
&lt;br /&gt;
=='''References'''==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Schen35</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100746</id>
		<title>Active Job</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100746"/>
		<updated>2016-02-05T22:28:38Z</updated>

		<summary type="html">&lt;p&gt;Schen35: /* Examples */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
Active Job is a framework that helps 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 an interface that adapts different queueing backends like Backburner[https://github.com/nesquena/backburner], Delayed Job[https://github.com/collectiveidea/delayed_job], Qu[https://github.com/bkeepers/qu] and so on. &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” ]&amp;lt;/ref&amp;gt; Overall, Active Job is a interface which you can work with common queues.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
====Ruby On Rails versions support Active Job====&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;float:left;&amp;quot;&lt;br /&gt;
|+ Version history&lt;br /&gt;
|-&lt;br /&gt;
! Version !! Date&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || 2014/12/19&lt;br /&gt;
|-&lt;br /&gt;
| 4.2.5 || 2015/11/13&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
====Active Job adapters&amp;lt;ref name=&amp;quot;Active-Job-Adapter&amp;quot;&amp;gt;Active Job Adapter[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters.html&amp;quot;Active Job Adapter&amp;quot; ]&amp;lt;/ref&amp;gt;====&lt;br /&gt;
*Backburner[https://github.com/nesquena/backburner]&lt;br /&gt;
*Delayed Job[https://github.com/collectiveidea/delayed_job]&lt;br /&gt;
*Qu[https://github.com/bkeepers/qu]&lt;br /&gt;
*Que[https://github.com/chanks/que]&lt;br /&gt;
*queue_classic[https://github.com/QueueClassic/queue_classic]&lt;br /&gt;
*Resque 1.x[https://github.com/resque/resque]&lt;br /&gt;
*Sidekiq[http://sidekiq.org/]&lt;br /&gt;
*Sneakers[https://github.com/jondot/sneakers]&lt;br /&gt;
*Sucker Punch[https://github.com/brandonhilkert/sucker_punch]&lt;br /&gt;
*Active Job Async Job&lt;br /&gt;
*Active Job Inline[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters/InlineAdapter.html]&lt;br /&gt;
&lt;br /&gt;
=='''How to use &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/active_job_basics.html&amp;lt;/ref&amp;gt;'''==&lt;br /&gt;
We will introduce how to creating a job and and how to add the job into a queue. &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
=== Download ===&lt;br /&gt;
With RubyGems you can install the Active Job into your computer:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gem install activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://github.com/rails/rails/tree/master/activejob Souce code of Active Job] available on GitHub, as part of Rails.&amp;lt;ref&amp;gt; GitHub [https://github.com/rails/rails/tree/master/activejob “Active Job -- Make work happen later” 2015] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Create a Job ===&lt;br /&gt;
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.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki&lt;br /&gt;
  invoke  test_unit&lt;br /&gt;
  create  test/jobs/update_wiki_job_test.rb&lt;br /&gt;
  create  app/jobs/update_wiki_job.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Active Job provides the ability to run your Job on a specific queue by create a job as following:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki --queue urgent&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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:&amp;lt;ref&amp;gt; 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] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ApplicationJob &amp;lt; ActiveJob::Base&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 4.2 a Job class defined a perform method and set a “queue_as” value:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :default&lt;br /&gt;
 &lt;br /&gt;
  def perform(*wiki)&lt;br /&gt;
   # Do something later&lt;br /&gt;
  wiki.update_contents&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There is a perform method to be called when the job was first enqueued. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Adding a Job to the queue ===&lt;br /&gt;
If you wish your Job be processed as soon as the queuing system is free, you can enqueue a Job like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.perform_later wiki&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can add a Job be performed tomorrow at noon:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait_until: Date.tomorrow.noon).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you want your Job be performed a week from now, most of the queueing backends ( Sidekiq&lt;br /&gt;
, Delayed Job, etc. ) allow you to set a delay time.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait: 1.week).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Execution of Job ===&lt;br /&gt;
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]).&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt; Without setting any adapter, the job would be performed immediately.&amp;lt;br&amp;gt;&lt;br /&gt;
Queueing backend can be set at: /config/application.rb, in this example we use the Sidekiq.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    # Be sure to have the adapter's gem in your Gemfile and follow&lt;br /&gt;
    # the adapter's specific installation and deployment instructions.&lt;br /&gt;
    config.active_job.queue_adapter = :sidekiq&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Play with Queues ===&lt;br /&gt;
Active Job allows to schedule the job to be processed on a specific queue, this became helpful as common adapters support multiple queues.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Queue name can be prefixed for all jobs using config.active_job.queue_name_prefixin application.rb:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# config/application.rb&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    config.active_job.queue_name_prefix = Rails.env&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# app/jobs/update_wiki_job.rb&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# Now your job will run on queue production_low_priority on your&lt;br /&gt;
# production environment and on staging_low_priority on your staging&lt;br /&gt;
# environment&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can control a job to run on a queue you like, by passing a :queue option to #set :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
MyJob.set(queue: :another_queue).perform_later(record)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the code snippets are referred from the source&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
In this section, some examples are provided to demonstrate how to use ActiveJob in practices.&lt;br /&gt;
=== Background Mail Sender ===&lt;br /&gt;
You can send emails asynchronously with Action Mailer which Active Job is already integrated in.&amp;lt;ref name= &amp;quot;Action-Mailer-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/action_mailer_basics.html “Action Mailer Basics” 2014]&amp;lt;/ref&amp;gt; Here in this example, we try to use Active Job with Action Mailer.&amp;lt;br&amp;gt;&lt;br /&gt;
==== 1. Getting started with Rails:&amp;lt;ref name= &amp;quot;Welcome_to_Rails&amp;quot;&amp;gt;GitHub [https://github.com/rails/rails “Welcome to Rails” 2015]&amp;lt;/ref&amp;gt; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Install Rails if you haven’t done it yet:&lt;br /&gt;
$ gem install rails&lt;br /&gt;
&lt;br /&gt;
# Create a new Rails application, “myapp_activejob”&lt;br /&gt;
# is the name of the application:&lt;br /&gt;
$ rails new myapp_activejob&lt;br /&gt;
&lt;br /&gt;
# Change your directory to myapp_activejob&lt;br /&gt;
$ cd myapp_activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Edit the Gemfile in the folder, add following into it:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'responders'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Run “ $ bundle update “ and make sure “$ rails server” works.&lt;br /&gt;
&lt;br /&gt;
==== 2. Resque setup&amp;lt;ref name= &amp;quot;Download_Redis&amp;quot;&amp;gt;Redis [http://redis.io/download “Download Redis” 2016]&amp;lt;/ref&amp;gt;====&lt;br /&gt;
We will use [https://github.com/resque/resque Resque] as the enqueuing backend. You need to install [http://redis.io/ Redis] before you can run Resque. You can getting Redis with [http://brew.sh/ Homebrew]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ brew install redis&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can download, extract and compile Redis with:&amp;lt;ref name= &amp;quot;Download_Redis&amp;quot;&amp;gt;Redis [http://redis.io/download “Download Redis” 2016]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ wget http://download.redis.io/releases/redis-3.0.7.tar.gz&lt;br /&gt;
$ tar xzf redis-3.0.7.tar.gz&lt;br /&gt;
$ cd redis-3.0.7&lt;br /&gt;
$ make&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Redis does not officially support Windows, but you can find  [https://github.com/MSOpenTech/redis Redis on Windows] by Microsoft Open Tech group.&amp;lt;br&amp;gt;&lt;br /&gt;
Next get Resque be installed. To use resque with Active Job, we also need resque-scheduler.&lt;br /&gt;
Add the following into Gemfile, and run “$ bundle install”.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'resque'&lt;br /&gt;
gem 'resque-scheduler'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After the installation, create a Resque configuration file resque.rb in config/initializers/&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/initializers/resque.rb&lt;br /&gt;
&lt;br /&gt;
Resque.redis = Redis.new(host: 'localhost', post: 6379)&lt;br /&gt;
Resque.after_fork = Proc.new { ActiveRecord::Base.establish_connection }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As the Resque tasks and Resque Scheduler rake tasks are needed in this example, we need to create a resque.rake file in lib/tasks/:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#lib/tasks/resque.rake&lt;br /&gt;
&lt;br /&gt;
require 'resque/tasks'&lt;br /&gt;
require 'resque/scheduler/tasks'&lt;br /&gt;
&lt;br /&gt;
namespace :resque do&lt;br /&gt;
  task setup: :environment do&lt;br /&gt;
    ENV['TERM_CHILD'] ||= '1'&lt;br /&gt;
    ENV['QUEUE'] ||= '*'&lt;br /&gt;
    require 'resque'&lt;br /&gt;
    require 'resque-scheduler'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== 3. Creating a Mailer&amp;lt;ref name= &amp;quot;Action-Mailer-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/action_mailer_basics.html “Action Mailer Basics” 2014]&amp;lt;/ref&amp;gt; ====&lt;br /&gt;
By doing the things above, we already have our Rails project and the Resque for queuing get set. Next we will creat a Mailer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate mailer user_mailer&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a method and a view for sending email.&amp;lt;br&amp;gt;&lt;br /&gt;
In app/mailer/user_mailer.rb:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/mailers/user_mailer.rb&lt;br /&gt;
&lt;br /&gt;
class UserMailer &amp;lt; ActionMailer::Base&lt;br /&gt;
  default from: 'from@example.com'&lt;br /&gt;
&lt;br /&gt;
  def test_email(email)&lt;br /&gt;
    mail(&lt;br /&gt;
      to: email,&lt;br /&gt;
      subject: 'We are testing Active Job!'&lt;br /&gt;
    )&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In app/views/user_mailer/test_email.text&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/views/user_mailer/test_email.text&lt;br /&gt;
&lt;br /&gt;
Hey, we are testing Active Job!&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== 4. Active Job&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt;====&lt;br /&gt;
Create a configuration file active_job.rb in config/initializers/ , to set Resque as queue_adapter.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/initializers/active_job.rb&lt;br /&gt;
&lt;br /&gt;
ActiveJob::Base.queue_adapter = :resque&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a Job with generator.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job test_email&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the generated file app/jobs/test_email_job.rb, define the perform method and set it’s queue_as.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/jobs/test_email_job.rb&lt;br /&gt;
&lt;br /&gt;
class TestEmailJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :email&lt;br /&gt;
&lt;br /&gt;
  def perform(email)&lt;br /&gt;
    UserMailer.test_email(email).deliver_now&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can use a UserController to put the job into the queue for later execution. Here we set the email to be sent one minute later for test.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job test_email&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/controllers/users_controller.rb&lt;br /&gt;
&lt;br /&gt;
class UsersController &amp;lt; ApplicationController&lt;br /&gt;
  def new&lt;br /&gt;
    @user = User.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def create&lt;br /&gt;
    @user = User.create(user_params)&lt;br /&gt;
    TestEmailJob.new(@user.email).deliver_later!(wait: 1.minute)&lt;br /&gt;
    # redirect somewhere&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Routes and view need to set to make it work.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/routes.rb&lt;br /&gt;
&lt;br /&gt;
Rails.application.routes.draw do&lt;br /&gt;
  resources :users, only: [:new, :create]&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/views/users/new.html.erb&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= form_for @user do |f| %&amp;gt;&lt;br /&gt;
  &amp;lt;%= f.email_field :email %&amp;gt;&lt;br /&gt;
  &amp;lt;%= f.submit %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== 5. Try execution ====&lt;br /&gt;
Before we try the mailer, we need a “mailcatcher” for the test. Add following into Gemfile and “$ bundle install”&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'mailcatcher'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To set the environment, add following things into config/environments/development.rb.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/environments/development.rb&lt;br /&gt;
&lt;br /&gt;
Rails.application.configure do&lt;br /&gt;
  ...&lt;br /&gt;
  config.action_mailer.delivery_method = :smtp&lt;br /&gt;
  config.action_mailer.smtp_settings = { address: &amp;quot;localhost&amp;quot;, port: 1025 }&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run it!&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Start mailcatcher&lt;br /&gt;
$ mailcatcher&lt;br /&gt;
&lt;br /&gt;
# Start redis-server&lt;br /&gt;
$ redis-server&lt;br /&gt;
&lt;br /&gt;
# Start resque:work&lt;br /&gt;
$ bundle exec rake resque:work&lt;br /&gt;
&lt;br /&gt;
# Start resque:scheduler&lt;br /&gt;
$ rake environment resque:scheduler&lt;br /&gt;
&lt;br /&gt;
# Finally start rails server&lt;br /&gt;
$ rails server&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Open the browser, visit localhost:3000/users/new and sign up as a new user. One minute later you can see following in Resque scheduler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resque-scheduler: [INFO] 2016-02-05T22:53:15+09:00: Processing Delayed Items&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
And in mailcatcher:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
==&amp;gt; SMTP: Received message from '&amp;lt;from@example.com&amp;gt;' (315 bytes)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Yeah!&lt;br /&gt;
&lt;br /&gt;
=='''References'''==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Schen35</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100745</id>
		<title>Active Job</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100745"/>
		<updated>2016-02-05T22:11:29Z</updated>

		<summary type="html">&lt;p&gt;Schen35: /* Examples */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
Active Job is a framework that helps 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 an interface that adapts different queueing backends like Backburner[https://github.com/nesquena/backburner], Delayed Job[https://github.com/collectiveidea/delayed_job], Qu[https://github.com/bkeepers/qu] and so on. &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” ]&amp;lt;/ref&amp;gt; Overall, Active Job is a interface which you can work with common queues.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
====Ruby On Rails versions support Active Job====&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;float:left;&amp;quot;&lt;br /&gt;
|+ Version history&lt;br /&gt;
|-&lt;br /&gt;
! Version !! Date&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || 2014/12/19&lt;br /&gt;
|-&lt;br /&gt;
| 4.2.5 || 2015/11/13&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
====Active Job adapters&amp;lt;ref name=&amp;quot;Active-Job-Adapter&amp;quot;&amp;gt;Active Job Adapter[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters.html&amp;quot;Active Job Adapter&amp;quot; ]&amp;lt;/ref&amp;gt;====&lt;br /&gt;
*Backburner[https://github.com/nesquena/backburner]&lt;br /&gt;
*Delayed Job[https://github.com/collectiveidea/delayed_job]&lt;br /&gt;
*Qu[https://github.com/bkeepers/qu]&lt;br /&gt;
*Que[https://github.com/chanks/que]&lt;br /&gt;
*queue_classic[https://github.com/QueueClassic/queue_classic]&lt;br /&gt;
*Resque 1.x[https://github.com/resque/resque]&lt;br /&gt;
*Sidekiq[http://sidekiq.org/]&lt;br /&gt;
*Sneakers[https://github.com/jondot/sneakers]&lt;br /&gt;
*Sucker Punch[https://github.com/brandonhilkert/sucker_punch]&lt;br /&gt;
*Active Job Async Job&lt;br /&gt;
*Active Job Inline[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters/InlineAdapter.html]&lt;br /&gt;
&lt;br /&gt;
=='''How to use &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/active_job_basics.html&amp;lt;/ref&amp;gt;'''==&lt;br /&gt;
We will introduce how to creating a job and and how to add the job into a queue. &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
=== Download ===&lt;br /&gt;
With RubyGems you can install the Active Job into your computer:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gem install activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://github.com/rails/rails/tree/master/activejob Souce code of Active Job] available on GitHub, as part of Rails.&amp;lt;ref&amp;gt; GitHub [https://github.com/rails/rails/tree/master/activejob “Active Job -- Make work happen later” 2015] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Create a Job ===&lt;br /&gt;
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.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki&lt;br /&gt;
  invoke  test_unit&lt;br /&gt;
  create  test/jobs/update_wiki_job_test.rb&lt;br /&gt;
  create  app/jobs/update_wiki_job.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Active Job provides the ability to run your Job on a specific queue by create a job as following:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki --queue urgent&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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:&amp;lt;ref&amp;gt; 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] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ApplicationJob &amp;lt; ActiveJob::Base&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 4.2 a Job class defined a perform method and set a “queue_as” value:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :default&lt;br /&gt;
 &lt;br /&gt;
  def perform(*wiki)&lt;br /&gt;
   # Do something later&lt;br /&gt;
  wiki.update_contents&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There is a perform method to be called when the job was first enqueued. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Adding a Job to the queue ===&lt;br /&gt;
If you wish your Job be processed as soon as the queuing system is free, you can enqueue a Job like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.perform_later wiki&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can add a Job be performed tomorrow at noon:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait_until: Date.tomorrow.noon).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you want your Job be performed a week from now, most of the queueing backends ( Sidekiq&lt;br /&gt;
, Delayed Job, etc. ) allow you to set a delay time.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait: 1.week).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Execution of Job ===&lt;br /&gt;
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]).&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt; Without setting any adapter, the job would be performed immediately.&amp;lt;br&amp;gt;&lt;br /&gt;
Queueing backend can be set at: /config/application.rb, in this example we use the Sidekiq.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    # Be sure to have the adapter's gem in your Gemfile and follow&lt;br /&gt;
    # the adapter's specific installation and deployment instructions.&lt;br /&gt;
    config.active_job.queue_adapter = :sidekiq&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Play with Queues ===&lt;br /&gt;
Active Job allows to schedule the job to be processed on a specific queue, this became helpful as common adapters support multiple queues.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Queue name can be prefixed for all jobs using config.active_job.queue_name_prefixin application.rb:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# config/application.rb&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    config.active_job.queue_name_prefix = Rails.env&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# app/jobs/update_wiki_job.rb&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# Now your job will run on queue production_low_priority on your&lt;br /&gt;
# production environment and on staging_low_priority on your staging&lt;br /&gt;
# environment&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can control a job to run on a queue you like, by passing a :queue option to #set :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
MyJob.set(queue: :another_queue).perform_later(record)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the code snippets are referred from the source&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
In this section, some examples are provided to demonstrate how to use ActiveJob in practices.&lt;br /&gt;
=== Background Mail Sender ===&lt;br /&gt;
You can send emails asynchronously with Action Mailer which Active Job is already integrated in.&amp;lt;ref name= &amp;quot;Action-Mailer-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/action_mailer_basics.html “Action Mailer Basics” 2014]&amp;lt;/ref&amp;gt; Here in this example, we try to use Active Job with Action Mailer.&amp;lt;br&amp;gt;&lt;br /&gt;
==== 1. Getting started with Rails:&amp;lt;ref name= &amp;quot;Welcome_to_Rails&amp;quot;&amp;gt;GitHub [https://github.com/rails/rails “Welcome to Rails” 2015]&amp;lt;/ref&amp;gt; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Install Rails if you haven’t done it yet:&lt;br /&gt;
$ gem install rails&lt;br /&gt;
&lt;br /&gt;
# Create a new Rails application, “myapp_activejob”&lt;br /&gt;
# is the name of the application:&lt;br /&gt;
$ rails new myapp_activejob&lt;br /&gt;
&lt;br /&gt;
# Change your directory to myapp_activejob&lt;br /&gt;
$ cd myapp_activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Edit the Gemfile in the folder, add following into it:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'responders'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Run “ $ bundle update “ and make sure “$ rails server” works.&lt;br /&gt;
&lt;br /&gt;
==== 2. Resque setup&amp;lt;ref name= &amp;quot;Download_Redis&amp;quot;&amp;gt;Redis [http://redis.io/download “Download Redis” 2016]&amp;lt;/ref&amp;gt;====&lt;br /&gt;
We will use [https://github.com/resque/resque Resque] as the enqueuing backend. You need to install [http://redis.io/ Redis] before you can run Resque. You can getting Redis with [http://brew.sh/ Homebrew]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ brew install redis&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can download, extract and compile Redis with:&amp;lt;ref name= &amp;quot;Download_Redis&amp;quot;&amp;gt;Redis [http://redis.io/download “Download Redis” 2016]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ wget http://download.redis.io/releases/redis-3.0.7.tar.gz&lt;br /&gt;
$ tar xzf redis-3.0.7.tar.gz&lt;br /&gt;
$ cd redis-3.0.7&lt;br /&gt;
$ make&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Redis does not officially support Windows, but you can find  [https://github.com/MSOpenTech/redis Redis on Windows] by Microsoft Open Tech group.&amp;lt;br&amp;gt;&lt;br /&gt;
Next get Resque be installed. To use resque with Active Job, we also need resque-scheduler.&lt;br /&gt;
Add the following into Gemfile, and run “$ bundle install”.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'resque'&lt;br /&gt;
gem 'resque-scheduler'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After the installation, create a Resque configuration file resque.rb in config/initializers/&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/initializers/resque.rb&lt;br /&gt;
&lt;br /&gt;
Resque.redis = Redis.new(host: 'localhost', post: 6379)&lt;br /&gt;
Resque.after_fork = Proc.new { ActiveRecord::Base.establish_connection }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As the Resque tasks and Resque Scheduler rake tasks are needed in this example, we need to create a resque.rake file in lib/tasks/:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#lib/tasks/resque.rake&lt;br /&gt;
&lt;br /&gt;
require 'resque/tasks'&lt;br /&gt;
require 'resque/scheduler/tasks'&lt;br /&gt;
&lt;br /&gt;
namespace :resque do&lt;br /&gt;
  task setup: :environment do&lt;br /&gt;
    ENV['TERM_CHILD'] ||= '1'&lt;br /&gt;
    ENV['QUEUE'] ||= '*'&lt;br /&gt;
    require 'resque'&lt;br /&gt;
    require 'resque-scheduler'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== 3. Creating a Mailer&amp;lt;ref name= &amp;quot;Action-Mailer-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/action_mailer_basics.html “Action Mailer Basics” 2014]&amp;lt;/ref&amp;gt; ====&lt;br /&gt;
By doing the things above, we already have our Rails project and the Resque for queuing get set. Next we will creat a Mailer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate mailer user_mailer&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a method and a view for sending email.&amp;lt;br&amp;gt;&lt;br /&gt;
In app/mailer/user_mailer.rb:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/mailers/user_mailer.rb&lt;br /&gt;
&lt;br /&gt;
class UserMailer &amp;lt; ActionMailer::Base&lt;br /&gt;
  default from: 'from@example.com'&lt;br /&gt;
&lt;br /&gt;
  def follow_up_email(email)&lt;br /&gt;
    mail(&lt;br /&gt;
      to: email,&lt;br /&gt;
      subject: 'We hope you are enjoying our app'&lt;br /&gt;
    )&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In app/views/user_mailer/follow_up_email.text&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/views/user_mailer/follow_up_email.text&lt;br /&gt;
&lt;br /&gt;
Hey, we saw that you recently signed up for our app.&lt;br /&gt;
We hope you're enjoying it!&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== 4. Active Job====&lt;br /&gt;
Create a configuration file active_job.rb in config/initializers/ , to set Resque as queue_adapter.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/initializers/active_job.rb&lt;br /&gt;
&lt;br /&gt;
ActiveJob::Base.queue_adapter = :resque&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a Job with generator.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job follow_up_email&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the generated file app/jobs/user_registered_mailer_job.rb, define the perform method and set it’s queue_as.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/jobs/follow_up_email_job.rb&lt;br /&gt;
&lt;br /&gt;
class FollowUpEmailJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :email&lt;br /&gt;
&lt;br /&gt;
  def perform(email)&lt;br /&gt;
    UserMailer.follow_up_email(email).deliver_now&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can use a UserController to put the job into the queue for later execution. Here we set the email to be sent one minute later for test.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job follow_up_email&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/controllers/users_controller.rb&lt;br /&gt;
&lt;br /&gt;
class UsersController &amp;lt; ApplicationController&lt;br /&gt;
  def new&lt;br /&gt;
    @user = User.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def create&lt;br /&gt;
    @user = User.create(user_params)&lt;br /&gt;
    FollowUpEmailJob.new(@user.email).deliver_later!(wait: 1.minute)&lt;br /&gt;
    # redirect somewhere&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Routes and view need to set to make it work.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/routes.rb&lt;br /&gt;
&lt;br /&gt;
Rails.application.routes.draw do&lt;br /&gt;
  resources :users, only: [:new, :create]&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/views/users/new.html.erb&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= form_for @user do |f| %&amp;gt;&lt;br /&gt;
  &amp;lt;%= f.email_field :email %&amp;gt;&lt;br /&gt;
  &amp;lt;%= f.submit %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== 5. Try execution ====&lt;br /&gt;
Before we try the mailer, we need a “mailcatcher” for the test. Add following into Gemfile and “$ bundle install”&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'mailcatcher'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To set the environment, add following things into config/environments/development.rb.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/environments/development.rb&lt;br /&gt;
&lt;br /&gt;
Rails.application.configure do&lt;br /&gt;
  ...&lt;br /&gt;
  config.action_mailer.delivery_method = :smtp&lt;br /&gt;
  config.action_mailer.smtp_settings = { :address =&amp;gt; &amp;quot;localhost&amp;quot;, :port =&amp;gt; 1025 }&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run it!&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Start mailcatcher&lt;br /&gt;
$ mailcatcher&lt;br /&gt;
&lt;br /&gt;
# Start redis-server&lt;br /&gt;
$ redis-server&lt;br /&gt;
&lt;br /&gt;
# Start resque:work&lt;br /&gt;
$ bundle exec rake resque:work&lt;br /&gt;
&lt;br /&gt;
# Start resque:scheduler&lt;br /&gt;
$ rake environment resque:scheduler&lt;br /&gt;
&lt;br /&gt;
# Finally start rails server&lt;br /&gt;
$ rails server&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In your browser, view the Resque dashboard at http://0.0.0.0:5678. In another tab, visit http://127.0.0.1:1080 to see the Mailcatcher dashboard. &lt;br /&gt;
Visit localhost:3000/users/new and sign up as a new user. One minute later you can see following in Resque scheduler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resque-scheduler: [INFO] 2016-02-05T22:53:15+09:00: Processing Delayed Items&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
And in mailcatcher:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
==&amp;gt; SMTP: Received message from '&amp;lt;from@example.com&amp;gt;' (315 bytes)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Yeah!&lt;br /&gt;
&lt;br /&gt;
=='''References'''==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Schen35</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100744</id>
		<title>Active Job</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100744"/>
		<updated>2016-02-05T22:08:39Z</updated>

		<summary type="html">&lt;p&gt;Schen35: /* 2. Resque setup */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
Active Job is a framework that helps 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 an interface that adapts different queueing backends like Backburner[https://github.com/nesquena/backburner], Delayed Job[https://github.com/collectiveidea/delayed_job], Qu[https://github.com/bkeepers/qu] and so on. &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” ]&amp;lt;/ref&amp;gt; Overall, Active Job is a interface which you can work with common queues.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
====Ruby On Rails versions support Active Job====&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;float:left;&amp;quot;&lt;br /&gt;
|+ Version history&lt;br /&gt;
|-&lt;br /&gt;
! Version !! Date&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || 2014/12/19&lt;br /&gt;
|-&lt;br /&gt;
| 4.2.5 || 2015/11/13&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
====Active Job adapters&amp;lt;ref name=&amp;quot;Active-Job-Adapter&amp;quot;&amp;gt;Active Job Adapter[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters.html&amp;quot;Active Job Adapter&amp;quot; ]&amp;lt;/ref&amp;gt;====&lt;br /&gt;
*Backburner[https://github.com/nesquena/backburner]&lt;br /&gt;
*Delayed Job[https://github.com/collectiveidea/delayed_job]&lt;br /&gt;
*Qu[https://github.com/bkeepers/qu]&lt;br /&gt;
*Que[https://github.com/chanks/que]&lt;br /&gt;
*queue_classic[https://github.com/QueueClassic/queue_classic]&lt;br /&gt;
*Resque 1.x[https://github.com/resque/resque]&lt;br /&gt;
*Sidekiq[http://sidekiq.org/]&lt;br /&gt;
*Sneakers[https://github.com/jondot/sneakers]&lt;br /&gt;
*Sucker Punch[https://github.com/brandonhilkert/sucker_punch]&lt;br /&gt;
*Active Job Async Job&lt;br /&gt;
*Active Job Inline[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters/InlineAdapter.html]&lt;br /&gt;
&lt;br /&gt;
=='''How to use &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/active_job_basics.html&amp;lt;/ref&amp;gt;'''==&lt;br /&gt;
We will introduce how to creating a job and and how to add the job into a queue. &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
=== Download ===&lt;br /&gt;
With RubyGems you can install the Active Job into your computer:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gem install activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://github.com/rails/rails/tree/master/activejob Souce code of Active Job] available on GitHub, as part of Rails.&amp;lt;ref&amp;gt; GitHub [https://github.com/rails/rails/tree/master/activejob “Active Job -- Make work happen later” 2015] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Create a Job ===&lt;br /&gt;
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.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki&lt;br /&gt;
  invoke  test_unit&lt;br /&gt;
  create  test/jobs/update_wiki_job_test.rb&lt;br /&gt;
  create  app/jobs/update_wiki_job.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Active Job provides the ability to run your Job on a specific queue by create a job as following:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki --queue urgent&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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:&amp;lt;ref&amp;gt; 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] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ApplicationJob &amp;lt; ActiveJob::Base&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 4.2 a Job class defined a perform method and set a “queue_as” value:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :default&lt;br /&gt;
 &lt;br /&gt;
  def perform(*wiki)&lt;br /&gt;
   # Do something later&lt;br /&gt;
  wiki.update_contents&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There is a perform method to be called when the job was first enqueued. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Adding a Job to the queue ===&lt;br /&gt;
If you wish your Job be processed as soon as the queuing system is free, you can enqueue a Job like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.perform_later wiki&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can add a Job be performed tomorrow at noon:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait_until: Date.tomorrow.noon).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you want your Job be performed a week from now, most of the queueing backends ( Sidekiq&lt;br /&gt;
, Delayed Job, etc. ) allow you to set a delay time.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait: 1.week).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Execution of Job ===&lt;br /&gt;
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]).&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt; Without setting any adapter, the job would be performed immediately.&amp;lt;br&amp;gt;&lt;br /&gt;
Queueing backend can be set at: /config/application.rb, in this example we use the Sidekiq.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    # Be sure to have the adapter's gem in your Gemfile and follow&lt;br /&gt;
    # the adapter's specific installation and deployment instructions.&lt;br /&gt;
    config.active_job.queue_adapter = :sidekiq&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Play with Queues ===&lt;br /&gt;
Active Job allows to schedule the job to be processed on a specific queue, this became helpful as common adapters support multiple queues.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Queue name can be prefixed for all jobs using config.active_job.queue_name_prefixin application.rb:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# config/application.rb&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    config.active_job.queue_name_prefix = Rails.env&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# app/jobs/update_wiki_job.rb&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# Now your job will run on queue production_low_priority on your&lt;br /&gt;
# production environment and on staging_low_priority on your staging&lt;br /&gt;
# environment&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can control a job to run on a queue you like, by passing a :queue option to #set :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
MyJob.set(queue: :another_queue).perform_later(record)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the code snippets are referred from the source&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
In this section, some examples are provided to demonstrate how to use ActiveJob in practices.&lt;br /&gt;
=== Background Mail Sender ===&lt;br /&gt;
You can send emails asynchronously with Action Mailer which Active Job is already integrated in.&amp;lt;ref name= &amp;quot;Action-Mailer-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/action_mailer_basics.html “Action Mailer Basics” 2014]&amp;lt;/ref&amp;gt; Here in this example, we try to use Active Job with Action Mailer.&amp;lt;br&amp;gt;&lt;br /&gt;
==== 1. Getting started with Rails:&amp;lt;ref name= &amp;quot;Welcome_to_Rails&amp;quot;&amp;gt;GitHub [https://github.com/rails/rails “Welcome to Rails” 2015]&amp;lt;/ref&amp;gt; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Install Rails if you haven’t done it yet:&lt;br /&gt;
$ gem install rails&lt;br /&gt;
&lt;br /&gt;
# Create a new Rails application, “myapp_activejob”&lt;br /&gt;
# is the name of the application:&lt;br /&gt;
$ rails new myapp_activejob&lt;br /&gt;
&lt;br /&gt;
# Change your directory to myapp_activejob&lt;br /&gt;
$ cd myapp_activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Edit the Gemfile in the folder, add following into it:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'responders'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Run “ $ bundle update “ and make sure “$ rails server” works.&lt;br /&gt;
&lt;br /&gt;
==== 2. Resque setup&amp;lt;ref name= &amp;quot;Download_Redis&amp;quot;&amp;gt;Redis [http://redis.io/download “Download Redis” 2016]&amp;lt;/ref&amp;gt;====&lt;br /&gt;
We will use [https://github.com/resque/resque Resque] as the enqueuing backend. You need to install [http://redis.io/ Redis] before you can run Resque. You can getting Redis with [http://brew.sh/ Homebrew]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ brew install redis&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can download, extract and compile Redis with:&amp;lt;ref name= &amp;quot;Download_Redis&amp;quot;&amp;gt;Redis [http://redis.io/download “Download Redis” 2016]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ wget http://download.redis.io/releases/redis-3.0.7.tar.gz&lt;br /&gt;
$ tar xzf redis-3.0.7.tar.gz&lt;br /&gt;
$ cd redis-3.0.7&lt;br /&gt;
$ make&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Redis does not officially support Windows, but you can find  [https://github.com/MSOpenTech/redis Redis on Windows] by Microsoft Open Tech group.&amp;lt;br&amp;gt;&lt;br /&gt;
Next get Resque be installed. To use resque with Active Job, we also need resque-scheduler.&lt;br /&gt;
Add the following into Gemfile, and run “$ bundle install”.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'resque'&lt;br /&gt;
gem 'resque-scheduler'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After the installation, create a Resque configuration file resque.rb in config/initializers/&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/initializers/resque.rb&lt;br /&gt;
&lt;br /&gt;
Resque.redis = Redis.new(host: 'localhost', post: 6379)&lt;br /&gt;
Resque.after_fork = Proc.new { ActiveRecord::Base.establish_connection }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As the Resque tasks and Resque Scheduler rake tasks are needed in this example, we need to create a resque.rake file in lib/tasks/:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#lib/tasks/resque.rake&lt;br /&gt;
&lt;br /&gt;
require 'resque/tasks'&lt;br /&gt;
require 'resque/scheduler/tasks'&lt;br /&gt;
&lt;br /&gt;
namespace :resque do&lt;br /&gt;
  task setup: :environment do&lt;br /&gt;
    ENV['TERM_CHILD'] ||= '1'&lt;br /&gt;
    ENV['QUEUE'] ||= '*'&lt;br /&gt;
    require 'resque'&lt;br /&gt;
    require 'resque-scheduler'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== 3. Creating a Mailer ====&lt;br /&gt;
By doing the things above, we already have our Rails project and the Resque for queuing get set. Next we will creat a Mailer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate mailer user_mailer&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a method and a view for sending email.&amp;lt;br&amp;gt;&lt;br /&gt;
In app/mailer/user_mailer.rb:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/mailers/user_mailer.rb&lt;br /&gt;
&lt;br /&gt;
class UserMailer &amp;lt; ActionMailer::Base&lt;br /&gt;
  default from: 'from@example.com'&lt;br /&gt;
&lt;br /&gt;
  def follow_up_email(email)&lt;br /&gt;
    mail(&lt;br /&gt;
      to: email,&lt;br /&gt;
      subject: 'We hope you are enjoying our app'&lt;br /&gt;
    )&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In app/views/user_mailer/follow_up_email.text&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/views/user_mailer/follow_up_email.text&lt;br /&gt;
&lt;br /&gt;
Hey, we saw that you recently signed up for our app.&lt;br /&gt;
We hope you're enjoying it!&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== 4. Active Job====&lt;br /&gt;
Create a configuration file active_job.rb in config/initializers/ , to set Resque as queue_adapter.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/initializers/active_job.rb&lt;br /&gt;
&lt;br /&gt;
ActiveJob::Base.queue_adapter = :resque&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a Job with generator.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job follow_up_email&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the generated file app/jobs/user_registered_mailer_job.rb, define the perform method and set it’s queue_as.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/jobs/follow_up_email_job.rb&lt;br /&gt;
&lt;br /&gt;
class FollowUpEmailJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :email&lt;br /&gt;
&lt;br /&gt;
  def perform(email)&lt;br /&gt;
    UserMailer.follow_up_email(email).deliver_now&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can use a UserController to put the job into the queue for later execution. Here we set the email to be sent one minute later for test.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job follow_up_email&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/controllers/users_controller.rb&lt;br /&gt;
&lt;br /&gt;
class UsersController &amp;lt; ApplicationController&lt;br /&gt;
  def new&lt;br /&gt;
    @user = User.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def create&lt;br /&gt;
    @user = User.create(user_params)&lt;br /&gt;
    FollowUpEmailJob.new(@user.email).deliver_later!(wait: 1.minute)&lt;br /&gt;
    # redirect somewhere&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Routes and view need to set to make it work.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/routes.rb&lt;br /&gt;
&lt;br /&gt;
Rails.application.routes.draw do&lt;br /&gt;
  resources :users, only: [:new, :create]&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/views/users/new.html.erb&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= form_for @user do |f| %&amp;gt;&lt;br /&gt;
  &amp;lt;%= f.email_field :email %&amp;gt;&lt;br /&gt;
  &amp;lt;%= f.submit %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== 5. Try execution ====&lt;br /&gt;
Before we try the mailer, we need a “mailcatcher” for the test. Add following into Gemfile and “$ bundle install”&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'mailcatcher'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To set the environment, add following things into config/environments/development.rb.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/environments/development.rb&lt;br /&gt;
&lt;br /&gt;
Rails.application.configure do&lt;br /&gt;
  ...&lt;br /&gt;
  config.action_mailer.delivery_method = :smtp&lt;br /&gt;
  config.action_mailer.smtp_settings = { :address =&amp;gt; &amp;quot;localhost&amp;quot;, :port =&amp;gt; 1025 }&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run it!&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Start mailcatcher&lt;br /&gt;
$ mailcatcher&lt;br /&gt;
&lt;br /&gt;
# Start redis-server&lt;br /&gt;
$ redis-server&lt;br /&gt;
&lt;br /&gt;
# Start resque:work&lt;br /&gt;
$ bundle exec rake resque:work&lt;br /&gt;
&lt;br /&gt;
# Start resque:scheduler&lt;br /&gt;
$ rake environment resque:scheduler&lt;br /&gt;
&lt;br /&gt;
# Finally start rails server&lt;br /&gt;
$ rails server&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In your browser, view the Resque dashboard at http://0.0.0.0:5678. In another tab, visit http://127.0.0.1:1080 to see the Mailcatcher dashboard. &lt;br /&gt;
Visit localhost:3000/users/new and sign up as a new user. One minute later you can see following in Resque scheduler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resque-scheduler: [INFO] 2016-02-05T22:53:15+09:00: Processing Delayed Items&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
And in mailcatcher:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
==&amp;gt; SMTP: Received message from '&amp;lt;from@example.com&amp;gt;' (315 bytes)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Yeah!&lt;br /&gt;
&lt;br /&gt;
=='''References'''==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Schen35</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100743</id>
		<title>Active Job</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100743"/>
		<updated>2016-02-05T22:06:30Z</updated>

		<summary type="html">&lt;p&gt;Schen35: /* 5. Try execution */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
Active Job is a framework that helps 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 an interface that adapts different queueing backends like Backburner[https://github.com/nesquena/backburner], Delayed Job[https://github.com/collectiveidea/delayed_job], Qu[https://github.com/bkeepers/qu] and so on. &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” ]&amp;lt;/ref&amp;gt; Overall, Active Job is a interface which you can work with common queues.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
====Ruby On Rails versions support Active Job====&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;float:left;&amp;quot;&lt;br /&gt;
|+ Version history&lt;br /&gt;
|-&lt;br /&gt;
! Version !! Date&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || 2014/12/19&lt;br /&gt;
|-&lt;br /&gt;
| 4.2.5 || 2015/11/13&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
====Active Job adapters&amp;lt;ref name=&amp;quot;Active-Job-Adapter&amp;quot;&amp;gt;Active Job Adapter[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters.html&amp;quot;Active Job Adapter&amp;quot; ]&amp;lt;/ref&amp;gt;====&lt;br /&gt;
*Backburner[https://github.com/nesquena/backburner]&lt;br /&gt;
*Delayed Job[https://github.com/collectiveidea/delayed_job]&lt;br /&gt;
*Qu[https://github.com/bkeepers/qu]&lt;br /&gt;
*Que[https://github.com/chanks/que]&lt;br /&gt;
*queue_classic[https://github.com/QueueClassic/queue_classic]&lt;br /&gt;
*Resque 1.x[https://github.com/resque/resque]&lt;br /&gt;
*Sidekiq[http://sidekiq.org/]&lt;br /&gt;
*Sneakers[https://github.com/jondot/sneakers]&lt;br /&gt;
*Sucker Punch[https://github.com/brandonhilkert/sucker_punch]&lt;br /&gt;
*Active Job Async Job&lt;br /&gt;
*Active Job Inline[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters/InlineAdapter.html]&lt;br /&gt;
&lt;br /&gt;
=='''How to use &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/active_job_basics.html&amp;lt;/ref&amp;gt;'''==&lt;br /&gt;
We will introduce how to creating a job and and how to add the job into a queue. &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
=== Download ===&lt;br /&gt;
With RubyGems you can install the Active Job into your computer:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gem install activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://github.com/rails/rails/tree/master/activejob Souce code of Active Job] available on GitHub, as part of Rails.&amp;lt;ref&amp;gt; GitHub [https://github.com/rails/rails/tree/master/activejob “Active Job -- Make work happen later” 2015] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Create a Job ===&lt;br /&gt;
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.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki&lt;br /&gt;
  invoke  test_unit&lt;br /&gt;
  create  test/jobs/update_wiki_job_test.rb&lt;br /&gt;
  create  app/jobs/update_wiki_job.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Active Job provides the ability to run your Job on a specific queue by create a job as following:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki --queue urgent&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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:&amp;lt;ref&amp;gt; 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] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ApplicationJob &amp;lt; ActiveJob::Base&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 4.2 a Job class defined a perform method and set a “queue_as” value:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :default&lt;br /&gt;
 &lt;br /&gt;
  def perform(*wiki)&lt;br /&gt;
   # Do something later&lt;br /&gt;
  wiki.update_contents&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There is a perform method to be called when the job was first enqueued. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Adding a Job to the queue ===&lt;br /&gt;
If you wish your Job be processed as soon as the queuing system is free, you can enqueue a Job like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.perform_later wiki&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can add a Job be performed tomorrow at noon:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait_until: Date.tomorrow.noon).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you want your Job be performed a week from now, most of the queueing backends ( Sidekiq&lt;br /&gt;
, Delayed Job, etc. ) allow you to set a delay time.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait: 1.week).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Execution of Job ===&lt;br /&gt;
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]).&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt; Without setting any adapter, the job would be performed immediately.&amp;lt;br&amp;gt;&lt;br /&gt;
Queueing backend can be set at: /config/application.rb, in this example we use the Sidekiq.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    # Be sure to have the adapter's gem in your Gemfile and follow&lt;br /&gt;
    # the adapter's specific installation and deployment instructions.&lt;br /&gt;
    config.active_job.queue_adapter = :sidekiq&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Play with Queues ===&lt;br /&gt;
Active Job allows to schedule the job to be processed on a specific queue, this became helpful as common adapters support multiple queues.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Queue name can be prefixed for all jobs using config.active_job.queue_name_prefixin application.rb:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# config/application.rb&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    config.active_job.queue_name_prefix = Rails.env&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# app/jobs/update_wiki_job.rb&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# Now your job will run on queue production_low_priority on your&lt;br /&gt;
# production environment and on staging_low_priority on your staging&lt;br /&gt;
# environment&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can control a job to run on a queue you like, by passing a :queue option to #set :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
MyJob.set(queue: :another_queue).perform_later(record)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the code snippets are referred from the source&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
In this section, some examples are provided to demonstrate how to use ActiveJob in practices.&lt;br /&gt;
=== Background Mail Sender ===&lt;br /&gt;
You can send emails asynchronously with Action Mailer which Active Job is already integrated in.&amp;lt;ref name= &amp;quot;Action-Mailer-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/action_mailer_basics.html “Action Mailer Basics” 2014]&amp;lt;/ref&amp;gt; Here in this example, we try to use Active Job with Action Mailer.&amp;lt;br&amp;gt;&lt;br /&gt;
==== 1. Getting started with Rails:&amp;lt;ref name= &amp;quot;Welcome_to_Rails&amp;quot;&amp;gt;GitHub [https://github.com/rails/rails “Welcome to Rails” 2015]&amp;lt;/ref&amp;gt; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Install Rails if you haven’t done it yet:&lt;br /&gt;
$ gem install rails&lt;br /&gt;
&lt;br /&gt;
# Create a new Rails application, “myapp_activejob”&lt;br /&gt;
# is the name of the application:&lt;br /&gt;
$ rails new myapp_activejob&lt;br /&gt;
&lt;br /&gt;
# Change your directory to myapp_activejob&lt;br /&gt;
$ cd myapp_activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Edit the Gemfile in the folder, add following into it:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'responders'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Run “ $ bundle update “ and make sure “$ rails server” works.&lt;br /&gt;
&lt;br /&gt;
==== 2. Resque setup ====&lt;br /&gt;
We will use [https://github.com/resque/resque Resque] as the enqueuing backend. You need to install [http://redis.io/ Redis] before you can run Resque. You can getting Redis with [http://brew.sh/ Homebrew]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ brew install redis&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can download, extract and compile Redis with:&amp;lt;ref name= &amp;quot;Download_Redis&amp;quot;&amp;gt;Redis [http://redis.io/download “Download Redis” 2016]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ wget http://download.redis.io/releases/redis-3.0.7.tar.gz&lt;br /&gt;
$ tar xzf redis-3.0.7.tar.gz&lt;br /&gt;
$ cd redis-3.0.7&lt;br /&gt;
$ make&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Redis does not officially support Windows, but you can find  [https://github.com/MSOpenTech/redis Redis on Windows] by Microsoft Open Tech group.&amp;lt;br&amp;gt;&lt;br /&gt;
Next get Resque be installed. To use resque with Active Job, we also need resque-scheduler.&lt;br /&gt;
Add the following into Gemfile, and run “$ bundle install”.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'resque'&lt;br /&gt;
gem 'resque-scheduler'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After the installation, create a Resque configuration file resque.rb in config/initializers/&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/initializers/resque.rb&lt;br /&gt;
&lt;br /&gt;
Resque.redis = Redis.new(host: 'localhost', post: 6379)&lt;br /&gt;
Resque.after_fork = Proc.new { ActiveRecord::Base.establish_connection }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As the Resque tasks and Resque Scheduler rake tasks are needed in this example, we need to create a resque.rake file in lib/tasks/:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#lib/tasks/resque.rake&lt;br /&gt;
&lt;br /&gt;
require 'resque/tasks'&lt;br /&gt;
require 'resque/scheduler/tasks'&lt;br /&gt;
&lt;br /&gt;
namespace :resque do&lt;br /&gt;
  task setup: :environment do&lt;br /&gt;
    ENV['TERM_CHILD'] ||= '1'&lt;br /&gt;
    ENV['QUEUE'] ||= '*'&lt;br /&gt;
    require 'resque'&lt;br /&gt;
    require 'resque-scheduler'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== 3. Creating a Mailer ====&lt;br /&gt;
By doing the things above, we already have our Rails project and the Resque for queuing get set. Next we will creat a Mailer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate mailer user_mailer&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a method and a view for sending email.&amp;lt;br&amp;gt;&lt;br /&gt;
In app/mailer/user_mailer.rb:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/mailers/user_mailer.rb&lt;br /&gt;
&lt;br /&gt;
class UserMailer &amp;lt; ActionMailer::Base&lt;br /&gt;
  default from: 'from@example.com'&lt;br /&gt;
&lt;br /&gt;
  def follow_up_email(email)&lt;br /&gt;
    mail(&lt;br /&gt;
      to: email,&lt;br /&gt;
      subject: 'We hope you are enjoying our app'&lt;br /&gt;
    )&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In app/views/user_mailer/follow_up_email.text&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/views/user_mailer/follow_up_email.text&lt;br /&gt;
&lt;br /&gt;
Hey, we saw that you recently signed up for our app.&lt;br /&gt;
We hope you're enjoying it!&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== 4. Active Job====&lt;br /&gt;
Create a configuration file active_job.rb in config/initializers/ , to set Resque as queue_adapter.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/initializers/active_job.rb&lt;br /&gt;
&lt;br /&gt;
ActiveJob::Base.queue_adapter = :resque&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a Job with generator.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job follow_up_email&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the generated file app/jobs/user_registered_mailer_job.rb, define the perform method and set it’s queue_as.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/jobs/follow_up_email_job.rb&lt;br /&gt;
&lt;br /&gt;
class FollowUpEmailJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :email&lt;br /&gt;
&lt;br /&gt;
  def perform(email)&lt;br /&gt;
    UserMailer.follow_up_email(email).deliver_now&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can use a UserController to put the job into the queue for later execution. Here we set the email to be sent one minute later for test.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job follow_up_email&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/controllers/users_controller.rb&lt;br /&gt;
&lt;br /&gt;
class UsersController &amp;lt; ApplicationController&lt;br /&gt;
  def new&lt;br /&gt;
    @user = User.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def create&lt;br /&gt;
    @user = User.create(user_params)&lt;br /&gt;
    FollowUpEmailJob.new(@user.email).deliver_later!(wait: 1.minute)&lt;br /&gt;
    # redirect somewhere&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Routes and view need to set to make it work.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/routes.rb&lt;br /&gt;
&lt;br /&gt;
Rails.application.routes.draw do&lt;br /&gt;
  resources :users, only: [:new, :create]&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/views/users/new.html.erb&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= form_for @user do |f| %&amp;gt;&lt;br /&gt;
  &amp;lt;%= f.email_field :email %&amp;gt;&lt;br /&gt;
  &amp;lt;%= f.submit %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== 5. Try execution ====&lt;br /&gt;
Before we try the mailer, we need a “mailcatcher” for the test. Add following into Gemfile and “$ bundle install”&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'mailcatcher'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To set the environment, add following things into config/environments/development.rb.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/environments/development.rb&lt;br /&gt;
&lt;br /&gt;
Rails.application.configure do&lt;br /&gt;
  ...&lt;br /&gt;
  config.action_mailer.delivery_method = :smtp&lt;br /&gt;
  config.action_mailer.smtp_settings = { :address =&amp;gt; &amp;quot;localhost&amp;quot;, :port =&amp;gt; 1025 }&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run it!&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Start mailcatcher&lt;br /&gt;
$ mailcatcher&lt;br /&gt;
&lt;br /&gt;
# Start redis-server&lt;br /&gt;
$ redis-server&lt;br /&gt;
&lt;br /&gt;
# Start resque:work&lt;br /&gt;
$ bundle exec rake resque:work&lt;br /&gt;
&lt;br /&gt;
# Start resque:scheduler&lt;br /&gt;
$ rake environment resque:scheduler&lt;br /&gt;
&lt;br /&gt;
# Finally start rails server&lt;br /&gt;
$ rails server&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In your browser, view the Resque dashboard at http://0.0.0.0:5678. In another tab, visit http://127.0.0.1:1080 to see the Mailcatcher dashboard. &lt;br /&gt;
Visit localhost:3000/users/new and sign up as a new user. One minute later you can see following in Resque scheduler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resque-scheduler: [INFO] 2016-02-05T22:53:15+09:00: Processing Delayed Items&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
And in mailcatcher:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
==&amp;gt; SMTP: Received message from '&amp;lt;from@example.com&amp;gt;' (315 bytes)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Yeah!&lt;br /&gt;
&lt;br /&gt;
=='''References'''==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Schen35</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100742</id>
		<title>Active Job</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100742"/>
		<updated>2016-02-05T22:05:51Z</updated>

		<summary type="html">&lt;p&gt;Schen35: /* 5. Try execution */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
Active Job is a framework that helps 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 an interface that adapts different queueing backends like Backburner[https://github.com/nesquena/backburner], Delayed Job[https://github.com/collectiveidea/delayed_job], Qu[https://github.com/bkeepers/qu] and so on. &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” ]&amp;lt;/ref&amp;gt; Overall, Active Job is a interface which you can work with common queues.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
====Ruby On Rails versions support Active Job====&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;float:left;&amp;quot;&lt;br /&gt;
|+ Version history&lt;br /&gt;
|-&lt;br /&gt;
! Version !! Date&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || 2014/12/19&lt;br /&gt;
|-&lt;br /&gt;
| 4.2.5 || 2015/11/13&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
====Active Job adapters&amp;lt;ref name=&amp;quot;Active-Job-Adapter&amp;quot;&amp;gt;Active Job Adapter[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters.html&amp;quot;Active Job Adapter&amp;quot; ]&amp;lt;/ref&amp;gt;====&lt;br /&gt;
*Backburner[https://github.com/nesquena/backburner]&lt;br /&gt;
*Delayed Job[https://github.com/collectiveidea/delayed_job]&lt;br /&gt;
*Qu[https://github.com/bkeepers/qu]&lt;br /&gt;
*Que[https://github.com/chanks/que]&lt;br /&gt;
*queue_classic[https://github.com/QueueClassic/queue_classic]&lt;br /&gt;
*Resque 1.x[https://github.com/resque/resque]&lt;br /&gt;
*Sidekiq[http://sidekiq.org/]&lt;br /&gt;
*Sneakers[https://github.com/jondot/sneakers]&lt;br /&gt;
*Sucker Punch[https://github.com/brandonhilkert/sucker_punch]&lt;br /&gt;
*Active Job Async Job&lt;br /&gt;
*Active Job Inline[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters/InlineAdapter.html]&lt;br /&gt;
&lt;br /&gt;
=='''How to use &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/active_job_basics.html&amp;lt;/ref&amp;gt;'''==&lt;br /&gt;
We will introduce how to creating a job and and how to add the job into a queue. &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
=== Download ===&lt;br /&gt;
With RubyGems you can install the Active Job into your computer:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gem install activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://github.com/rails/rails/tree/master/activejob Souce code of Active Job] available on GitHub, as part of Rails.&amp;lt;ref&amp;gt; GitHub [https://github.com/rails/rails/tree/master/activejob “Active Job -- Make work happen later” 2015] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Create a Job ===&lt;br /&gt;
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.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki&lt;br /&gt;
  invoke  test_unit&lt;br /&gt;
  create  test/jobs/update_wiki_job_test.rb&lt;br /&gt;
  create  app/jobs/update_wiki_job.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Active Job provides the ability to run your Job on a specific queue by create a job as following:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki --queue urgent&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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:&amp;lt;ref&amp;gt; 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] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ApplicationJob &amp;lt; ActiveJob::Base&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 4.2 a Job class defined a perform method and set a “queue_as” value:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :default&lt;br /&gt;
 &lt;br /&gt;
  def perform(*wiki)&lt;br /&gt;
   # Do something later&lt;br /&gt;
  wiki.update_contents&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There is a perform method to be called when the job was first enqueued. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Adding a Job to the queue ===&lt;br /&gt;
If you wish your Job be processed as soon as the queuing system is free, you can enqueue a Job like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.perform_later wiki&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can add a Job be performed tomorrow at noon:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait_until: Date.tomorrow.noon).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you want your Job be performed a week from now, most of the queueing backends ( Sidekiq&lt;br /&gt;
, Delayed Job, etc. ) allow you to set a delay time.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait: 1.week).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Execution of Job ===&lt;br /&gt;
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]).&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt; Without setting any adapter, the job would be performed immediately.&amp;lt;br&amp;gt;&lt;br /&gt;
Queueing backend can be set at: /config/application.rb, in this example we use the Sidekiq.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    # Be sure to have the adapter's gem in your Gemfile and follow&lt;br /&gt;
    # the adapter's specific installation and deployment instructions.&lt;br /&gt;
    config.active_job.queue_adapter = :sidekiq&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Play with Queues ===&lt;br /&gt;
Active Job allows to schedule the job to be processed on a specific queue, this became helpful as common adapters support multiple queues.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Queue name can be prefixed for all jobs using config.active_job.queue_name_prefixin application.rb:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# config/application.rb&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    config.active_job.queue_name_prefix = Rails.env&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# app/jobs/update_wiki_job.rb&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# Now your job will run on queue production_low_priority on your&lt;br /&gt;
# production environment and on staging_low_priority on your staging&lt;br /&gt;
# environment&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can control a job to run on a queue you like, by passing a :queue option to #set :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
MyJob.set(queue: :another_queue).perform_later(record)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the code snippets are referred from the source&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
In this section, some examples are provided to demonstrate how to use ActiveJob in practices.&lt;br /&gt;
=== Background Mail Sender ===&lt;br /&gt;
You can send emails asynchronously with Action Mailer which Active Job is already integrated in.&amp;lt;ref name= &amp;quot;Action-Mailer-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/action_mailer_basics.html “Action Mailer Basics” 2014]&amp;lt;/ref&amp;gt; Here in this example, we try to use Active Job with Action Mailer.&amp;lt;br&amp;gt;&lt;br /&gt;
==== 1. Getting started with Rails:&amp;lt;ref name= &amp;quot;Welcome_to_Rails&amp;quot;&amp;gt;GitHub [https://github.com/rails/rails “Welcome to Rails” 2015]&amp;lt;/ref&amp;gt; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Install Rails if you haven’t done it yet:&lt;br /&gt;
$ gem install rails&lt;br /&gt;
&lt;br /&gt;
# Create a new Rails application, “myapp_activejob”&lt;br /&gt;
# is the name of the application:&lt;br /&gt;
$ rails new myapp_activejob&lt;br /&gt;
&lt;br /&gt;
# Change your directory to myapp_activejob&lt;br /&gt;
$ cd myapp_activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Edit the Gemfile in the folder, add following into it:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'responders'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Run “ $ bundle update “ and make sure “$ rails server” works.&lt;br /&gt;
&lt;br /&gt;
==== 2. Resque setup ====&lt;br /&gt;
We will use [https://github.com/resque/resque Resque] as the enqueuing backend. You need to install [http://redis.io/ Redis] before you can run Resque. You can getting Redis with [http://brew.sh/ Homebrew]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ brew install redis&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can download, extract and compile Redis with:&amp;lt;ref name= &amp;quot;Download_Redis&amp;quot;&amp;gt;Redis [http://redis.io/download “Download Redis” 2016]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ wget http://download.redis.io/releases/redis-3.0.7.tar.gz&lt;br /&gt;
$ tar xzf redis-3.0.7.tar.gz&lt;br /&gt;
$ cd redis-3.0.7&lt;br /&gt;
$ make&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Redis does not officially support Windows, but you can find  [https://github.com/MSOpenTech/redis Redis on Windows] by Microsoft Open Tech group.&amp;lt;br&amp;gt;&lt;br /&gt;
Next get Resque be installed. To use resque with Active Job, we also need resque-scheduler.&lt;br /&gt;
Add the following into Gemfile, and run “$ bundle install”.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'resque'&lt;br /&gt;
gem 'resque-scheduler'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After the installation, create a Resque configuration file resque.rb in config/initializers/&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/initializers/resque.rb&lt;br /&gt;
&lt;br /&gt;
Resque.redis = Redis.new(host: 'localhost', post: 6379)&lt;br /&gt;
Resque.after_fork = Proc.new { ActiveRecord::Base.establish_connection }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As the Resque tasks and Resque Scheduler rake tasks are needed in this example, we need to create a resque.rake file in lib/tasks/:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#lib/tasks/resque.rake&lt;br /&gt;
&lt;br /&gt;
require 'resque/tasks'&lt;br /&gt;
require 'resque/scheduler/tasks'&lt;br /&gt;
&lt;br /&gt;
namespace :resque do&lt;br /&gt;
  task setup: :environment do&lt;br /&gt;
    ENV['TERM_CHILD'] ||= '1'&lt;br /&gt;
    ENV['QUEUE'] ||= '*'&lt;br /&gt;
    require 'resque'&lt;br /&gt;
    require 'resque-scheduler'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== 3. Creating a Mailer ====&lt;br /&gt;
By doing the things above, we already have our Rails project and the Resque for queuing get set. Next we will creat a Mailer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate mailer user_mailer&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a method and a view for sending email.&amp;lt;br&amp;gt;&lt;br /&gt;
In app/mailer/user_mailer.rb:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/mailers/user_mailer.rb&lt;br /&gt;
&lt;br /&gt;
class UserMailer &amp;lt; ActionMailer::Base&lt;br /&gt;
  default from: 'from@example.com'&lt;br /&gt;
&lt;br /&gt;
  def follow_up_email(email)&lt;br /&gt;
    mail(&lt;br /&gt;
      to: email,&lt;br /&gt;
      subject: 'We hope you are enjoying our app'&lt;br /&gt;
    )&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In app/views/user_mailer/follow_up_email.text&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/views/user_mailer/follow_up_email.text&lt;br /&gt;
&lt;br /&gt;
Hey, we saw that you recently signed up for our app.&lt;br /&gt;
We hope you're enjoying it!&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== 4. Active Job====&lt;br /&gt;
Create a configuration file active_job.rb in config/initializers/ , to set Resque as queue_adapter.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/initializers/active_job.rb&lt;br /&gt;
&lt;br /&gt;
ActiveJob::Base.queue_adapter = :resque&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a Job with generator.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job follow_up_email&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the generated file app/jobs/user_registered_mailer_job.rb, define the perform method and set it’s queue_as.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/jobs/follow_up_email_job.rb&lt;br /&gt;
&lt;br /&gt;
class FollowUpEmailJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :email&lt;br /&gt;
&lt;br /&gt;
  def perform(email)&lt;br /&gt;
    UserMailer.follow_up_email(email).deliver_now&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can use a UserController to put the job into the queue for later execution. Here we set the email to be sent one minute later for test.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job follow_up_email&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/controllers/users_controller.rb&lt;br /&gt;
&lt;br /&gt;
class UsersController &amp;lt; ApplicationController&lt;br /&gt;
  def new&lt;br /&gt;
    @user = User.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def create&lt;br /&gt;
    @user = User.create(user_params)&lt;br /&gt;
    FollowUpEmailJob.new(@user.email).deliver_later!(wait: 1.minute)&lt;br /&gt;
    # redirect somewhere&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Routes and view need to set to make it work.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/routes.rb&lt;br /&gt;
&lt;br /&gt;
Rails.application.routes.draw do&lt;br /&gt;
  resources :users, only: [:new, :create]&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/views/users/new.html.erb&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= form_for @user do |f| %&amp;gt;&lt;br /&gt;
  &amp;lt;%= f.email_field :email %&amp;gt;&lt;br /&gt;
  &amp;lt;%= f.submit %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== 5. Try execution ====&lt;br /&gt;
Before we try the mailer, we need a “mailcatcher” for the test. Add following into Gemfile and “$ bundle install”&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'mailcatcher'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To set the environment, add following things into config/environments/development.rb.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/environments/development.rb&lt;br /&gt;
&lt;br /&gt;
Rails.application.configure do&lt;br /&gt;
  ...&lt;br /&gt;
  config.action_mailer.delivery_method = :smtp&lt;br /&gt;
  config.action_mailer.smtp_settings = { :address =&amp;gt; &amp;quot;localhost&amp;quot;, :port =&amp;gt; 1025 }&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run it!&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Start mailcatcher&lt;br /&gt;
$ mailcatcher&lt;br /&gt;
&lt;br /&gt;
# Start redis-server&lt;br /&gt;
$ redis-server&lt;br /&gt;
&lt;br /&gt;
# Start resque:work&lt;br /&gt;
$ bundle exec rake resque:work&lt;br /&gt;
&lt;br /&gt;
# Start resque:scheduler&lt;br /&gt;
$ rake environment resque:scheduler&lt;br /&gt;
&lt;br /&gt;
# Finally start rails server&lt;br /&gt;
$ rails server&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In your browser, view the Resque dashboard at http:/http://0.0.0.0:5678. In another tab, visit http://127.0.0.1:1080 to see the Mailcatcher dashboard. &lt;br /&gt;
Visit localhost:3000/users/new and sign up as a new user. One minute later you can see following in Resque scheduler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resque-scheduler: [INFO] 2016-02-05T22:53:15+09:00: Processing Delayed Items&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
And in mailcatcher:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
==&amp;gt; SMTP: Received message from '&amp;lt;from@example.com&amp;gt;' (315 bytes)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Yeah!&lt;br /&gt;
&lt;br /&gt;
=='''References'''==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Schen35</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100741</id>
		<title>Active Job</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100741"/>
		<updated>2016-02-05T21:41:51Z</updated>

		<summary type="html">&lt;p&gt;Schen35: /* 5. Try execution */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
Active Job is a framework that helps 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 an interface that adapts different queueing backends like Backburner[https://github.com/nesquena/backburner], Delayed Job[https://github.com/collectiveidea/delayed_job], Qu[https://github.com/bkeepers/qu] and so on. &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” ]&amp;lt;/ref&amp;gt; Overall, Active Job is a interface which you can work with common queues.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
====Ruby On Rails versions support Active Job====&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;float:left;&amp;quot;&lt;br /&gt;
|+ Version history&lt;br /&gt;
|-&lt;br /&gt;
! Version !! Date&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || 2014/12/19&lt;br /&gt;
|-&lt;br /&gt;
| 4.2.5 || 2015/11/13&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
====Active Job adapters&amp;lt;ref name=&amp;quot;Active-Job-Adapter&amp;quot;&amp;gt;Active Job Adapter[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters.html&amp;quot;Active Job Adapter&amp;quot; ]&amp;lt;/ref&amp;gt;====&lt;br /&gt;
*Backburner[https://github.com/nesquena/backburner]&lt;br /&gt;
*Delayed Job[https://github.com/collectiveidea/delayed_job]&lt;br /&gt;
*Qu[https://github.com/bkeepers/qu]&lt;br /&gt;
*Que[https://github.com/chanks/que]&lt;br /&gt;
*queue_classic[https://github.com/QueueClassic/queue_classic]&lt;br /&gt;
*Resque 1.x[https://github.com/resque/resque]&lt;br /&gt;
*Sidekiq[http://sidekiq.org/]&lt;br /&gt;
*Sneakers[https://github.com/jondot/sneakers]&lt;br /&gt;
*Sucker Punch[https://github.com/brandonhilkert/sucker_punch]&lt;br /&gt;
*Active Job Async Job&lt;br /&gt;
*Active Job Inline[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters/InlineAdapter.html]&lt;br /&gt;
&lt;br /&gt;
=='''How to use &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/active_job_basics.html&amp;lt;/ref&amp;gt;'''==&lt;br /&gt;
We will introduce how to creating a job and and how to add the job into a queue. &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
=== Download ===&lt;br /&gt;
With RubyGems you can install the Active Job into your computer:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gem install activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://github.com/rails/rails/tree/master/activejob Souce code of Active Job] available on GitHub, as part of Rails.&amp;lt;ref&amp;gt; GitHub [https://github.com/rails/rails/tree/master/activejob “Active Job -- Make work happen later” 2015] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Create a Job ===&lt;br /&gt;
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.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki&lt;br /&gt;
  invoke  test_unit&lt;br /&gt;
  create  test/jobs/update_wiki_job_test.rb&lt;br /&gt;
  create  app/jobs/update_wiki_job.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Active Job provides the ability to run your Job on a specific queue by create a job as following:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki --queue urgent&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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:&amp;lt;ref&amp;gt; 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] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ApplicationJob &amp;lt; ActiveJob::Base&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 4.2 a Job class defined a perform method and set a “queue_as” value:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :default&lt;br /&gt;
 &lt;br /&gt;
  def perform(*wiki)&lt;br /&gt;
   # Do something later&lt;br /&gt;
  wiki.update_contents&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There is a perform method to be called when the job was first enqueued. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Adding a Job to the queue ===&lt;br /&gt;
If you wish your Job be processed as soon as the queuing system is free, you can enqueue a Job like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.perform_later wiki&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can add a Job be performed tomorrow at noon:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait_until: Date.tomorrow.noon).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you want your Job be performed a week from now, most of the queueing backends ( Sidekiq&lt;br /&gt;
, Delayed Job, etc. ) allow you to set a delay time.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait: 1.week).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Execution of Job ===&lt;br /&gt;
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]).&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt; Without setting any adapter, the job would be performed immediately.&amp;lt;br&amp;gt;&lt;br /&gt;
Queueing backend can be set at: /config/application.rb, in this example we use the Sidekiq.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    # Be sure to have the adapter's gem in your Gemfile and follow&lt;br /&gt;
    # the adapter's specific installation and deployment instructions.&lt;br /&gt;
    config.active_job.queue_adapter = :sidekiq&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Play with Queues ===&lt;br /&gt;
Active Job allows to schedule the job to be processed on a specific queue, this became helpful as common adapters support multiple queues.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Queue name can be prefixed for all jobs using config.active_job.queue_name_prefixin application.rb:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# config/application.rb&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    config.active_job.queue_name_prefix = Rails.env&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# app/jobs/update_wiki_job.rb&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# Now your job will run on queue production_low_priority on your&lt;br /&gt;
# production environment and on staging_low_priority on your staging&lt;br /&gt;
# environment&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can control a job to run on a queue you like, by passing a :queue option to #set :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
MyJob.set(queue: :another_queue).perform_later(record)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the code snippets are referred from the source&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
In this section, some examples are provided to demonstrate how to use ActiveJob in practices.&lt;br /&gt;
=== Background Mail Sender ===&lt;br /&gt;
You can send emails asynchronously with Action Mailer which Active Job is already integrated in.&amp;lt;ref name= &amp;quot;Action-Mailer-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/action_mailer_basics.html “Action Mailer Basics” 2014]&amp;lt;/ref&amp;gt; Here in this example, we try to use Active Job with Action Mailer.&amp;lt;br&amp;gt;&lt;br /&gt;
==== 1. Getting started with Rails:&amp;lt;ref name= &amp;quot;Welcome_to_Rails&amp;quot;&amp;gt;GitHub [https://github.com/rails/rails “Welcome to Rails” 2015]&amp;lt;/ref&amp;gt; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Install Rails if you haven’t done it yet:&lt;br /&gt;
$ gem install rails&lt;br /&gt;
&lt;br /&gt;
# Create a new Rails application, “myapp_activejob”&lt;br /&gt;
# is the name of the application:&lt;br /&gt;
$ rails new myapp_activejob&lt;br /&gt;
&lt;br /&gt;
# Change your directory to myapp_activejob&lt;br /&gt;
$ cd myapp_activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Edit the Gemfile in the folder, add following into it:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'responders'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Run “ $ bundle update “ and make sure “$ rails server” works.&lt;br /&gt;
&lt;br /&gt;
==== 2. Resque setup ====&lt;br /&gt;
We will use [https://github.com/resque/resque Resque] as the enqueuing backend. You need to install [http://redis.io/ Redis] before you can run Resque. You can getting Redis with [http://brew.sh/ Homebrew]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ brew install redis&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can download, extract and compile Redis with:&amp;lt;ref name= &amp;quot;Download_Redis&amp;quot;&amp;gt;Redis [http://redis.io/download “Download Redis” 2016]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ wget http://download.redis.io/releases/redis-3.0.7.tar.gz&lt;br /&gt;
$ tar xzf redis-3.0.7.tar.gz&lt;br /&gt;
$ cd redis-3.0.7&lt;br /&gt;
$ make&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Redis does not officially support Windows, but you can find  [https://github.com/MSOpenTech/redis Redis on Windows] by Microsoft Open Tech group.&amp;lt;br&amp;gt;&lt;br /&gt;
Next get Resque be installed. To use resque with Active Job, we also need resque-scheduler.&lt;br /&gt;
Add the following into Gemfile, and run “$ bundle install”.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'resque'&lt;br /&gt;
gem 'resque-scheduler'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After the installation, create a Resque configuration file resque.rb in config/initializers/&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/initializers/resque.rb&lt;br /&gt;
&lt;br /&gt;
Resque.redis = Redis.new(host: 'localhost', post: 6379)&lt;br /&gt;
Resque.after_fork = Proc.new { ActiveRecord::Base.establish_connection }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As the Resque tasks and Resque Scheduler rake tasks are needed in this example, we need to create a resque.rake file in lib/tasks/:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#lib/tasks/resque.rake&lt;br /&gt;
&lt;br /&gt;
require 'resque/tasks'&lt;br /&gt;
require 'resque/scheduler/tasks'&lt;br /&gt;
&lt;br /&gt;
namespace :resque do&lt;br /&gt;
  task setup: :environment do&lt;br /&gt;
    ENV['TERM_CHILD'] ||= '1'&lt;br /&gt;
    ENV['QUEUE'] ||= '*'&lt;br /&gt;
    require 'resque'&lt;br /&gt;
    require 'resque-scheduler'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== 3. Creating a Mailer ====&lt;br /&gt;
By doing the things above, we already have our Rails project and the Resque for queuing get set. Next we will creat a Mailer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate mailer user_mailer&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a method and a view for sending email.&amp;lt;br&amp;gt;&lt;br /&gt;
In app/mailer/user_mailer.rb:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/mailers/user_mailer.rb&lt;br /&gt;
&lt;br /&gt;
class UserMailer &amp;lt; ActionMailer::Base&lt;br /&gt;
  default from: 'from@example.com'&lt;br /&gt;
&lt;br /&gt;
  def follow_up_email(email)&lt;br /&gt;
    mail(&lt;br /&gt;
      to: email,&lt;br /&gt;
      subject: 'We hope you are enjoying our app'&lt;br /&gt;
    )&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In app/views/user_mailer/follow_up_email.text&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/views/user_mailer/follow_up_email.text&lt;br /&gt;
&lt;br /&gt;
Hey, we saw that you recently signed up for our app.&lt;br /&gt;
We hope you're enjoying it!&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== 4. Active Job====&lt;br /&gt;
Create a configuration file active_job.rb in config/initializers/ , to set Resque as queue_adapter.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/initializers/active_job.rb&lt;br /&gt;
&lt;br /&gt;
ActiveJob::Base.queue_adapter = :resque&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a Job with generator.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job follow_up_email&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the generated file app/jobs/user_registered_mailer_job.rb, define the perform method and set it’s queue_as.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/jobs/follow_up_email_job.rb&lt;br /&gt;
&lt;br /&gt;
class FollowUpEmailJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :email&lt;br /&gt;
&lt;br /&gt;
  def perform(email)&lt;br /&gt;
    UserMailer.follow_up_email(email).deliver_now&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can use a UserController to put the job into the queue for later execution. Here we set the email to be sent one minute later for test.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job follow_up_email&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/controllers/users_controller.rb&lt;br /&gt;
&lt;br /&gt;
class UsersController &amp;lt; ApplicationController&lt;br /&gt;
  def new&lt;br /&gt;
    @user = User.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def create&lt;br /&gt;
    @user = User.create(user_params)&lt;br /&gt;
    FollowUpEmailJob.new(@user.email).deliver_later!(wait: 1.minute)&lt;br /&gt;
    # redirect somewhere&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Routes and view need to set to make it work.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/routes.rb&lt;br /&gt;
&lt;br /&gt;
Rails.application.routes.draw do&lt;br /&gt;
  resources :users, only: [:new, :create]&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/views/users/new.html.erb&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= form_for @user do |f| %&amp;gt;&lt;br /&gt;
  &amp;lt;%= f.email_field :email %&amp;gt;&lt;br /&gt;
  &amp;lt;%= f.submit %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== 5. Try execution ====&lt;br /&gt;
Before we try the mailer, we need a “mailcatcher” for the test. Add following into Gemfile and “$ bundle install”&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'mailcatcher'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To set the environment, add following things into config/environments/development.rb.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/environments/development.rb&lt;br /&gt;
&lt;br /&gt;
Rails.application.configure do&lt;br /&gt;
  ...&lt;br /&gt;
  config.action_mailer.delivery_method = :smtp&lt;br /&gt;
  config.action_mailer.smtp_settings = { :address =&amp;gt; &amp;quot;localhost&amp;quot;, :port =&amp;gt; 1025 }&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run it!&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Start mailcatcher&lt;br /&gt;
$ mailcatcher&lt;br /&gt;
&lt;br /&gt;
# Start redis-server&lt;br /&gt;
$ redis-server&lt;br /&gt;
&lt;br /&gt;
# Start resque:work&lt;br /&gt;
$ bundle exec rake resque:work&lt;br /&gt;
&lt;br /&gt;
# Start resque:scheduler&lt;br /&gt;
$ rake environment resque:scheduler&lt;br /&gt;
&lt;br /&gt;
# Finally start rails server&lt;br /&gt;
$ rails server&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In your browser, view the Resque dashboard at http:/http://0.0.0.0:5678. In another tab, visit http://127.0.0.1:1080 to see the Mailcatcher dashboard.&lt;br /&gt;
&lt;br /&gt;
One minute later you can see following in Resque scheduler:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resque-scheduler: [INFO] 2016-02-05T16:22:55-05:00: Processing Delayed Items&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
And in mailcatcher:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
==&amp;gt; SMTP: Received message from '&amp;lt;from@example.com&amp;gt;' (315 bytes)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Yeah!&lt;br /&gt;
&lt;br /&gt;
=='''References'''==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Schen35</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100740</id>
		<title>Active Job</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100740"/>
		<updated>2016-02-05T21:18:16Z</updated>

		<summary type="html">&lt;p&gt;Schen35: /* 5. Try execution */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
Active Job is a framework that helps 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 an interface that adapts different queueing backends like Backburner[https://github.com/nesquena/backburner], Delayed Job[https://github.com/collectiveidea/delayed_job], Qu[https://github.com/bkeepers/qu] and so on. &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” ]&amp;lt;/ref&amp;gt; Overall, Active Job is a interface which you can work with common queues.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
====Ruby On Rails versions support Active Job====&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;float:left;&amp;quot;&lt;br /&gt;
|+ Version history&lt;br /&gt;
|-&lt;br /&gt;
! Version !! Date&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || 2014/12/19&lt;br /&gt;
|-&lt;br /&gt;
| 4.2.5 || 2015/11/13&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
====Active Job adapters&amp;lt;ref name=&amp;quot;Active-Job-Adapter&amp;quot;&amp;gt;Active Job Adapter[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters.html&amp;quot;Active Job Adapter&amp;quot; ]&amp;lt;/ref&amp;gt;====&lt;br /&gt;
*Backburner[https://github.com/nesquena/backburner]&lt;br /&gt;
*Delayed Job[https://github.com/collectiveidea/delayed_job]&lt;br /&gt;
*Qu[https://github.com/bkeepers/qu]&lt;br /&gt;
*Que[https://github.com/chanks/que]&lt;br /&gt;
*queue_classic[https://github.com/QueueClassic/queue_classic]&lt;br /&gt;
*Resque 1.x[https://github.com/resque/resque]&lt;br /&gt;
*Sidekiq[http://sidekiq.org/]&lt;br /&gt;
*Sneakers[https://github.com/jondot/sneakers]&lt;br /&gt;
*Sucker Punch[https://github.com/brandonhilkert/sucker_punch]&lt;br /&gt;
*Active Job Async Job&lt;br /&gt;
*Active Job Inline[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters/InlineAdapter.html]&lt;br /&gt;
&lt;br /&gt;
=='''How to use &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/active_job_basics.html&amp;lt;/ref&amp;gt;'''==&lt;br /&gt;
We will introduce how to creating a job and and how to add the job into a queue. &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
=== Download ===&lt;br /&gt;
With RubyGems you can install the Active Job into your computer:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gem install activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://github.com/rails/rails/tree/master/activejob Souce code of Active Job] available on GitHub, as part of Rails.&amp;lt;ref&amp;gt; GitHub [https://github.com/rails/rails/tree/master/activejob “Active Job -- Make work happen later” 2015] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Create a Job ===&lt;br /&gt;
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.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki&lt;br /&gt;
  invoke  test_unit&lt;br /&gt;
  create  test/jobs/update_wiki_job_test.rb&lt;br /&gt;
  create  app/jobs/update_wiki_job.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Active Job provides the ability to run your Job on a specific queue by create a job as following:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki --queue urgent&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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:&amp;lt;ref&amp;gt; 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] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ApplicationJob &amp;lt; ActiveJob::Base&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 4.2 a Job class defined a perform method and set a “queue_as” value:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :default&lt;br /&gt;
 &lt;br /&gt;
  def perform(*wiki)&lt;br /&gt;
   # Do something later&lt;br /&gt;
  wiki.update_contents&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There is a perform method to be called when the job was first enqueued. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Adding a Job to the queue ===&lt;br /&gt;
If you wish your Job be processed as soon as the queuing system is free, you can enqueue a Job like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.perform_later wiki&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can add a Job be performed tomorrow at noon:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait_until: Date.tomorrow.noon).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you want your Job be performed a week from now, most of the queueing backends ( Sidekiq&lt;br /&gt;
, Delayed Job, etc. ) allow you to set a delay time.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait: 1.week).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Execution of Job ===&lt;br /&gt;
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]).&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt; Without setting any adapter, the job would be performed immediately.&amp;lt;br&amp;gt;&lt;br /&gt;
Queueing backend can be set at: /config/application.rb, in this example we use the Sidekiq.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    # Be sure to have the adapter's gem in your Gemfile and follow&lt;br /&gt;
    # the adapter's specific installation and deployment instructions.&lt;br /&gt;
    config.active_job.queue_adapter = :sidekiq&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Play with Queues ===&lt;br /&gt;
Active Job allows to schedule the job to be processed on a specific queue, this became helpful as common adapters support multiple queues.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Queue name can be prefixed for all jobs using config.active_job.queue_name_prefixin application.rb:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# config/application.rb&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    config.active_job.queue_name_prefix = Rails.env&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# app/jobs/update_wiki_job.rb&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# Now your job will run on queue production_low_priority on your&lt;br /&gt;
# production environment and on staging_low_priority on your staging&lt;br /&gt;
# environment&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can control a job to run on a queue you like, by passing a :queue option to #set :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
MyJob.set(queue: :another_queue).perform_later(record)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the code snippets are referred from the source&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
In this section, some examples are provided to demonstrate how to use ActiveJob in practices.&lt;br /&gt;
=== Background Mail Sender ===&lt;br /&gt;
You can send emails asynchronously with Action Mailer which Active Job is already integrated in.&amp;lt;ref name= &amp;quot;Action-Mailer-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/action_mailer_basics.html “Action Mailer Basics” 2014]&amp;lt;/ref&amp;gt; Here in this example, we try to use Active Job with Action Mailer.&amp;lt;br&amp;gt;&lt;br /&gt;
==== 1. Getting started with Rails:&amp;lt;ref name= &amp;quot;Welcome_to_Rails&amp;quot;&amp;gt;GitHub [https://github.com/rails/rails “Welcome to Rails” 2015]&amp;lt;/ref&amp;gt; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Install Rails if you haven’t done it yet:&lt;br /&gt;
$ gem install rails&lt;br /&gt;
&lt;br /&gt;
# Create a new Rails application, “myapp_activejob”&lt;br /&gt;
# is the name of the application:&lt;br /&gt;
$ rails new myapp_activejob&lt;br /&gt;
&lt;br /&gt;
# Change your directory to myapp_activejob&lt;br /&gt;
$ cd myapp_activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Edit the Gemfile in the folder, add following into it:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'responders'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Run “ $ bundle update “ and make sure “$ rails server” works.&lt;br /&gt;
&lt;br /&gt;
==== 2. Resque setup ====&lt;br /&gt;
We will use [https://github.com/resque/resque Resque] as the enqueuing backend. You need to install [http://redis.io/ Redis] before you can run Resque. You can getting Redis with [http://brew.sh/ Homebrew]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ brew install redis&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can download, extract and compile Redis with:&amp;lt;ref name= &amp;quot;Download_Redis&amp;quot;&amp;gt;Redis [http://redis.io/download “Download Redis” 2016]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ wget http://download.redis.io/releases/redis-3.0.7.tar.gz&lt;br /&gt;
$ tar xzf redis-3.0.7.tar.gz&lt;br /&gt;
$ cd redis-3.0.7&lt;br /&gt;
$ make&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Redis does not officially support Windows, but you can find  [https://github.com/MSOpenTech/redis Redis on Windows] by Microsoft Open Tech group.&amp;lt;br&amp;gt;&lt;br /&gt;
Next get Resque be installed. To use resque with Active Job, we also need resque-scheduler.&lt;br /&gt;
Add the following into Gemfile, and run “$ bundle install”.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'resque'&lt;br /&gt;
gem 'resque-scheduler'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After the installation, create a Resque configuration file resque.rb in config/initializers/&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/initializers/resque.rb&lt;br /&gt;
&lt;br /&gt;
Resque.redis = Redis.new(host: 'localhost', post: 6379)&lt;br /&gt;
Resque.after_fork = Proc.new { ActiveRecord::Base.establish_connection }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As the Resque tasks and Resque Scheduler rake tasks are needed in this example, we need to create a resque.rake file in lib/tasks/:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#lib/tasks/resque.rake&lt;br /&gt;
&lt;br /&gt;
require 'resque/tasks'&lt;br /&gt;
require 'resque/scheduler/tasks'&lt;br /&gt;
&lt;br /&gt;
namespace :resque do&lt;br /&gt;
  task setup: :environment do&lt;br /&gt;
    ENV['TERM_CHILD'] ||= '1'&lt;br /&gt;
    ENV['QUEUE'] ||= '*'&lt;br /&gt;
    require 'resque'&lt;br /&gt;
    require 'resque-scheduler'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== 3. Creating a Mailer ====&lt;br /&gt;
By doing the things above, we already have our Rails project and the Resque for queuing get set. Next we will creat a Mailer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate mailer user_mailer&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a method and a view for sending email.&amp;lt;br&amp;gt;&lt;br /&gt;
In app/mailer/user_mailer.rb:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/mailers/user_mailer.rb&lt;br /&gt;
&lt;br /&gt;
class UserMailer &amp;lt; ActionMailer::Base&lt;br /&gt;
  default from: 'from@example.com'&lt;br /&gt;
&lt;br /&gt;
  def follow_up_email(email)&lt;br /&gt;
    mail(&lt;br /&gt;
      to: email,&lt;br /&gt;
      subject: 'We hope you are enjoying our app'&lt;br /&gt;
    )&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In app/views/user_mailer/follow_up_email.text&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/views/user_mailer/follow_up_email.text&lt;br /&gt;
&lt;br /&gt;
Hey, we saw that you recently signed up for our app.&lt;br /&gt;
We hope you're enjoying it!&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== 4. Active Job====&lt;br /&gt;
Create a configuration file active_job.rb in config/initializers/ , to set Resque as queue_adapter.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/initializers/active_job.rb&lt;br /&gt;
&lt;br /&gt;
ActiveJob::Base.queue_adapter = :resque&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a Job with generator.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job follow_up_email&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the generated file app/jobs/user_registered_mailer_job.rb, define the perform method and set it’s queue_as.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/jobs/follow_up_email_job.rb&lt;br /&gt;
&lt;br /&gt;
class FollowUpEmailJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :email&lt;br /&gt;
&lt;br /&gt;
  def perform(email)&lt;br /&gt;
    UserMailer.follow_up_email(email).deliver_now&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can use a UserController to put the job into the queue for later execution. Here we set the email to be sent one minute later for test.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job follow_up_email&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/controllers/users_controller.rb&lt;br /&gt;
&lt;br /&gt;
class UsersController &amp;lt; ApplicationController&lt;br /&gt;
  def new&lt;br /&gt;
    @user = User.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def create&lt;br /&gt;
    @user = User.create(user_params)&lt;br /&gt;
    FollowUpEmailJob.new(@user.email).deliver_later!(wait: 1.minute)&lt;br /&gt;
    # redirect somewhere&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Routes and view need to set to make it work.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/routes.rb&lt;br /&gt;
&lt;br /&gt;
Rails.application.routes.draw do&lt;br /&gt;
  resources :users, only: [:new, :create]&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/views/users/new.html.erb&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= form_for @user do |f| %&amp;gt;&lt;br /&gt;
  &amp;lt;%= f.email_field :email %&amp;gt;&lt;br /&gt;
  &amp;lt;%= f.submit %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== 5. Try execution ====&lt;br /&gt;
Before we try the mailer, we need a “mailcatcher” for the test. Add following into Gemfile and “$ bundle install”&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'mailcatcher'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To set the environment, add following things into config/environments/development.rb.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/environments/development.rb&lt;br /&gt;
&lt;br /&gt;
Rails.application.configure do&lt;br /&gt;
  ...&lt;br /&gt;
  config.action_mailer.delivery_method = :smtp&lt;br /&gt;
  config.action_mailer.smtp_settings = { :address =&amp;gt; &amp;quot;localhost&amp;quot;, :port =&amp;gt; 1025 }&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run it!&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Start mailcatcher&lt;br /&gt;
$ mailcatcher&lt;br /&gt;
&lt;br /&gt;
# Start redis-server&lt;br /&gt;
$ redis-server&lt;br /&gt;
&lt;br /&gt;
# Start resque:work&lt;br /&gt;
$ bundle exec rake resque:work&lt;br /&gt;
&lt;br /&gt;
# Start resque:scheduler&lt;br /&gt;
$ rake environment resque:scheduler&lt;br /&gt;
&lt;br /&gt;
# Finally start rails server&lt;br /&gt;
$ rails server&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In your browser, view the Resque dashboard at http:/http://0.0.0.0:5678. In another tab, visit http://127.0.0.1:1080 to see the Mailcatcher dashboard.&lt;br /&gt;
&lt;br /&gt;
One minute later you can see following in Resque scheduler:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resque-scheduler: [INFO] 2016-02-05T22:53:15+09:00: Processing Delayed Items&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
And in mailcatcher:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
==&amp;gt; SMTP: Received message from '&amp;lt;from@example.com&amp;gt;' (315 bytes)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Yeah!&lt;br /&gt;
&lt;br /&gt;
=='''References'''==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Schen35</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100739</id>
		<title>Active Job</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100739"/>
		<updated>2016-02-05T21:17:32Z</updated>

		<summary type="html">&lt;p&gt;Schen35: /* How to use  */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
Active Job is a framework that helps 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 an interface that adapts different queueing backends like Backburner[https://github.com/nesquena/backburner], Delayed Job[https://github.com/collectiveidea/delayed_job], Qu[https://github.com/bkeepers/qu] and so on. &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” ]&amp;lt;/ref&amp;gt; Overall, Active Job is a interface which you can work with common queues.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
====Ruby On Rails versions support Active Job====&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;float:left;&amp;quot;&lt;br /&gt;
|+ Version history&lt;br /&gt;
|-&lt;br /&gt;
! Version !! Date&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || 2014/12/19&lt;br /&gt;
|-&lt;br /&gt;
| 4.2.5 || 2015/11/13&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
====Active Job adapters&amp;lt;ref name=&amp;quot;Active-Job-Adapter&amp;quot;&amp;gt;Active Job Adapter[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters.html&amp;quot;Active Job Adapter&amp;quot; ]&amp;lt;/ref&amp;gt;====&lt;br /&gt;
*Backburner[https://github.com/nesquena/backburner]&lt;br /&gt;
*Delayed Job[https://github.com/collectiveidea/delayed_job]&lt;br /&gt;
*Qu[https://github.com/bkeepers/qu]&lt;br /&gt;
*Que[https://github.com/chanks/que]&lt;br /&gt;
*queue_classic[https://github.com/QueueClassic/queue_classic]&lt;br /&gt;
*Resque 1.x[https://github.com/resque/resque]&lt;br /&gt;
*Sidekiq[http://sidekiq.org/]&lt;br /&gt;
*Sneakers[https://github.com/jondot/sneakers]&lt;br /&gt;
*Sucker Punch[https://github.com/brandonhilkert/sucker_punch]&lt;br /&gt;
*Active Job Async Job&lt;br /&gt;
*Active Job Inline[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters/InlineAdapter.html]&lt;br /&gt;
&lt;br /&gt;
=='''How to use &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/active_job_basics.html&amp;lt;/ref&amp;gt;'''==&lt;br /&gt;
We will introduce how to creating a job and and how to add the job into a queue. &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
=== Download ===&lt;br /&gt;
With RubyGems you can install the Active Job into your computer:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gem install activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://github.com/rails/rails/tree/master/activejob Souce code of Active Job] available on GitHub, as part of Rails.&amp;lt;ref&amp;gt; GitHub [https://github.com/rails/rails/tree/master/activejob “Active Job -- Make work happen later” 2015] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Create a Job ===&lt;br /&gt;
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.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki&lt;br /&gt;
  invoke  test_unit&lt;br /&gt;
  create  test/jobs/update_wiki_job_test.rb&lt;br /&gt;
  create  app/jobs/update_wiki_job.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Active Job provides the ability to run your Job on a specific queue by create a job as following:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki --queue urgent&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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:&amp;lt;ref&amp;gt; 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] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ApplicationJob &amp;lt; ActiveJob::Base&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 4.2 a Job class defined a perform method and set a “queue_as” value:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :default&lt;br /&gt;
 &lt;br /&gt;
  def perform(*wiki)&lt;br /&gt;
   # Do something later&lt;br /&gt;
  wiki.update_contents&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There is a perform method to be called when the job was first enqueued. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Adding a Job to the queue ===&lt;br /&gt;
If you wish your Job be processed as soon as the queuing system is free, you can enqueue a Job like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.perform_later wiki&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can add a Job be performed tomorrow at noon:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait_until: Date.tomorrow.noon).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you want your Job be performed a week from now, most of the queueing backends ( Sidekiq&lt;br /&gt;
, Delayed Job, etc. ) allow you to set a delay time.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait: 1.week).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Execution of Job ===&lt;br /&gt;
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]).&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt; Without setting any adapter, the job would be performed immediately.&amp;lt;br&amp;gt;&lt;br /&gt;
Queueing backend can be set at: /config/application.rb, in this example we use the Sidekiq.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    # Be sure to have the adapter's gem in your Gemfile and follow&lt;br /&gt;
    # the adapter's specific installation and deployment instructions.&lt;br /&gt;
    config.active_job.queue_adapter = :sidekiq&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Play with Queues ===&lt;br /&gt;
Active Job allows to schedule the job to be processed on a specific queue, this became helpful as common adapters support multiple queues.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Queue name can be prefixed for all jobs using config.active_job.queue_name_prefixin application.rb:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# config/application.rb&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    config.active_job.queue_name_prefix = Rails.env&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# app/jobs/update_wiki_job.rb&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# Now your job will run on queue production_low_priority on your&lt;br /&gt;
# production environment and on staging_low_priority on your staging&lt;br /&gt;
# environment&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can control a job to run on a queue you like, by passing a :queue option to #set :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
MyJob.set(queue: :another_queue).perform_later(record)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the code snippets are referred from the source&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
In this section, some examples are provided to demonstrate how to use ActiveJob in practices.&lt;br /&gt;
=== Background Mail Sender ===&lt;br /&gt;
You can send emails asynchronously with Action Mailer which Active Job is already integrated in.&amp;lt;ref name= &amp;quot;Action-Mailer-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/action_mailer_basics.html “Action Mailer Basics” 2014]&amp;lt;/ref&amp;gt; Here in this example, we try to use Active Job with Action Mailer.&amp;lt;br&amp;gt;&lt;br /&gt;
==== 1. Getting started with Rails:&amp;lt;ref name= &amp;quot;Welcome_to_Rails&amp;quot;&amp;gt;GitHub [https://github.com/rails/rails “Welcome to Rails” 2015]&amp;lt;/ref&amp;gt; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Install Rails if you haven’t done it yet:&lt;br /&gt;
$ gem install rails&lt;br /&gt;
&lt;br /&gt;
# Create a new Rails application, “myapp_activejob”&lt;br /&gt;
# is the name of the application:&lt;br /&gt;
$ rails new myapp_activejob&lt;br /&gt;
&lt;br /&gt;
# Change your directory to myapp_activejob&lt;br /&gt;
$ cd myapp_activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Edit the Gemfile in the folder, add following into it:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'responders'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Run “ $ bundle update “ and make sure “$ rails server” works.&lt;br /&gt;
&lt;br /&gt;
==== 2. Resque setup ====&lt;br /&gt;
We will use [https://github.com/resque/resque Resque] as the enqueuing backend. You need to install [http://redis.io/ Redis] before you can run Resque. You can getting Redis with [http://brew.sh/ Homebrew]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ brew install redis&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can download, extract and compile Redis with:&amp;lt;ref name= &amp;quot;Download_Redis&amp;quot;&amp;gt;Redis [http://redis.io/download “Download Redis” 2016]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ wget http://download.redis.io/releases/redis-3.0.7.tar.gz&lt;br /&gt;
$ tar xzf redis-3.0.7.tar.gz&lt;br /&gt;
$ cd redis-3.0.7&lt;br /&gt;
$ make&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Redis does not officially support Windows, but you can find  [https://github.com/MSOpenTech/redis Redis on Windows] by Microsoft Open Tech group.&amp;lt;br&amp;gt;&lt;br /&gt;
Next get Resque be installed. To use resque with Active Job, we also need resque-scheduler.&lt;br /&gt;
Add the following into Gemfile, and run “$ bundle install”.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'resque'&lt;br /&gt;
gem 'resque-scheduler'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After the installation, create a Resque configuration file resque.rb in config/initializers/&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/initializers/resque.rb&lt;br /&gt;
&lt;br /&gt;
Resque.redis = Redis.new(host: 'localhost', post: 6379)&lt;br /&gt;
Resque.after_fork = Proc.new { ActiveRecord::Base.establish_connection }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As the Resque tasks and Resque Scheduler rake tasks are needed in this example, we need to create a resque.rake file in lib/tasks/:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#lib/tasks/resque.rake&lt;br /&gt;
&lt;br /&gt;
require 'resque/tasks'&lt;br /&gt;
require 'resque/scheduler/tasks'&lt;br /&gt;
&lt;br /&gt;
namespace :resque do&lt;br /&gt;
  task setup: :environment do&lt;br /&gt;
    ENV['TERM_CHILD'] ||= '1'&lt;br /&gt;
    ENV['QUEUE'] ||= '*'&lt;br /&gt;
    require 'resque'&lt;br /&gt;
    require 'resque-scheduler'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== 3. Creating a Mailer ====&lt;br /&gt;
By doing the things above, we already have our Rails project and the Resque for queuing get set. Next we will creat a Mailer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate mailer user_mailer&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a method and a view for sending email.&amp;lt;br&amp;gt;&lt;br /&gt;
In app/mailer/user_mailer.rb:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/mailers/user_mailer.rb&lt;br /&gt;
&lt;br /&gt;
class UserMailer &amp;lt; ActionMailer::Base&lt;br /&gt;
  default from: 'from@example.com'&lt;br /&gt;
&lt;br /&gt;
  def follow_up_email(email)&lt;br /&gt;
    mail(&lt;br /&gt;
      to: email,&lt;br /&gt;
      subject: 'We hope you are enjoying our app'&lt;br /&gt;
    )&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In app/views/user_mailer/follow_up_email.text&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/views/user_mailer/follow_up_email.text&lt;br /&gt;
&lt;br /&gt;
Hey, we saw that you recently signed up for our app.&lt;br /&gt;
We hope you're enjoying it!&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== 4. Active Job====&lt;br /&gt;
Create a configuration file active_job.rb in config/initializers/ , to set Resque as queue_adapter.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/initializers/active_job.rb&lt;br /&gt;
&lt;br /&gt;
ActiveJob::Base.queue_adapter = :resque&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a Job with generator.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job follow_up_email&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the generated file app/jobs/user_registered_mailer_job.rb, define the perform method and set it’s queue_as.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/jobs/follow_up_email_job.rb&lt;br /&gt;
&lt;br /&gt;
class FollowUpEmailJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :email&lt;br /&gt;
&lt;br /&gt;
  def perform(email)&lt;br /&gt;
    UserMailer.follow_up_email(email).deliver_now&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can use a UserController to put the job into the queue for later execution. Here we set the email to be sent one minute later for test.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job follow_up_email&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/controllers/users_controller.rb&lt;br /&gt;
&lt;br /&gt;
class UsersController &amp;lt; ApplicationController&lt;br /&gt;
  def new&lt;br /&gt;
    @user = User.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def create&lt;br /&gt;
    @user = User.create(user_params)&lt;br /&gt;
    FollowUpEmailJob.new(@user.email).deliver_later!(wait: 1.minute)&lt;br /&gt;
    # redirect somewhere&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Routes and view need to set to make it work.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/routes.rb&lt;br /&gt;
&lt;br /&gt;
Rails.application.routes.draw do&lt;br /&gt;
  resources :users, only: [:new, :create]&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/views/users/new.html.erb&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= form_for @user do |f| %&amp;gt;&lt;br /&gt;
  &amp;lt;%= f.email_field :email %&amp;gt;&lt;br /&gt;
  &amp;lt;%= f.submit %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== 5. Try execution ====&lt;br /&gt;
Before we try the mailer, we need a “mailcatcher” for the test. Add following into Gemfile and “$ bundle install”&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'mailcatcher'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To set the environment, add following things into config/environments/development.rb.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/environments/development.rb&lt;br /&gt;
&lt;br /&gt;
Rails.application.configure do&lt;br /&gt;
  ...&lt;br /&gt;
  config.action_mailer.delivery_method = :smtp&lt;br /&gt;
  config.action_mailer.smtp_settings = { :address =&amp;gt; &amp;quot;localhost&amp;quot;, :port =&amp;gt; 1025 }&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run it!&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Start mailcatcher&lt;br /&gt;
$ mailcatcher&lt;br /&gt;
&lt;br /&gt;
# Start redis-server&lt;br /&gt;
$ redis-server&lt;br /&gt;
&lt;br /&gt;
# Start resque:work&lt;br /&gt;
$ bundle exec rake resque:work&lt;br /&gt;
&lt;br /&gt;
# Start resque:scheduler&lt;br /&gt;
$ rake environment resque:scheduler&lt;br /&gt;
&lt;br /&gt;
# Finally start rails server&lt;br /&gt;
$ rails server&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In your browser, view the Resque dashboard at http:/http://0.0.0.0:5678. In another tab, visit http://127.0.0.1:1080 to see the Mailcatcher dashboard.&lt;br /&gt;
&lt;br /&gt;
One minute later you can see following in Resque scheduler:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resque-scheduler: [INFO] 2016-02-05T22:53:15+09:00: Processing Delayed Items&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
And in mailcatcher:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
==&amp;gt; SMTP: Received message from '&amp;lt;from@example.com&amp;gt;' (315 bytes)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Yeah!&lt;br /&gt;
&lt;br /&gt;
=='''References'''==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Schen35</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100738</id>
		<title>Active Job</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100738"/>
		<updated>2016-02-05T21:16:17Z</updated>

		<summary type="html">&lt;p&gt;Schen35: /* 4. Active Job: */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
Active Job is a framework that helps 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 an interface that adapts different queueing backends like Backburner[https://github.com/nesquena/backburner], Delayed Job[https://github.com/collectiveidea/delayed_job], Qu[https://github.com/bkeepers/qu] and so on. &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” ]&amp;lt;/ref&amp;gt; Overall, Active Job is a interface which you can work with common queues.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
====Ruby On Rails versions support Active Job====&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;float:left;&amp;quot;&lt;br /&gt;
|+ Version history&lt;br /&gt;
|-&lt;br /&gt;
! Version !! Date&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || 2014/12/19&lt;br /&gt;
|-&lt;br /&gt;
| 4.2.5 || 2015/11/13&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
====Active Job adapters&amp;lt;ref name=&amp;quot;Active-Job-Adapter&amp;quot;&amp;gt;Active Job Adapter[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters.html&amp;quot;Active Job Adapter&amp;quot; ]&amp;lt;/ref&amp;gt;====&lt;br /&gt;
*Backburner[https://github.com/nesquena/backburner]&lt;br /&gt;
*Delayed Job[https://github.com/collectiveidea/delayed_job]&lt;br /&gt;
*Qu[https://github.com/bkeepers/qu]&lt;br /&gt;
*Que[https://github.com/chanks/que]&lt;br /&gt;
*queue_classic[https://github.com/QueueClassic/queue_classic]&lt;br /&gt;
*Resque 1.x[https://github.com/resque/resque]&lt;br /&gt;
*Sidekiq[http://sidekiq.org/]&lt;br /&gt;
*Sneakers[https://github.com/jondot/sneakers]&lt;br /&gt;
*Sucker Punch[https://github.com/brandonhilkert/sucker_punch]&lt;br /&gt;
*Active Job Async Job&lt;br /&gt;
*Active Job Inline[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters/InlineAdapter.html]&lt;br /&gt;
&lt;br /&gt;
=='''How to use &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;/&amp;gt;'''==&lt;br /&gt;
We will introduce how to creating a job and and how to add the job into a queue. &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;/&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
=== Download ===&lt;br /&gt;
With RubyGems you can install the Active Job into your computer:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gem install activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://github.com/rails/rails/tree/master/activejob Souce code of Active Job] available on GitHub, as part of Rails.&amp;lt;ref&amp;gt; GitHub [https://github.com/rails/rails/tree/master/activejob “Active Job -- Make work happen later” 2015] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Create a Job ===&lt;br /&gt;
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.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki&lt;br /&gt;
  invoke  test_unit&lt;br /&gt;
  create  test/jobs/update_wiki_job_test.rb&lt;br /&gt;
  create  app/jobs/update_wiki_job.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Active Job provides the ability to run your Job on a specific queue by create a job as following:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki --queue urgent&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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:&amp;lt;ref&amp;gt; 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] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ApplicationJob &amp;lt; ActiveJob::Base&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 4.2 a Job class looks like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :default&lt;br /&gt;
 &lt;br /&gt;
  def perform(*wiki)&lt;br /&gt;
   # Do something later&lt;br /&gt;
  wiki.update_contents&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There is a perform method to be called when the job was first enqueued. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Adding a Job to the queue ===&lt;br /&gt;
If you wish your Job be processed as soon as the queuing system is free, you can enqueue a Job like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.perform_later wiki&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can add a Job be performed tomorrow at noon:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait_until: Date.tomorrow.noon).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you want your Job be performed a week from now, most of the queueing backends ( Sidekiq&lt;br /&gt;
, Delayed Job, etc. ) allow you to set a delay time.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait: 1.week).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Execution of Job ===&lt;br /&gt;
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]).&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt; Without setting any adapter, the job would be performed immediately.&amp;lt;br&amp;gt;&lt;br /&gt;
Queueing backend can be set at: /config/application.rb, in this example we use the Sidekiq.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    # Be sure to have the adapter's gem in your Gemfile and follow&lt;br /&gt;
    # the adapter's specific installation and deployment instructions.&lt;br /&gt;
    config.active_job.queue_adapter = :sidekiq&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Examples ==&lt;br /&gt;
In this section, some examples are provided to demonstrate how to use ActiveJob in practices.&lt;br /&gt;
=== Background Mail Sender ===&lt;br /&gt;
You can send emails asynchronously with Action Mailer which Active Job is already integrated in.&amp;lt;ref name= &amp;quot;Action-Mailer-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/action_mailer_basics.html “Action Mailer Basics” 2014]&amp;lt;/ref&amp;gt; Here in this example, we try to use Active Job with Action Mailer.&amp;lt;br&amp;gt;&lt;br /&gt;
==== 1. Getting started with Rails:&amp;lt;ref name= &amp;quot;Welcome_to_Rails&amp;quot;&amp;gt;GitHub [https://github.com/rails/rails “Welcome to Rails” 2015]&amp;lt;/ref&amp;gt; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Install Rails if you haven’t done it yet:&lt;br /&gt;
$ gem install rails&lt;br /&gt;
&lt;br /&gt;
# Create a new Rails application, “myapp_activejob”&lt;br /&gt;
# is the name of the application:&lt;br /&gt;
$ rails new myapp_activejob&lt;br /&gt;
&lt;br /&gt;
# Change your directory to myapp_activejob&lt;br /&gt;
$ cd myapp_activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Edit the Gemfile in the folder, add following into it:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'responders'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Run “ $ bundle update “ and make sure “$ rails server” works.&lt;br /&gt;
&lt;br /&gt;
==== 2. Resque setup ====&lt;br /&gt;
We will use [https://github.com/resque/resque Resque] as the enqueuing backend. You need to install [http://redis.io/ Redis] before you can run Resque. You can getting Redis with [http://brew.sh/ Homebrew]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ brew install redis&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can download, extract and compile Redis with:&amp;lt;ref name= &amp;quot;Download_Redis&amp;quot;&amp;gt;Redis [http://redis.io/download “Download Redis” 2016]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ wget http://download.redis.io/releases/redis-3.0.7.tar.gz&lt;br /&gt;
$ tar xzf redis-3.0.7.tar.gz&lt;br /&gt;
$ cd redis-3.0.7&lt;br /&gt;
$ make&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Redis does not officially support Windows, but you can find  [https://github.com/MSOpenTech/redis Redis on Windows] by Microsoft Open Tech group.&amp;lt;br&amp;gt;&lt;br /&gt;
Next get Resque be installed. To use resque with Active Job, we also need resque-scheduler.&lt;br /&gt;
Add the following into Gemfile, and run “$ bundle install”.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'resque'&lt;br /&gt;
gem 'resque-scheduler'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After the installation, create a Resque configuration file resque.rb in config/initializers/&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/initializers/resque.rb&lt;br /&gt;
&lt;br /&gt;
Resque.redis = Redis.new(host: 'localhost', post: 6379)&lt;br /&gt;
Resque.after_fork = Proc.new { ActiveRecord::Base.establish_connection }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As the Resque tasks and Resque Scheduler rake tasks are needed in this example, we need to create a resque.rake file in lib/tasks/:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#lib/tasks/resque.rake&lt;br /&gt;
&lt;br /&gt;
require 'resque/tasks'&lt;br /&gt;
require 'resque/scheduler/tasks'&lt;br /&gt;
&lt;br /&gt;
namespace :resque do&lt;br /&gt;
  task setup: :environment do&lt;br /&gt;
    ENV['TERM_CHILD'] ||= '1'&lt;br /&gt;
    ENV['QUEUE'] ||= '*'&lt;br /&gt;
    require 'resque'&lt;br /&gt;
    require 'resque-scheduler'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== 3. Creating a Mailer ====&lt;br /&gt;
By doing the things above, we already have our Rails project and the Resque for queuing get set. Next we will creat a Mailer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate mailer user_mailer&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a method and a view for sending email.&amp;lt;br&amp;gt;&lt;br /&gt;
In app/mailer/user_mailer.rb:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/mailers/user_mailer.rb&lt;br /&gt;
&lt;br /&gt;
class UserMailer &amp;lt; ActionMailer::Base&lt;br /&gt;
  default from: 'from@example.com'&lt;br /&gt;
&lt;br /&gt;
  def follow_up_email(email)&lt;br /&gt;
    mail(&lt;br /&gt;
      to: email,&lt;br /&gt;
      subject: 'We hope you are enjoying our app'&lt;br /&gt;
    )&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In app/views/user_mailer/follow_up_email.text&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/views/user_mailer/follow_up_email.text&lt;br /&gt;
&lt;br /&gt;
Hey, we saw that you recently signed up for our app.&lt;br /&gt;
We hope you're enjoying it!&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== 4. Active Job====&lt;br /&gt;
Create a configuration file active_job.rb in config/initializers/ , to set Resque as queue_adapter.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/initializers/active_job.rb&lt;br /&gt;
&lt;br /&gt;
ActiveJob::Base.queue_adapter = :resque&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a Job with generator.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job follow_up_email&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the generated file app/jobs/user_registered_mailer_job.rb, define the perform method and set it’s queue_as.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/jobs/follow_up_email_job.rb&lt;br /&gt;
&lt;br /&gt;
class FollowUpEmailJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :email&lt;br /&gt;
&lt;br /&gt;
  def perform(email)&lt;br /&gt;
    UserMailer.follow_up_email(email).deliver_now&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can use a UserController to put the job into the queue for later execution. Here we set the email to be sent one minute later for test.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job follow_up_email&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/controllers/users_controller.rb&lt;br /&gt;
&lt;br /&gt;
class UsersController &amp;lt; ApplicationController&lt;br /&gt;
  def new&lt;br /&gt;
    @user = User.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def create&lt;br /&gt;
    @user = User.create(user_params)&lt;br /&gt;
    FollowUpEmailJob.new(@user.email).deliver_later!(wait: 1.minute)&lt;br /&gt;
    # redirect somewhere&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Routes and view need to set to make it work.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/routes.rb&lt;br /&gt;
&lt;br /&gt;
Rails.application.routes.draw do&lt;br /&gt;
  resources :users, only: [:new, :create]&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/views/users/new.html.erb&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= form_for @user do |f| %&amp;gt;&lt;br /&gt;
  &amp;lt;%= f.email_field :email %&amp;gt;&lt;br /&gt;
  &amp;lt;%= f.submit %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== 5. Try execution ====&lt;br /&gt;
Before we try the mailer, we need a “mailcatcher” for the test. Add following into Gemfile and “$ bundle install”&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'mailcatcher'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To set the environment, add following things into config/environments/development.rb.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/environments/development.rb&lt;br /&gt;
&lt;br /&gt;
Rails.application.configure do&lt;br /&gt;
  ...&lt;br /&gt;
  config.action_mailer.delivery_method = :smtp&lt;br /&gt;
  config.action_mailer.smtp_settings = { :address =&amp;gt; &amp;quot;localhost&amp;quot;, :port =&amp;gt; 1025 }&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run it!&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Start mailcatcher&lt;br /&gt;
$ mailcatcher&lt;br /&gt;
&lt;br /&gt;
# Start redis-server&lt;br /&gt;
$ redis-server&lt;br /&gt;
&lt;br /&gt;
# Start resque:work&lt;br /&gt;
$ bundle exec rake resque:work&lt;br /&gt;
&lt;br /&gt;
# Start resque:scheduler&lt;br /&gt;
$ rake environment resque:scheduler&lt;br /&gt;
&lt;br /&gt;
# Finally start rails server&lt;br /&gt;
$ rails server&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In your browser, view the Resque dashboard at http:/http://0.0.0.0:5678. In another tab, visit http://127.0.0.1:1080 to see the Mailcatcher dashboard.&lt;br /&gt;
&lt;br /&gt;
One minute later you can see following in Resque scheduler:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resque-scheduler: [INFO] 2016-02-05T22:53:15+09:00: Processing Delayed Items&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
And in mailcatcher:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
==&amp;gt; SMTP: Received message from '&amp;lt;from@example.com&amp;gt;' (315 bytes)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Yeah!&lt;br /&gt;
&lt;br /&gt;
=='''References'''==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Schen35</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100737</id>
		<title>Active Job</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100737"/>
		<updated>2016-02-05T21:15:58Z</updated>

		<summary type="html">&lt;p&gt;Schen35: /* Examples */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
Active Job is a framework that helps 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 an interface that adapts different queueing backends like Backburner[https://github.com/nesquena/backburner], Delayed Job[https://github.com/collectiveidea/delayed_job], Qu[https://github.com/bkeepers/qu] and so on. &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” ]&amp;lt;/ref&amp;gt; Overall, Active Job is a interface which you can work with common queues.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
====Ruby On Rails versions support Active Job====&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;float:left;&amp;quot;&lt;br /&gt;
|+ Version history&lt;br /&gt;
|-&lt;br /&gt;
! Version !! Date&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || 2014/12/19&lt;br /&gt;
|-&lt;br /&gt;
| 4.2.5 || 2015/11/13&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
====Active Job adapters&amp;lt;ref name=&amp;quot;Active-Job-Adapter&amp;quot;&amp;gt;Active Job Adapter[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters.html&amp;quot;Active Job Adapter&amp;quot; ]&amp;lt;/ref&amp;gt;====&lt;br /&gt;
*Backburner[https://github.com/nesquena/backburner]&lt;br /&gt;
*Delayed Job[https://github.com/collectiveidea/delayed_job]&lt;br /&gt;
*Qu[https://github.com/bkeepers/qu]&lt;br /&gt;
*Que[https://github.com/chanks/que]&lt;br /&gt;
*queue_classic[https://github.com/QueueClassic/queue_classic]&lt;br /&gt;
*Resque 1.x[https://github.com/resque/resque]&lt;br /&gt;
*Sidekiq[http://sidekiq.org/]&lt;br /&gt;
*Sneakers[https://github.com/jondot/sneakers]&lt;br /&gt;
*Sucker Punch[https://github.com/brandonhilkert/sucker_punch]&lt;br /&gt;
*Active Job Async Job&lt;br /&gt;
*Active Job Inline[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters/InlineAdapter.html]&lt;br /&gt;
&lt;br /&gt;
=='''How to use &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;/&amp;gt;'''==&lt;br /&gt;
We will introduce how to creating a job and and how to add the job into a queue. &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;/&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
=== Download ===&lt;br /&gt;
With RubyGems you can install the Active Job into your computer:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gem install activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://github.com/rails/rails/tree/master/activejob Souce code of Active Job] available on GitHub, as part of Rails.&amp;lt;ref&amp;gt; GitHub [https://github.com/rails/rails/tree/master/activejob “Active Job -- Make work happen later” 2015] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Create a Job ===&lt;br /&gt;
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.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki&lt;br /&gt;
  invoke  test_unit&lt;br /&gt;
  create  test/jobs/update_wiki_job_test.rb&lt;br /&gt;
  create  app/jobs/update_wiki_job.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Active Job provides the ability to run your Job on a specific queue by create a job as following:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki --queue urgent&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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:&amp;lt;ref&amp;gt; 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] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ApplicationJob &amp;lt; ActiveJob::Base&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 4.2 a Job class looks like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :default&lt;br /&gt;
 &lt;br /&gt;
  def perform(*wiki)&lt;br /&gt;
   # Do something later&lt;br /&gt;
  wiki.update_contents&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There is a perform method to be called when the job was first enqueued. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Adding a Job to the queue ===&lt;br /&gt;
If you wish your Job be processed as soon as the queuing system is free, you can enqueue a Job like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.perform_later wiki&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can add a Job be performed tomorrow at noon:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait_until: Date.tomorrow.noon).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you want your Job be performed a week from now, most of the queueing backends ( Sidekiq&lt;br /&gt;
, Delayed Job, etc. ) allow you to set a delay time.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait: 1.week).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Execution of Job ===&lt;br /&gt;
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]).&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt; Without setting any adapter, the job would be performed immediately.&amp;lt;br&amp;gt;&lt;br /&gt;
Queueing backend can be set at: /config/application.rb, in this example we use the Sidekiq.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    # Be sure to have the adapter's gem in your Gemfile and follow&lt;br /&gt;
    # the adapter's specific installation and deployment instructions.&lt;br /&gt;
    config.active_job.queue_adapter = :sidekiq&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Examples ==&lt;br /&gt;
In this section, some examples are provided to demonstrate how to use ActiveJob in practices.&lt;br /&gt;
=== Background Mail Sender ===&lt;br /&gt;
You can send emails asynchronously with Action Mailer which Active Job is already integrated in.&amp;lt;ref name= &amp;quot;Action-Mailer-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/action_mailer_basics.html “Action Mailer Basics” 2014]&amp;lt;/ref&amp;gt; Here in this example, we try to use Active Job with Action Mailer.&amp;lt;br&amp;gt;&lt;br /&gt;
==== 1. Getting started with Rails:&amp;lt;ref name= &amp;quot;Welcome_to_Rails&amp;quot;&amp;gt;GitHub [https://github.com/rails/rails “Welcome to Rails” 2015]&amp;lt;/ref&amp;gt; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Install Rails if you haven’t done it yet:&lt;br /&gt;
$ gem install rails&lt;br /&gt;
&lt;br /&gt;
# Create a new Rails application, “myapp_activejob”&lt;br /&gt;
# is the name of the application:&lt;br /&gt;
$ rails new myapp_activejob&lt;br /&gt;
&lt;br /&gt;
# Change your directory to myapp_activejob&lt;br /&gt;
$ cd myapp_activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Edit the Gemfile in the folder, add following into it:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'responders'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Run “ $ bundle update “ and make sure “$ rails server” works.&lt;br /&gt;
&lt;br /&gt;
==== 2. Resque setup ====&lt;br /&gt;
We will use [https://github.com/resque/resque Resque] as the enqueuing backend. You need to install [http://redis.io/ Redis] before you can run Resque. You can getting Redis with [http://brew.sh/ Homebrew]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ brew install redis&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can download, extract and compile Redis with:&amp;lt;ref name= &amp;quot;Download_Redis&amp;quot;&amp;gt;Redis [http://redis.io/download “Download Redis” 2016]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ wget http://download.redis.io/releases/redis-3.0.7.tar.gz&lt;br /&gt;
$ tar xzf redis-3.0.7.tar.gz&lt;br /&gt;
$ cd redis-3.0.7&lt;br /&gt;
$ make&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Redis does not officially support Windows, but you can find  [https://github.com/MSOpenTech/redis Redis on Windows] by Microsoft Open Tech group.&amp;lt;br&amp;gt;&lt;br /&gt;
Next get Resque be installed. To use resque with Active Job, we also need resque-scheduler.&lt;br /&gt;
Add the following into Gemfile, and run “$ bundle install”.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'resque'&lt;br /&gt;
gem 'resque-scheduler'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After the installation, create a Resque configuration file resque.rb in config/initializers/&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/initializers/resque.rb&lt;br /&gt;
&lt;br /&gt;
Resque.redis = Redis.new(host: 'localhost', post: 6379)&lt;br /&gt;
Resque.after_fork = Proc.new { ActiveRecord::Base.establish_connection }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As the Resque tasks and Resque Scheduler rake tasks are needed in this example, we need to create a resque.rake file in lib/tasks/:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#lib/tasks/resque.rake&lt;br /&gt;
&lt;br /&gt;
require 'resque/tasks'&lt;br /&gt;
require 'resque/scheduler/tasks'&lt;br /&gt;
&lt;br /&gt;
namespace :resque do&lt;br /&gt;
  task setup: :environment do&lt;br /&gt;
    ENV['TERM_CHILD'] ||= '1'&lt;br /&gt;
    ENV['QUEUE'] ||= '*'&lt;br /&gt;
    require 'resque'&lt;br /&gt;
    require 'resque-scheduler'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== 3. Creating a Mailer ====&lt;br /&gt;
By doing the things above, we already have our Rails project and the Resque for queuing get set. Next we will creat a Mailer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate mailer user_mailer&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a method and a view for sending email.&amp;lt;br&amp;gt;&lt;br /&gt;
In app/mailer/user_mailer.rb:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/mailers/user_mailer.rb&lt;br /&gt;
&lt;br /&gt;
class UserMailer &amp;lt; ActionMailer::Base&lt;br /&gt;
  default from: 'from@example.com'&lt;br /&gt;
&lt;br /&gt;
  def follow_up_email(email)&lt;br /&gt;
    mail(&lt;br /&gt;
      to: email,&lt;br /&gt;
      subject: 'We hope you are enjoying our app'&lt;br /&gt;
    )&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In app/views/user_mailer/follow_up_email.text&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/views/user_mailer/follow_up_email.text&lt;br /&gt;
&lt;br /&gt;
Hey, we saw that you recently signed up for our app.&lt;br /&gt;
We hope you're enjoying it!&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== 4. Active Job:====&lt;br /&gt;
Create a configuration file active_job.rb in config/initializers/ , to set Resque as queue_adapter.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/initializers/active_job.rb&lt;br /&gt;
&lt;br /&gt;
ActiveJob::Base.queue_adapter = :resque&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a Job with generator.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job follow_up_email&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the generated file app/jobs/user_registered_mailer_job.rb, define the perform method and set it’s queue_as.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/jobs/follow_up_email_job.rb&lt;br /&gt;
&lt;br /&gt;
class FollowUpEmailJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :email&lt;br /&gt;
&lt;br /&gt;
  def perform(email)&lt;br /&gt;
    UserMailer.follow_up_email(email).deliver_now&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can use a UserController to put the job into the queue for later execution. Here we set the email to be sent one minute later for test.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job follow_up_email&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/controllers/users_controller.rb&lt;br /&gt;
&lt;br /&gt;
class UsersController &amp;lt; ApplicationController&lt;br /&gt;
  def new&lt;br /&gt;
    @user = User.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def create&lt;br /&gt;
    @user = User.create(user_params)&lt;br /&gt;
    FollowUpEmailJob.new(@user.email).deliver_later!(wait: 1.minute)&lt;br /&gt;
    # redirect somewhere&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Routes and view need to set to make it work.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/routes.rb&lt;br /&gt;
&lt;br /&gt;
Rails.application.routes.draw do&lt;br /&gt;
  resources :users, only: [:new, :create]&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/views/users/new.html.erb&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= form_for @user do |f| %&amp;gt;&lt;br /&gt;
  &amp;lt;%= f.email_field :email %&amp;gt;&lt;br /&gt;
  &amp;lt;%= f.submit %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== 5. Try execution ====&lt;br /&gt;
Before we try the mailer, we need a “mailcatcher” for the test. Add following into Gemfile and “$ bundle install”&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'mailcatcher'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To set the environment, add following things into config/environments/development.rb.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/environments/development.rb&lt;br /&gt;
&lt;br /&gt;
Rails.application.configure do&lt;br /&gt;
  ...&lt;br /&gt;
  config.action_mailer.delivery_method = :smtp&lt;br /&gt;
  config.action_mailer.smtp_settings = { :address =&amp;gt; &amp;quot;localhost&amp;quot;, :port =&amp;gt; 1025 }&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run it!&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Start mailcatcher&lt;br /&gt;
$ mailcatcher&lt;br /&gt;
&lt;br /&gt;
# Start redis-server&lt;br /&gt;
$ redis-server&lt;br /&gt;
&lt;br /&gt;
# Start resque:work&lt;br /&gt;
$ bundle exec rake resque:work&lt;br /&gt;
&lt;br /&gt;
# Start resque:scheduler&lt;br /&gt;
$ rake environment resque:scheduler&lt;br /&gt;
&lt;br /&gt;
# Finally start rails server&lt;br /&gt;
$ rails server&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In your browser, view the Resque dashboard at http:/http://0.0.0.0:5678. In another tab, visit http://127.0.0.1:1080 to see the Mailcatcher dashboard.&lt;br /&gt;
&lt;br /&gt;
One minute later you can see following in Resque scheduler:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resque-scheduler: [INFO] 2016-02-05T22:53:15+09:00: Processing Delayed Items&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
And in mailcatcher:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
==&amp;gt; SMTP: Received message from '&amp;lt;from@example.com&amp;gt;' (315 bytes)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Yeah!&lt;br /&gt;
&lt;br /&gt;
=='''References'''==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Schen35</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100736</id>
		<title>Active Job</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100736"/>
		<updated>2016-02-05T21:14:57Z</updated>

		<summary type="html">&lt;p&gt;Schen35: /* Play with Queues */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
Active Job is a framework that helps 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 an interface that adapts different queueing backends like Backburner[https://github.com/nesquena/backburner], Delayed Job[https://github.com/collectiveidea/delayed_job], Qu[https://github.com/bkeepers/qu] and so on. &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” ]&amp;lt;/ref&amp;gt; Overall, Active Job is a interface which you can work with common queues.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
====Ruby On Rails versions support Active Job====&lt;br /&gt;
{| class=&amp;quot;wikitable sortable&amp;quot; style=&amp;quot;float:left;&amp;quot;&lt;br /&gt;
|+ Version history&lt;br /&gt;
|-&lt;br /&gt;
! Version !! Date&lt;br /&gt;
|-&lt;br /&gt;
| 4.2 || 2014/12/19&lt;br /&gt;
|-&lt;br /&gt;
| 4.2.5 || 2015/11/13&lt;br /&gt;
|-&lt;br /&gt;
|}&lt;br /&gt;
&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
====Active Job adapters&amp;lt;ref name=&amp;quot;Active-Job-Adapter&amp;quot;&amp;gt;Active Job Adapter[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters.html&amp;quot;Active Job Adapter&amp;quot; ]&amp;lt;/ref&amp;gt;====&lt;br /&gt;
*Backburner[https://github.com/nesquena/backburner]&lt;br /&gt;
*Delayed Job[https://github.com/collectiveidea/delayed_job]&lt;br /&gt;
*Qu[https://github.com/bkeepers/qu]&lt;br /&gt;
*Que[https://github.com/chanks/que]&lt;br /&gt;
*queue_classic[https://github.com/QueueClassic/queue_classic]&lt;br /&gt;
*Resque 1.x[https://github.com/resque/resque]&lt;br /&gt;
*Sidekiq[http://sidekiq.org/]&lt;br /&gt;
*Sneakers[https://github.com/jondot/sneakers]&lt;br /&gt;
*Sucker Punch[https://github.com/brandonhilkert/sucker_punch]&lt;br /&gt;
*Active Job Async Job&lt;br /&gt;
*Active Job Inline[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters/InlineAdapter.html]&lt;br /&gt;
&lt;br /&gt;
=='''How to use &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;/&amp;gt;'''==&lt;br /&gt;
We will introduce how to creating a job and and how to add the job into a queue. &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;/&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
=== Download ===&lt;br /&gt;
With RubyGems you can install the Active Job into your computer:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gem install activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://github.com/rails/rails/tree/master/activejob Souce code of Active Job] available on GitHub, as part of Rails.&amp;lt;ref&amp;gt; GitHub [https://github.com/rails/rails/tree/master/activejob “Active Job -- Make work happen later” 2015] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Create a Job ===&lt;br /&gt;
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.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki&lt;br /&gt;
  invoke  test_unit&lt;br /&gt;
  create  test/jobs/update_wiki_job_test.rb&lt;br /&gt;
  create  app/jobs/update_wiki_job.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Active Job provides the ability to run your Job on a specific queue by create a job as following:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki --queue urgent&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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:&amp;lt;ref&amp;gt; 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] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ApplicationJob &amp;lt; ActiveJob::Base&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 4.2 a Job class looks like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :default&lt;br /&gt;
 &lt;br /&gt;
  def perform(*wiki)&lt;br /&gt;
   # Do something later&lt;br /&gt;
  wiki.update_contents&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There is a perform method to be called when the job was first enqueued. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Adding a Job to the queue ===&lt;br /&gt;
If you wish your Job be processed as soon as the queuing system is free, you can enqueue a Job like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.perform_later wiki&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can add a Job be performed tomorrow at noon:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait_until: Date.tomorrow.noon).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you want your Job be performed a week from now, most of the queueing backends ( Sidekiq&lt;br /&gt;
, Delayed Job, etc. ) allow you to set a delay time.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait: 1.week).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Execution of Job ===&lt;br /&gt;
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]).&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt; Without setting any adapter, the job would be performed immediately.&amp;lt;br&amp;gt;&lt;br /&gt;
Queueing backend can be set at: /config/application.rb, in this example we use the Sidekiq.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    # Be sure to have the adapter's gem in your Gemfile and follow&lt;br /&gt;
    # the adapter's specific installation and deployment instructions.&lt;br /&gt;
    config.active_job.queue_adapter = :sidekiq&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
== Examples ==&lt;br /&gt;
In this section, some examples are provided to demonstrate how to use ActiveJob in practices.&lt;br /&gt;
=== Background Mail Sender ===&lt;br /&gt;
You can send emails asynchronously with Action Mailer which Active Job is already integrated in.&amp;lt;ref name= &amp;quot;Action-Mailer-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/action_mailer_basics.html “Action Mailer Basics” 2014]&amp;lt;/ref&amp;gt; Here in this example, we try to use Active Job with Action Mailer.&amp;lt;br&amp;gt;&lt;br /&gt;
==== 1. Getting started with Rails:&amp;lt;ref name= &amp;quot;Welcome_to_Rails&amp;quot;&amp;gt;GitHub [https://github.com/rails/rails “Welcome to Rails” 2015]&amp;lt;/ref&amp;gt; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Install Rails if you haven’t done it yet:&lt;br /&gt;
$ gem install rails&lt;br /&gt;
&lt;br /&gt;
# Create a new Rails application, “myapp_activejob”&lt;br /&gt;
# is the name of the application:&lt;br /&gt;
$ rails new myapp_activejob&lt;br /&gt;
&lt;br /&gt;
# Change your directory to myapp_activejob&lt;br /&gt;
$ cd myapp_activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Edit the Gemfile in the folder, add following into it:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'responders'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Run “ $ bundle update “ and make sure “$ rails server” works.&lt;br /&gt;
&lt;br /&gt;
==== 2. Resque setup ====&lt;br /&gt;
We will use [https://github.com/resque/resque Resque] as the enqueuing backend. You need to install [http://redis.io/ Redis] before you can run Resque. You can getting Redis with [http://brew.sh/ Homebrew]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ brew install redis&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can download, extract and compile Redis with:&amp;lt;ref name= &amp;quot;Download_Redis&amp;quot;&amp;gt;Redis [http://redis.io/download “Download Redis” 2016]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ wget http://download.redis.io/releases/redis-3.0.7.tar.gz&lt;br /&gt;
$ tar xzf redis-3.0.7.tar.gz&lt;br /&gt;
$ cd redis-3.0.7&lt;br /&gt;
$ make&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Redis does not officially support Windows, but you can find  [https://github.com/MSOpenTech/redis Redis on Windows] by Microsoft Open Tech group.&amp;lt;br&amp;gt;&lt;br /&gt;
Next get Resque be installed. To use resque with Active Job, we also need resque-scheduler.&lt;br /&gt;
Add the following into Gemfile, and run “$ bundle install”.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'resque'&lt;br /&gt;
gem 'resque-scheduler'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After the installation, create a Resque configuration file resque.rb in config/initializers/&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/initializers/resque.rb&lt;br /&gt;
&lt;br /&gt;
Resque.redis = Redis.new(host: 'localhost', post: 6379)&lt;br /&gt;
Resque.after_fork = Proc.new { ActiveRecord::Base.establish_connection }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As the Resque tasks and Resque Scheduler rake tasks are needed in this example, we need to create a resque.rake file in lib/tasks/:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#lib/tasks/resque.rake&lt;br /&gt;
&lt;br /&gt;
require 'resque/tasks'&lt;br /&gt;
require 'resque/scheduler/tasks'&lt;br /&gt;
&lt;br /&gt;
namespace :resque do&lt;br /&gt;
  task setup: :environment do&lt;br /&gt;
    ENV['TERM_CHILD'] ||= '1'&lt;br /&gt;
    ENV['QUEUE'] ||= '*'&lt;br /&gt;
    require 'resque'&lt;br /&gt;
    require 'resque-scheduler'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== 3. Creating a Mailer ====&lt;br /&gt;
By doing the things above, we already have our Rails project and the Resque for queuing get set. Next we will creat a Mailer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate mailer user_mailer&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a method and a view for sending email.&amp;lt;br&amp;gt;&lt;br /&gt;
In app/mailer/user_mailer.rb:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/mailers/user_mailer.rb&lt;br /&gt;
&lt;br /&gt;
class UserMailer &amp;lt; ActionMailer::Base&lt;br /&gt;
  default from: 'from@example.com'&lt;br /&gt;
&lt;br /&gt;
  def follow_up_email(email)&lt;br /&gt;
    mail(&lt;br /&gt;
      to: email,&lt;br /&gt;
      subject: 'We hope you are enjoying our app'&lt;br /&gt;
    )&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In app/views/user_mailer/follow_up_email.text&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/views/user_mailer/follow_up_email.text&lt;br /&gt;
&lt;br /&gt;
Hey, we saw that you recently signed up for our app.&lt;br /&gt;
We hope you're enjoying it!&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== 4. Active Job:====&lt;br /&gt;
Create a configuration file active_job.rb in config/initializers/ , to set Resque as queue_adapter.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/initializers/active_job.rb&lt;br /&gt;
&lt;br /&gt;
ActiveJob::Base.queue_adapter = :resque&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a Job with generator.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job follow_up_email&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In the generated file app/jobs/user_registered_mailer_job.rb, define the perform method and set it’s queue_as.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/jobs/follow_up_email_job.rb&lt;br /&gt;
&lt;br /&gt;
class FollowUpEmailJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :email&lt;br /&gt;
&lt;br /&gt;
  def perform(email)&lt;br /&gt;
    UserMailer.follow_up_email(email).deliver_now&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now we can use a UserController to put the job into the queue for later execution. Here we set the email to be sent one minute later for test.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job follow_up_email&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/controllers/users_controller.rb&lt;br /&gt;
&lt;br /&gt;
class UsersController &amp;lt; ApplicationController&lt;br /&gt;
  def new&lt;br /&gt;
    @user = User.new&lt;br /&gt;
  end&lt;br /&gt;
&lt;br /&gt;
  def create&lt;br /&gt;
    @user = User.create(user_params)&lt;br /&gt;
    FollowUpEmailJob.new(@user.email).deliver_later!(wait: 1.minute)&lt;br /&gt;
    # redirect somewhere&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Routes and view need to set to make it work.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/routes.rb&lt;br /&gt;
&lt;br /&gt;
Rails.application.routes.draw do&lt;br /&gt;
  resources :users, only: [:new, :create]&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/views/users/new.html.erb&lt;br /&gt;
&lt;br /&gt;
&amp;lt;%= form_for @user do |f| %&amp;gt;&lt;br /&gt;
  &amp;lt;%= f.email_field :email %&amp;gt;&lt;br /&gt;
  &amp;lt;%= f.submit %&amp;gt;&lt;br /&gt;
&amp;lt;% end %&amp;gt;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== 5. Try execution ====&lt;br /&gt;
Before we try the mailer, we need a “mailcatcher” for the test. Add following into Gemfile and “$ bundle install”&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'mailcatcher'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To set the environment, add following things into config/environments/development.rb.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/environments/development.rb&lt;br /&gt;
&lt;br /&gt;
Rails.application.configure do&lt;br /&gt;
  ...&lt;br /&gt;
  config.action_mailer.delivery_method = :smtp&lt;br /&gt;
  config.action_mailer.smtp_settings = { :address =&amp;gt; &amp;quot;localhost&amp;quot;, :port =&amp;gt; 1025 }&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Run it!&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Start mailcatcher&lt;br /&gt;
$ mailcatcher&lt;br /&gt;
&lt;br /&gt;
# Start redis-server&lt;br /&gt;
$ redis-server&lt;br /&gt;
&lt;br /&gt;
# Start resque:work&lt;br /&gt;
$ bundle exec rake resque:work&lt;br /&gt;
&lt;br /&gt;
# Start resque:scheduler&lt;br /&gt;
$ rake environment resque:scheduler&lt;br /&gt;
&lt;br /&gt;
# Finally start rails server&lt;br /&gt;
$ rails server&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In your browser, view the Resque dashboard at http:/http://0.0.0.0:5678. In another tab, visit http://127.0.0.1:1080 to see the Mailcatcher dashboard.&lt;br /&gt;
&lt;br /&gt;
One minute later you can see following in Resque scheduler:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
resque-scheduler: [INFO] 2016-02-05T22:53:15+09:00: Processing Delayed Items&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
And in mailcatcher:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
==&amp;gt; SMTP: Received message from '&amp;lt;from@example.com&amp;gt;' (315 bytes)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Yeah!&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
In this section, some examples are provided to demonstrate how to use ActiveJob in practices.&lt;br /&gt;
=== Background Mail Sender ===&lt;br /&gt;
You can send emails asynchronously with Action Mailer which Active Job is already integrated in.&amp;lt;ref name= &amp;quot;Action-Mailer-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/action_mailer_basics.html “Action Mailer Basics” 2014]&amp;lt;/ref&amp;gt; Here in this example, we try to use Active Job with Action Mailer.&amp;lt;br&amp;gt;&lt;br /&gt;
==== 1. Getting started with Rails:&amp;lt;ref name= &amp;quot;Welcome_to_Rails&amp;quot;&amp;gt;GitHub [https://github.com/rails/rails “Welcome to Rails” 2015]&amp;lt;/ref&amp;gt; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Install Rails if you haven’t done it yet:&lt;br /&gt;
$ gem install rails&lt;br /&gt;
&lt;br /&gt;
# Create a new Rails application, “myapp_activejob”&lt;br /&gt;
# is the name of the application:&lt;br /&gt;
$ rails new myapp_activejob&lt;br /&gt;
&lt;br /&gt;
# Change your directory to myapp_activejob&lt;br /&gt;
$ cd myapp_activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Edit the Gemfile in the folder, add following into it:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'responders'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Run “ $ bundle update “ and make sure “$ rails server” works.&lt;br /&gt;
&lt;br /&gt;
==== 2. Resque setup ====&lt;br /&gt;
We will use [https://github.com/resque/resque Resque] as the enqueuing backend. You need to install [http://redis.io/ Redis] before you can run Resque. You can getting Redis with [http://brew.sh/ Homebrew]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ brew install redis&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can download, extract and compile Redis with:&amp;lt;ref name= &amp;quot;Download_Redis&amp;quot;&amp;gt;Redis [http://redis.io/download “Download Redis” 2016]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ wget http://download.redis.io/releases/redis-3.0.7.tar.gz&lt;br /&gt;
$ tar xzf redis-3.0.7.tar.gz&lt;br /&gt;
$ cd redis-3.0.7&lt;br /&gt;
$ make&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Redis does not officially support Windows, but you can find  [https://github.com/MSOpenTech/redis Redis on Windows] by Microsoft Open Tech group.&amp;lt;br&amp;gt;&lt;br /&gt;
Next get Resque be installed. To use resque with Active Job, we also need resque-scheduler.&lt;br /&gt;
Add the following into Gemfile, and run “$ bundle install”.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'resque'&lt;br /&gt;
gem 'resque-scheduler'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After the installation, create a Resque configuration file resque.rb in config/initializers/&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/initializers/resque.rb&lt;br /&gt;
&lt;br /&gt;
Resque.redis = Redis.new(host: 'localhost', post: 6379)&lt;br /&gt;
Resque.after_fork = Proc.new { ActiveRecord::Base.establish_connection }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As the Resque tasks and Resque Scheduler rake tasks are needed in this example, we need to create a resque.rake file in lib/tasks/:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#lib/tasks/resque.rake&lt;br /&gt;
&lt;br /&gt;
require 'resque/tasks'&lt;br /&gt;
require 'resque/scheduler/tasks'&lt;br /&gt;
&lt;br /&gt;
namespace :resque do&lt;br /&gt;
  task setup: :environment do&lt;br /&gt;
    ENV['TERM_CHILD'] ||= '1'&lt;br /&gt;
    ENV['QUEUE'] ||= '*'&lt;br /&gt;
    require 'resque'&lt;br /&gt;
    require 'resque-scheduler'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== 3. Creating a Mailer ====&lt;br /&gt;
By doing the things above, we already have our Rails project and the Resque for queuing get set. Next we will creat a Mailer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate mailer user_mailer&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a method and a view for sending email.&amp;lt;br&amp;gt;&lt;br /&gt;
In app/mailer/user_mailer.rb:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/mailers/user_mailer.rb&lt;br /&gt;
&lt;br /&gt;
class UserMailer &amp;lt; ActionMailer::Base&lt;br /&gt;
  default from: 'from@example.com'&lt;br /&gt;
&lt;br /&gt;
  def follow_up_email(email)&lt;br /&gt;
    mail(&lt;br /&gt;
      to: email,&lt;br /&gt;
      subject: 'We hope you are enjoying our app'&lt;br /&gt;
    )&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In app/views/user_mailer/follow_up_email.text&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/views/user_mailer/follow_up_email.text&lt;br /&gt;
&lt;br /&gt;
Hey, we saw that you recently signed up for our app.&lt;br /&gt;
We hope you're enjoying it!&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
4. Active Job:&lt;br /&gt;
Create a configuration file active_job.rb in config/initializers/ , to set Resque as queue_adapter.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/initializers/active_job.rb&lt;br /&gt;
&lt;br /&gt;
ActiveJob::Base.queue_adapter = :resque&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a Job to&lt;br /&gt;
&lt;br /&gt;
=='''References'''==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Schen35</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100730</id>
		<title>Active Job</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100730"/>
		<updated>2016-02-05T20:14:27Z</updated>

		<summary type="html">&lt;p&gt;Schen35: /* Examples */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
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[https://github.com/nesquena/backburner], Delayed Job[https://github.com/collectiveidea/delayed_job], Qu[https://github.com/bkeepers/qu] and so on. &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/active_job_basics.html&amp;lt;/ref&amp;gt; Overall, Active Job is a interface which you can work with common queues.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
====Active Job adapters&amp;lt;ref&amp;gt;http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters.html&amp;lt;/ref&amp;gt;====&lt;br /&gt;
*Backburner[https://github.com/nesquena/backburner]&lt;br /&gt;
*Delayed Job[https://github.com/collectiveidea/delayed_job]&lt;br /&gt;
*Qu[https://github.com/bkeepers/qu]&lt;br /&gt;
*Que[https://github.com/chanks/que]&lt;br /&gt;
*queue_classic[https://github.com/QueueClassic/queue_classic]&lt;br /&gt;
*Resque 1.x[https://github.com/resque/resque]&lt;br /&gt;
*Sidekiq[http://sidekiq.org/]&lt;br /&gt;
*Sneakers[https://github.com/jondot/sneakers]&lt;br /&gt;
*Sucker Punch[https://github.com/brandonhilkert/sucker_punch]&lt;br /&gt;
*Active Job Async Job&lt;br /&gt;
*Active Job Inline[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters/InlineAdapter.html]&lt;br /&gt;
&lt;br /&gt;
=='''How to use &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/active_job_basics.html&amp;lt;/ref&amp;gt;'''==&lt;br /&gt;
We will introduce how to creating a job and and how to add the job into a queue. &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
=== Download ===&lt;br /&gt;
With RubyGems you can install the Active Job into your computer:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gem install activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://github.com/rails/rails/tree/master/activejob Souce code of Active Job] available on GitHub, as part of Rails.&amp;lt;ref&amp;gt; GitHub [https://github.com/rails/rails/tree/master/activejob “Active Job -- Make work happen later” 2015] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Create a Job ===&lt;br /&gt;
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.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki&lt;br /&gt;
  invoke  test_unit&lt;br /&gt;
  create  test/jobs/update_wiki_job_test.rb&lt;br /&gt;
  create  app/jobs/update_wiki_job.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Active Job provides the ability to run your Job on a specific queue by create a job as following:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki --queue urgent&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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:&amp;lt;ref&amp;gt; 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] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ApplicationJob &amp;lt; ActiveJob::Base&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 4.2 a Job class looks like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :default&lt;br /&gt;
 &lt;br /&gt;
  def perform(*wiki)&lt;br /&gt;
   # Do something later&lt;br /&gt;
  wiki.update_contents&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There is a perform method to be called when the job was first enqueued. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Adding a Job to the queue ===&lt;br /&gt;
If you wish your Job be processed as soon as the queuing system is free, you can enqueue a Job like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.perform_later wiki&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can add a Job be performed tomorrow at noon:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait_until: Date.tomorrow.noon).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you want your Job be performed a week from now, most of the queueing backends ( Sidekiq&lt;br /&gt;
, Delayed Job, etc. ) allow you to set a delay time.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait: 1.week).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Execution of Job ===&lt;br /&gt;
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]).&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt; Without setting any adapter, the job would be performed immediately.&amp;lt;br&amp;gt;&lt;br /&gt;
Queueing backend can be set at: /config/application.rb, in this example we use the Sidekiq.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    # Be sure to have the adapter's gem in your Gemfile and follow&lt;br /&gt;
    # the adapter's specific installation and deployment instructions.&lt;br /&gt;
    config.active_job.queue_adapter = :sidekiq&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Play with Queues ===&lt;br /&gt;
Active Job allows to schedule the job to be processed on a specific queue, this became helpful as common adapters support multiple queues.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Queue name can be prefixed for all jobs using config.active_job.queue_name_prefixin application.rb:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# config/application.rb&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    config.active_job.queue_name_prefix = Rails.env&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# app/jobs/update_wiki_job.rb&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# Now your job will run on queue production_low_priority on your&lt;br /&gt;
# production environment and on staging_low_priority on your staging&lt;br /&gt;
# environment&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can control a job to run on a queue you like, by passing a :queue option to #set :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
MyJob.set(queue: :another_queue).perform_later(record)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the code snippets are referred from the source&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
In this section, some examples are provided to demonstrate how to use ActiveJob in practices.&lt;br /&gt;
=== Background Mail Sender ===&lt;br /&gt;
You can send emails asynchronously with Action Mailer which Active Job is already integrated in.&amp;lt;ref name= &amp;quot;Action-Mailer-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/action_mailer_basics.html “Action Mailer Basics” 2014]&amp;lt;/ref&amp;gt; Here in this example, we try to use Active Job with Action Mailer.&amp;lt;br&amp;gt;&lt;br /&gt;
==== 1. Getting started with Rails:&amp;lt;ref name= &amp;quot;Welcome_to_Rails&amp;quot;&amp;gt;GitHub [https://github.com/rails/rails “Welcome to Rails” 2015]&amp;lt;/ref&amp;gt; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Install Rails if you haven’t done it yet:&lt;br /&gt;
$ gem install rails&lt;br /&gt;
&lt;br /&gt;
# Create a new Rails application, “myapp_activejob”&lt;br /&gt;
# is the name of the application:&lt;br /&gt;
$ rails new myapp_activejob&lt;br /&gt;
&lt;br /&gt;
# Change your directory to myapp_activejob&lt;br /&gt;
$ cd myapp_activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Edit the Gemfile in the folder, add following into it:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'responders'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Run “ $ bundle update “ and make sure “$ rails server” works.&lt;br /&gt;
&lt;br /&gt;
==== 2. Resque setup ====&lt;br /&gt;
We will use [https://github.com/resque/resque Resque] as the enqueuing backend. You need to install [http://redis.io/ Redis] before you can run Resque. You can getting Redis with [http://brew.sh/ Homebrew]:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ brew install redis&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can download, extract and compile Redis with:&amp;lt;ref name= &amp;quot;Download_Redis&amp;quot;&amp;gt;Redis [http://redis.io/download “Download Redis” 2016]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ wget http://download.redis.io/releases/redis-3.0.7.tar.gz&lt;br /&gt;
$ tar xzf redis-3.0.7.tar.gz&lt;br /&gt;
$ cd redis-3.0.7&lt;br /&gt;
$ make&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Redis does not officially support Windows, but you can find  [https://github.com/MSOpenTech/redis Redis on Windows] by Microsoft Open Tech group.&amp;lt;br&amp;gt;&lt;br /&gt;
Next get Resque be installed. To use resque with Active Job, we also need resque-scheduler.&lt;br /&gt;
Add the following into Gemfile, and run “$ bundle install”.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'resque'&lt;br /&gt;
gem 'resque-scheduler'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
After the installation, create a Resque configuration file resque.rb in config/initializers/&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/initializers/resque.rb&lt;br /&gt;
&lt;br /&gt;
Resque.redis = Redis.new(host: 'localhost', post: 6379)&lt;br /&gt;
Resque.after_fork = Proc.new { ActiveRecord::Base.establish_connection }&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
As the Resque tasks and Resque Scheduler rake tasks are needed in this example, we need to create a resque.rake file in lib/tasks/:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#lib/tasks/resque.rake&lt;br /&gt;
&lt;br /&gt;
require 'resque/tasks'&lt;br /&gt;
require 'resque/scheduler/tasks'&lt;br /&gt;
&lt;br /&gt;
namespace :resque do&lt;br /&gt;
  task setup: :environment do&lt;br /&gt;
    ENV['TERM_CHILD'] ||= '1'&lt;br /&gt;
    ENV['QUEUE'] ||= '*'&lt;br /&gt;
    require 'resque'&lt;br /&gt;
    require 'resque-scheduler'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== 3. Creating a Mailer ====&lt;br /&gt;
By doing the things above, we already have our Rails project and the Resque for queuing get set. Next we will creat a Mailer.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate mailer user_mailer&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a method and a view for sending email.&amp;lt;br&amp;gt;&lt;br /&gt;
In app/mailer/user_mailer.rb:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/mailers/user_mailer.rb&lt;br /&gt;
&lt;br /&gt;
class UserMailer &amp;lt; ActionMailer::Base&lt;br /&gt;
  default from: 'from@example.com'&lt;br /&gt;
&lt;br /&gt;
  def follow_up_email(email)&lt;br /&gt;
    mail(&lt;br /&gt;
      to: email,&lt;br /&gt;
      subject: 'We hope you are enjoying our app'&lt;br /&gt;
    )&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In app/views/user_mailer/follow_up_email.text&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#app/views/user_mailer/follow_up_email.text&lt;br /&gt;
&lt;br /&gt;
Hey, we saw that you recently signed up for our app.&lt;br /&gt;
We hope you're enjoying it!&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
4. Active Job:&lt;br /&gt;
Create a configuration file active_job.rb in config/initializers/ , to set Resque as queue_adapter.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#config/initializers/active_job.rb&lt;br /&gt;
&lt;br /&gt;
ActiveJob::Base.queue_adapter = :resque&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Create a Job to&lt;br /&gt;
&lt;br /&gt;
=='''References'''==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Schen35</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100660</id>
		<title>Active Job</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100660"/>
		<updated>2016-02-03T20:00:36Z</updated>

		<summary type="html">&lt;p&gt;Schen35: /* Examples */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
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[https://github.com/nesquena/backburner], Delayed Job[https://github.com/collectiveidea/delayed_job], Qu[https://github.com/bkeepers/qu] and so on. &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/active_job_basics.html&amp;lt;/ref&amp;gt; Overall, Active Job is a interface which you can work with common queues.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
====Active Job adapters&amp;lt;ref&amp;gt;http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters.html&amp;lt;/ref&amp;gt;====&lt;br /&gt;
*Backburner[https://github.com/nesquena/backburner]&lt;br /&gt;
*Delayed Job[https://github.com/collectiveidea/delayed_job]&lt;br /&gt;
*Qu[https://github.com/bkeepers/qu]&lt;br /&gt;
*Que[https://github.com/chanks/que]&lt;br /&gt;
*queue_classic[https://github.com/QueueClassic/queue_classic]&lt;br /&gt;
*Resque 1.x[https://github.com/resque/resque]&lt;br /&gt;
*Sidekiq[http://sidekiq.org/]&lt;br /&gt;
*Sneakers[https://github.com/jondot/sneakers]&lt;br /&gt;
*Sucker Punch[https://github.com/brandonhilkert/sucker_punch]&lt;br /&gt;
*Active Job Async Job&lt;br /&gt;
*Active Job Inline[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters/InlineAdapter.html]&lt;br /&gt;
&lt;br /&gt;
=='''How to use &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/active_job_basics.html&amp;lt;/ref&amp;gt;'''==&lt;br /&gt;
We will introduce how to creating a job and and how to add the job into a queue. &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
=== Download ===&lt;br /&gt;
With RubyGems you can install the Active Job into your computer:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gem install activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://github.com/rails/rails/tree/master/activejob Souce code of Active Job] available on GitHub, as part of Rails.&amp;lt;ref&amp;gt; GitHub [https://github.com/rails/rails/tree/master/activejob “Active Job -- Make work happen later” 2015] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Create a Job ===&lt;br /&gt;
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.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki&lt;br /&gt;
  invoke  test_unit&lt;br /&gt;
  create  test/jobs/update_wiki_job_test.rb&lt;br /&gt;
  create  app/jobs/update_wiki_job.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Active Job provides the ability to run your Job on a specific queue by create a job as following:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki --queue urgent&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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:&amp;lt;ref&amp;gt; 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] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ApplicationJob &amp;lt; ActiveJob::Base&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 4.2 a Job class looks like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :default&lt;br /&gt;
 &lt;br /&gt;
  def perform(*wiki)&lt;br /&gt;
   # Do something later&lt;br /&gt;
  wiki.update_contents&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There is a perform method to be called when the job was first enqueued. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Adding a Job to the queue ===&lt;br /&gt;
If you wish your Job be processed as soon as the queuing system is free, you can enqueue a Job like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.perform_later wiki&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can add a Job be performed tomorrow at noon:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait_until: Date.tomorrow.noon).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you want your Job be performed a week from now, most of the queueing backends ( Sidekiq&lt;br /&gt;
, Delayed Job, etc. ) allow you to set a delay time.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait: 1.week).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Execution of Job ===&lt;br /&gt;
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]).&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt; Without setting any adapter, the job would be performed immediately.&amp;lt;br&amp;gt;&lt;br /&gt;
Queueing backend can be set at: /config/application.rb, in this example we use the Sidekiq.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    # Be sure to have the adapter's gem in your Gemfile and follow&lt;br /&gt;
    # the adapter's specific installation and deployment instructions.&lt;br /&gt;
    config.active_job.queue_adapter = :sidekiq&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Play with Queues ===&lt;br /&gt;
Active Job allows to schedule the job to be processed on a specific queue, this became helpful as common adapters support multiple queues.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Queue name can be prefixed for all jobs using config.active_job.queue_name_prefixin application.rb:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# config/application.rb&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    config.active_job.queue_name_prefix = Rails.env&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# app/jobs/update_wiki_job.rb&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# Now your job will run on queue production_low_priority on your&lt;br /&gt;
# production environment and on staging_low_priority on your staging&lt;br /&gt;
# environment&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can control a job to run on a queue you like, by passing a :queue option to #set :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
MyJob.set(queue: :another_queue).perform_later(record)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the code snippets are referred from the source&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
In this section, some examples are provided to demonstrate how to use ActiveJob in practices.&lt;br /&gt;
=== Background Mail Sender ===&lt;br /&gt;
You can send emails asynchronously with Action Mailer which Active Job is already integrated in.&amp;lt;ref name= &amp;quot;Action-Mailer-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/action_mailer_basics.html “Action Mailer Basics” 2014]&amp;lt;/ref&amp;gt; Here in this example, we try to use Active Job with Action Mailer.&amp;lt;br&amp;gt;&lt;br /&gt;
==== 1. Getting started with Rails:&amp;lt;ref name= &amp;quot;Welcome_to_Rails&amp;quot;&amp;gt;GitHub [https://github.com/rails/rails “Welcome to Rails” 2015]&amp;lt;/ref&amp;gt; ====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Install Rails if you haven’t done it yet:&lt;br /&gt;
$ gem install rails&lt;br /&gt;
&lt;br /&gt;
# Create a new Rails application, “myapp_activejob”&lt;br /&gt;
# is the name of the application:&lt;br /&gt;
$ rails new myapp_activejob&lt;br /&gt;
&lt;br /&gt;
# Change your directory to myapp_activejob&lt;br /&gt;
$ cd myapp_activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Edit the Gemfile in the folder, add following into it:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
gem 'responders'&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Run “ $ bundle update “ and make sure “$ rails server” works.&lt;br /&gt;
&lt;br /&gt;
=='''References'''==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Schen35</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100659</id>
		<title>Active Job</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100659"/>
		<updated>2016-02-02T20:54:16Z</updated>

		<summary type="html">&lt;p&gt;Schen35: /* Examples */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
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[https://github.com/nesquena/backburner], Delayed Job[https://github.com/collectiveidea/delayed_job], Qu[https://github.com/bkeepers/qu] and so on. &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/active_job_basics.html&amp;lt;/ref&amp;gt; Overall, Active Job is a interface which you can work with common queues.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
====Active Job adapters&amp;lt;ref&amp;gt;http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters.html&amp;lt;/ref&amp;gt;====&lt;br /&gt;
*Backburner[https://github.com/nesquena/backburner]&lt;br /&gt;
*Delayed Job[https://github.com/collectiveidea/delayed_job]&lt;br /&gt;
*Qu[https://github.com/bkeepers/qu]&lt;br /&gt;
*Que[https://github.com/chanks/que]&lt;br /&gt;
*queue_classic[https://github.com/QueueClassic/queue_classic]&lt;br /&gt;
*Resque 1.x[https://github.com/resque/resque]&lt;br /&gt;
*Sidekiq[http://sidekiq.org/]&lt;br /&gt;
*Sneakers[https://github.com/jondot/sneakers]&lt;br /&gt;
*Sucker Punch[https://github.com/brandonhilkert/sucker_punch]&lt;br /&gt;
*Active Job Async Job&lt;br /&gt;
*Active Job Inline[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters/InlineAdapter.html]&lt;br /&gt;
&lt;br /&gt;
=='''How to use &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/active_job_basics.html&amp;lt;/ref&amp;gt;'''==&lt;br /&gt;
We will introduce how to creating a job and and how to add the job into a queue. &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
=== Download ===&lt;br /&gt;
With RubyGems you can install the Active Job into your computer:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gem install activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://github.com/rails/rails/tree/master/activejob Souce code of Active Job] available on GitHub, as part of Rails.&amp;lt;ref&amp;gt; GitHub [https://github.com/rails/rails/tree/master/activejob “Active Job -- Make work happen later” 2015] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Create a Job ===&lt;br /&gt;
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.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki&lt;br /&gt;
  invoke  test_unit&lt;br /&gt;
  create  test/jobs/update_wiki_job_test.rb&lt;br /&gt;
  create  app/jobs/update_wiki_job.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Active Job provides the ability to run your Job on a specific queue by create a job as following:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki --queue urgent&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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:&amp;lt;ref&amp;gt; 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] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ApplicationJob &amp;lt; ActiveJob::Base&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 4.2 a Job class looks like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :default&lt;br /&gt;
 &lt;br /&gt;
  def perform(*wiki)&lt;br /&gt;
   # Do something later&lt;br /&gt;
  wiki.update_contents&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There is a perform method to be called when the job was first enqueued. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Adding a Job to the queue ===&lt;br /&gt;
If you wish your Job be processed as soon as the queuing system is free, you can enqueue a Job like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.perform_later wiki&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can add a Job be performed tomorrow at noon:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait_until: Date.tomorrow.noon).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you want your Job be performed a week from now, most of the queueing backends ( Sidekiq&lt;br /&gt;
, Delayed Job, etc. ) allow you to set a delay time.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait: 1.week).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Execution of Job ===&lt;br /&gt;
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]).&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt; Without setting any adapter, the job would be performed immediately.&amp;lt;br&amp;gt;&lt;br /&gt;
Queueing backend can be set at: /config/application.rb, in this example we use the Sidekiq.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    # Be sure to have the adapter's gem in your Gemfile and follow&lt;br /&gt;
    # the adapter's specific installation and deployment instructions.&lt;br /&gt;
    config.active_job.queue_adapter = :sidekiq&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Play with Queues ===&lt;br /&gt;
Active Job allows to schedule the job to be processed on a specific queue, this became helpful as common adapters support multiple queues.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Queue name can be prefixed for all jobs using config.active_job.queue_name_prefixin application.rb:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# config/application.rb&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    config.active_job.queue_name_prefix = Rails.env&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# app/jobs/update_wiki_job.rb&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# Now your job will run on queue production_low_priority on your&lt;br /&gt;
# production environment and on staging_low_priority on your staging&lt;br /&gt;
# environment&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can control a job to run on a queue you like, by passing a :queue option to #set :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
MyJob.set(queue: :another_queue).perform_later(record)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the code snippets are referred from the source&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
In this section, some examples are provided to demonstrate how to use ActiveJob in practices.&lt;br /&gt;
=== Background Mail Sender ===&lt;br /&gt;
You can send emails asynchronously with Action Mailer which Active Job is already integrated in.&amp;lt;ref name= &amp;quot;Action-Mailer-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/action_mailer_basics.html “Action Mailer Basics” 2014]&amp;lt;/ref&amp;gt; Here in this example, we try to use Active Job with Action Mailer.&amp;lt;br&amp;gt;&lt;br /&gt;
1. Getting started with Rails:&amp;lt;ref name= &amp;quot;Welcome_to_Rails&amp;quot;&amp;gt;GitHub [https://github.com/rails/rails “Welcome to Rails” 2015]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Install Rails if you haven’t done it yet:&lt;br /&gt;
$ gem install rails&lt;br /&gt;
&lt;br /&gt;
# Create a new Rails application, “myapp_activejob”&lt;br /&gt;
# is the name of the application:&lt;br /&gt;
$ rails new myapp_activejob&lt;br /&gt;
&lt;br /&gt;
# Change your directory to myapp_activejob&lt;br /&gt;
$ cd myapp_activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=='''References'''==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Schen35</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100658</id>
		<title>Active Job</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100658"/>
		<updated>2016-02-02T20:52:51Z</updated>

		<summary type="html">&lt;p&gt;Schen35: /* How to use http://guides.rubyonrails.org/active_job_basics.html */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
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[https://github.com/nesquena/backburner], Delayed Job[https://github.com/collectiveidea/delayed_job], Qu[https://github.com/bkeepers/qu] and so on. &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/active_job_basics.html&amp;lt;/ref&amp;gt; Overall, Active Job is a interface which you can work with common queues.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
====Active Job adapters&amp;lt;ref&amp;gt;http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters.html&amp;lt;/ref&amp;gt;====&lt;br /&gt;
*Backburner[https://github.com/nesquena/backburner]&lt;br /&gt;
*Delayed Job[https://github.com/collectiveidea/delayed_job]&lt;br /&gt;
*Qu[https://github.com/bkeepers/qu]&lt;br /&gt;
*Que[https://github.com/chanks/que]&lt;br /&gt;
*queue_classic[https://github.com/QueueClassic/queue_classic]&lt;br /&gt;
*Resque 1.x[https://github.com/resque/resque]&lt;br /&gt;
*Sidekiq[http://sidekiq.org/]&lt;br /&gt;
*Sneakers[https://github.com/jondot/sneakers]&lt;br /&gt;
*Sucker Punch[https://github.com/brandonhilkert/sucker_punch]&lt;br /&gt;
*Active Job Async Job&lt;br /&gt;
*Active Job Inline[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters/InlineAdapter.html]&lt;br /&gt;
&lt;br /&gt;
=='''How to use &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/active_job_basics.html&amp;lt;/ref&amp;gt;'''==&lt;br /&gt;
We will introduce how to creating a job and and how to add the job into a queue. &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
=== Download ===&lt;br /&gt;
With RubyGems you can install the Active Job into your computer:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gem install activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://github.com/rails/rails/tree/master/activejob Souce code of Active Job] available on GitHub, as part of Rails.&amp;lt;ref&amp;gt; GitHub [https://github.com/rails/rails/tree/master/activejob “Active Job -- Make work happen later” 2015] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Create a Job ===&lt;br /&gt;
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.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki&lt;br /&gt;
  invoke  test_unit&lt;br /&gt;
  create  test/jobs/update_wiki_job_test.rb&lt;br /&gt;
  create  app/jobs/update_wiki_job.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Active Job provides the ability to run your Job on a specific queue by create a job as following:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki --queue urgent&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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:&amp;lt;ref&amp;gt; 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] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ApplicationJob &amp;lt; ActiveJob::Base&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 4.2 a Job class looks like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :default&lt;br /&gt;
 &lt;br /&gt;
  def perform(*wiki)&lt;br /&gt;
   # Do something later&lt;br /&gt;
  wiki.update_contents&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There is a perform method to be called when the job was first enqueued. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Adding a Job to the queue ===&lt;br /&gt;
If you wish your Job be processed as soon as the queuing system is free, you can enqueue a Job like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.perform_later wiki&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can add a Job be performed tomorrow at noon:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait_until: Date.tomorrow.noon).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you want your Job be performed a week from now, most of the queueing backends ( Sidekiq&lt;br /&gt;
, Delayed Job, etc. ) allow you to set a delay time.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait: 1.week).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Execution of Job ===&lt;br /&gt;
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]).&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt; Without setting any adapter, the job would be performed immediately.&amp;lt;br&amp;gt;&lt;br /&gt;
Queueing backend can be set at: /config/application.rb, in this example we use the Sidekiq.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    # Be sure to have the adapter's gem in your Gemfile and follow&lt;br /&gt;
    # the adapter's specific installation and deployment instructions.&lt;br /&gt;
    config.active_job.queue_adapter = :sidekiq&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Play with Queues ===&lt;br /&gt;
Active Job allows to schedule the job to be processed on a specific queue, this became helpful as common adapters support multiple queues.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Queue name can be prefixed for all jobs using config.active_job.queue_name_prefixin application.rb:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# config/application.rb&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    config.active_job.queue_name_prefix = Rails.env&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# app/jobs/update_wiki_job.rb&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# Now your job will run on queue production_low_priority on your&lt;br /&gt;
# production environment and on staging_low_priority on your staging&lt;br /&gt;
# environment&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can control a job to run on a queue you like, by passing a :queue option to #set :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
MyJob.set(queue: :another_queue).perform_later(record)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the code snippets are referred from the source&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Examples ==&lt;br /&gt;
In this section, some examples are provided to demonstrate how to use ActiveJob in practices.&lt;br /&gt;
=== Background Mail Sender ===&lt;br /&gt;
You can send emails asynchronously with Action Mailer which Active Job is already integrated in.&amp;lt;ref name= &amp;quot;Action-Mailer-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/action_mailer_basics.html “Action Mailer Basics” 2014]&amp;lt;/ref&amp;gt; Here in this example, we try to use Active Job with Action Mailer.&amp;lt;br&amp;gt;&lt;br /&gt;
Getting started with Rails:&amp;lt;ref name= &amp;quot;Welcome_to_Rails&amp;quot;&amp;gt;GitHub [https://github.com/rails/rails “Welcome to Rails” 2015]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# Install Rails if you haven’t done it yet:&lt;br /&gt;
$ gem install rails&lt;br /&gt;
&lt;br /&gt;
# Create a new Rails application, “myapp_activejob” is the name of the application:&lt;br /&gt;
$ rails new myapp_activejob&lt;br /&gt;
&lt;br /&gt;
# Change your directory to myapp_activejob&lt;br /&gt;
$ cd myapp_activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=='''References'''==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Schen35</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100657</id>
		<title>Active Job</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100657"/>
		<updated>2016-02-02T19:46:16Z</updated>

		<summary type="html">&lt;p&gt;Schen35: /* How to use http://guides.rubyonrails.org/active_job_basics.html */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
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[https://github.com/nesquena/backburner], Delayed Job[https://github.com/collectiveidea/delayed_job], Qu[https://github.com/bkeepers/qu] and so on. &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/active_job_basics.html&amp;lt;/ref&amp;gt; Overall, Active Job is a interface which you can work with common queues.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
====Active Job adapters&amp;lt;ref&amp;gt;http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters.html&amp;lt;/ref&amp;gt;====&lt;br /&gt;
*Backburner[https://github.com/nesquena/backburner]&lt;br /&gt;
*Delayed Job[https://github.com/collectiveidea/delayed_job]&lt;br /&gt;
*Qu[https://github.com/bkeepers/qu]&lt;br /&gt;
*Que[https://github.com/chanks/que]&lt;br /&gt;
*queue_classic[https://github.com/QueueClassic/queue_classic]&lt;br /&gt;
*Resque 1.x[https://github.com/resque/resque]&lt;br /&gt;
*Sidekiq[http://sidekiq.org/]&lt;br /&gt;
*Sneakers[https://github.com/jondot/sneakers]&lt;br /&gt;
*Sucker Punch[https://github.com/brandonhilkert/sucker_punch]&lt;br /&gt;
*Active Job Async Job&lt;br /&gt;
*Active Job Inline[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters/InlineAdapter.html]&lt;br /&gt;
&lt;br /&gt;
=='''How to use &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/active_job_basics.html&amp;lt;/ref&amp;gt;'''==&lt;br /&gt;
We will introduce how to creating a job and and how to add the job into a queue. &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
=== Download ===&lt;br /&gt;
With RubyGems you can install the Active Job into your computer:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gem install activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://github.com/rails/rails/tree/master/activejob Souce code of Active Job] available on GitHub, as part of Rails.&amp;lt;ref&amp;gt; GitHub [https://github.com/rails/rails/tree/master/activejob “Active Job -- Make work happen later” 2015] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Create a Job ===&lt;br /&gt;
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.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki&lt;br /&gt;
  invoke  test_unit&lt;br /&gt;
  create  test/jobs/update_wiki_job_test.rb&lt;br /&gt;
  create  app/jobs/update_wiki_job.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Active Job provides the ability to run your Job on a specific queue by create a job as following:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki --queue urgent&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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:&amp;lt;ref&amp;gt; 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] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ApplicationJob &amp;lt; ActiveJob::Base&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 4.2 a Job class looks like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :default&lt;br /&gt;
 &lt;br /&gt;
  def perform(*wiki)&lt;br /&gt;
   # Do something later&lt;br /&gt;
  wiki.update_contents&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
There is a perform method to be called when the job was first enqueued. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Adding a Job to the queue ===&lt;br /&gt;
If you wish your Job be processed as soon as the queuing system is free, you can enqueue a Job like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.perform_later wiki&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can add a Job be performed tomorrow at noon:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait_until: Date.tomorrow.noon).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you want your Job be performed a week from now, most of the queueing backends ( Sidekiq&lt;br /&gt;
, Delayed Job, etc. ) allow you to set a delay time.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait: 1.week).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Execution of Job ===&lt;br /&gt;
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]).&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt; Without setting any adapter, the job would be performed immediately.&amp;lt;br&amp;gt;&lt;br /&gt;
Queueing backend can be set at: /config/application.rb, in this example we use the Sidekiq.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    # Be sure to have the adapter's gem in your Gemfile and follow&lt;br /&gt;
    # the adapter's specific installation and deployment instructions.&lt;br /&gt;
    config.active_job.queue_adapter = :sidekiq&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Play with Queues ===&lt;br /&gt;
Active Job allows to schedule the job to be processed on a specific queue, this became helpful as common adapters support multiple queues.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Queue name can be prefixed for all jobs using config.active_job.queue_name_prefixin application.rb:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# config/application.rb&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    config.active_job.queue_name_prefix = Rails.env&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# app/jobs/update_wiki_job.rb&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# Now your job will run on queue production_low_priority on your&lt;br /&gt;
# production environment and on staging_low_priority on your staging&lt;br /&gt;
# environment&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
You can control a job to run on a queue you like, by passing a :queue option to #set :&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
MyJob.set(queue: :another_queue).perform_later(record)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the code snippets are referred from the source&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=='''References'''==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Schen35</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100656</id>
		<title>Active Job</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100656"/>
		<updated>2016-01-31T14:49:45Z</updated>

		<summary type="html">&lt;p&gt;Schen35: /* How to use Rails Guides “Active Job Basics” 2014 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
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[https://github.com/nesquena/backburner], Delayed Job[https://github.com/collectiveidea/delayed_job], Qu[https://github.com/bkeepers/qu] and so on. &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/active_job_basics.html&amp;lt;/ref&amp;gt; Overall, Active Job is a interface which you can work with common queues.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
====Active Job adapters&amp;lt;ref&amp;gt;http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters.html&amp;lt;/ref&amp;gt;====&lt;br /&gt;
*Backburner[https://github.com/nesquena/backburner]&lt;br /&gt;
*Delayed Job[https://github.com/collectiveidea/delayed_job]&lt;br /&gt;
*Qu[https://github.com/bkeepers/qu]&lt;br /&gt;
*Que[https://github.com/chanks/que]&lt;br /&gt;
*queue_classic[https://github.com/QueueClassic/queue_classic]&lt;br /&gt;
*Resque 1.x[https://github.com/resque/resque]&lt;br /&gt;
*Sidekiq[http://sidekiq.org/]&lt;br /&gt;
*Sneakers[https://github.com/jondot/sneakers]&lt;br /&gt;
*Sucker Punch[https://github.com/brandonhilkert/sucker_punch]&lt;br /&gt;
*Active Job Async Job&lt;br /&gt;
*Active Job Inline[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters/InlineAdapter.html]&lt;br /&gt;
&lt;br /&gt;
=='''How to use &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/active_job_basics.html&amp;lt;/ref&amp;gt;'''==&lt;br /&gt;
We will introduce how to creating a job and and how to add the job into a queue. &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
=== Download ===&lt;br /&gt;
Active Job can be installed with latest version using RubyGems:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gem install activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://github.com/rails/rails/tree/master/activejob Souce code of Active Job] available on GitHub, as part of Rails.&amp;lt;ref&amp;gt; GitHub [https://github.com/rails/rails/tree/master/activejob “Active Job -- Make work happen later” 2015] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Create a Job ===&lt;br /&gt;
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”.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki&lt;br /&gt;
  invoke  test_unit&lt;br /&gt;
  create  test/jobs/update_wiki_job_test.rb&lt;br /&gt;
  create  app/jobs/update_wiki_job.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You can also create a job that will run on a specific queue:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki --queue urgent&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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:&amp;lt;ref&amp;gt; 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] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ApplicationJob &amp;lt; ActiveJob::Base&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 4.2 a Job class looks like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :default&lt;br /&gt;
 &lt;br /&gt;
  def perform(*wiki)&lt;br /&gt;
   # Do something later&lt;br /&gt;
  wiki.update_contents&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Adding a Job to the queue ===&lt;br /&gt;
If you wish your Job be processed as soon as the queuing system is free, you can enqueue a Job like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.perform_later wiki&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can add a Job be performed tomorrow at noon:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait_until: Date.tomorrow.noon).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you want your Job be performed a week from now, some queueing backends allow you to set a delay time.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait: 1.week).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Execution of Job ===&lt;br /&gt;
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]).&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt;  If no adapter is set, the job is immediately executed.&amp;lt;br&amp;gt;&lt;br /&gt;
You can set your queueing backend at: /config/application.rb, in this example we use the Sidekiq.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    # Be sure to have the adapter's gem in your Gemfile and follow&lt;br /&gt;
    # the adapter's specific installation and deployment instructions.&lt;br /&gt;
    config.active_job.queue_adapter = :sidekiq&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Play with Queues ===&lt;br /&gt;
Active Job allows to schedule the job to be processed on a specific queue, this became helpful as common adapters support multiple queues.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You can prefix the queue name for all your jobs using config.active_job.queue_name_prefixin application.rb:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# config/application.rb&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    config.active_job.queue_name_prefix = Rails.env&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# app/jobs/guests_cleanup.rb&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# Now your job will run on queue production_low_priority on your&lt;br /&gt;
# production environment and on staging_low_priority on your staging&lt;br /&gt;
# environment&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The default queue name prefix delimiter is '_'. This can be changed by setting config.active_job.queue_name_delimiter in application.rb:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# config/application.rb&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    config.active_job.queue_name_prefix = Rails.env&lt;br /&gt;
    config.active_job.queue_name_delimiter = '.'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# app/jobs/guests_cleanup.rb&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# Now your job will run on queue production.low_priority on your&lt;br /&gt;
# production environment and on staging.low_priority on your staging&lt;br /&gt;
# environment&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you want more control on what queue a job will be run you can pass a :queue option to #set:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
MyJob.set(queue: :another_queue).perform_later(record)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ProcessVideoJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as do&lt;br /&gt;
    video = self.arguments.first&lt;br /&gt;
    if video.owner.premium?&lt;br /&gt;
      :premium_videojobs&lt;br /&gt;
    else&lt;br /&gt;
      :videojobs&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def perform(video)&lt;br /&gt;
    # do process video&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
ProcessVideoJob.perform_later(Video.last)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the code snippets are referred from the source&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=='''References'''==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Schen35</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100655</id>
		<title>Active Job</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100655"/>
		<updated>2016-01-31T14:44:03Z</updated>

		<summary type="html">&lt;p&gt;Schen35: /* How to use http://guides.rubyonrails.org/active_job_basics.html */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
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[https://github.com/nesquena/backburner], Delayed Job[https://github.com/collectiveidea/delayed_job], Qu[https://github.com/bkeepers/qu] and so on. &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/active_job_basics.html&amp;lt;/ref&amp;gt; Overall, Active Job is a interface which you can work with common queues.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
====Active Job adapters&amp;lt;ref&amp;gt;http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters.html&amp;lt;/ref&amp;gt;====&lt;br /&gt;
*Backburner[https://github.com/nesquena/backburner]&lt;br /&gt;
*Delayed Job[https://github.com/collectiveidea/delayed_job]&lt;br /&gt;
*Qu[https://github.com/bkeepers/qu]&lt;br /&gt;
*Que[https://github.com/chanks/que]&lt;br /&gt;
*queue_classic[https://github.com/QueueClassic/queue_classic]&lt;br /&gt;
*Resque 1.x[https://github.com/resque/resque]&lt;br /&gt;
*Sidekiq[http://sidekiq.org/]&lt;br /&gt;
*Sneakers[https://github.com/jondot/sneakers]&lt;br /&gt;
*Sucker Punch[https://github.com/brandonhilkert/sucker_punch]&lt;br /&gt;
*Active Job Async Job&lt;br /&gt;
*Active Job Inline[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters/InlineAdapter.html]&lt;br /&gt;
&lt;br /&gt;
=='''How to use &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt;'''==&lt;br /&gt;
This section will guide to creating a job and and how to enqueuing the job. &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
=== Download ===&lt;br /&gt;
Active Job can be installed with latest version using RubyGems:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gem install activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://github.com/rails/rails/tree/master/activejob Souce code of Active Job] available on GitHub, as part of Rails.&amp;lt;ref&amp;gt; GitHub [https://github.com/rails/rails/tree/master/activejob “Active Job -- Make work happen later” 2015] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Create a Job ===&lt;br /&gt;
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”.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki&lt;br /&gt;
  invoke  test_unit&lt;br /&gt;
  create  test/jobs/update_wiki_job_test.rb&lt;br /&gt;
  create  app/jobs/update_wiki_job.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You can also create a job that will run on a specific queue:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki --queue urgent&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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:&amp;lt;ref&amp;gt; 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] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ApplicationJob &amp;lt; ActiveJob::Base&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 4.2 a Job class looks like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :default&lt;br /&gt;
 &lt;br /&gt;
  def perform(*wiki)&lt;br /&gt;
   # Do something later&lt;br /&gt;
  wiki.update_contents&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Adding a Job to the queue ===&lt;br /&gt;
If you wish your Job be processed as soon as the queuing system is free, you can enqueue a Job like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.perform_later wiki&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can add a Job be performed tomorrow at noon:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait_until: Date.tomorrow.noon).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you want your Job be performed a week from now, some queueing backends allow you to set a delay time.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait: 1.week).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Execution of Job ===&lt;br /&gt;
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]).&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt;  If no adapter is set, the job is immediately executed.&amp;lt;br&amp;gt;&lt;br /&gt;
You can set your queueing backend at: /config/application.rb, in this example we use the Sidekiq.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    # Be sure to have the adapter's gem in your Gemfile and follow&lt;br /&gt;
    # the adapter's specific installation and deployment instructions.&lt;br /&gt;
    config.active_job.queue_adapter = :sidekiq&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Play with Queues ===&lt;br /&gt;
Active Job allows to schedule the job to be processed on a specific queue, this became helpful as common adapters support multiple queues.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You can prefix the queue name for all your jobs using config.active_job.queue_name_prefixin application.rb:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# config/application.rb&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    config.active_job.queue_name_prefix = Rails.env&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# app/jobs/guests_cleanup.rb&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# Now your job will run on queue production_low_priority on your&lt;br /&gt;
# production environment and on staging_low_priority on your staging&lt;br /&gt;
# environment&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The default queue name prefix delimiter is '_'. This can be changed by setting config.active_job.queue_name_delimiter in application.rb:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# config/application.rb&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    config.active_job.queue_name_prefix = Rails.env&lt;br /&gt;
    config.active_job.queue_name_delimiter = '.'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# app/jobs/guests_cleanup.rb&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# Now your job will run on queue production.low_priority on your&lt;br /&gt;
# production environment and on staging.low_priority on your staging&lt;br /&gt;
# environment&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you want more control on what queue a job will be run you can pass a :queue option to #set:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
MyJob.set(queue: :another_queue).perform_later(record)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ProcessVideoJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as do&lt;br /&gt;
    video = self.arguments.first&lt;br /&gt;
    if video.owner.premium?&lt;br /&gt;
      :premium_videojobs&lt;br /&gt;
    else&lt;br /&gt;
      :videojobs&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def perform(video)&lt;br /&gt;
    # do process video&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
ProcessVideoJob.perform_later(Video.last)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the code snippets are referred from the source&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=='''References'''==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Schen35</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100654</id>
		<title>Active Job</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100654"/>
		<updated>2016-01-31T14:42:33Z</updated>

		<summary type="html">&lt;p&gt;Schen35: /* How to use http://guides.rubyonrails.org/active_job_basics.html */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
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[https://github.com/nesquena/backburner], Delayed Job[https://github.com/collectiveidea/delayed_job], Qu[https://github.com/bkeepers/qu] and so on. &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/active_job_basics.html&amp;lt;/ref&amp;gt; Overall, Active Job is a interface which you can work with common queues.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
====Active Job adapters&amp;lt;ref&amp;gt;http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters.html&amp;lt;/ref&amp;gt;====&lt;br /&gt;
*Backburner[https://github.com/nesquena/backburner]&lt;br /&gt;
*Delayed Job[https://github.com/collectiveidea/delayed_job]&lt;br /&gt;
*Qu[https://github.com/bkeepers/qu]&lt;br /&gt;
*Que[https://github.com/chanks/que]&lt;br /&gt;
*queue_classic[https://github.com/QueueClassic/queue_classic]&lt;br /&gt;
*Resque 1.x[https://github.com/resque/resque]&lt;br /&gt;
*Sidekiq[http://sidekiq.org/]&lt;br /&gt;
*Sneakers[https://github.com/jondot/sneakers]&lt;br /&gt;
*Sucker Punch[https://github.com/brandonhilkert/sucker_punch]&lt;br /&gt;
*Active Job Async Job&lt;br /&gt;
*Active Job Inline[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters/InlineAdapter.html]&lt;br /&gt;
&lt;br /&gt;
=='''How to use &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/active_job_basics.html&amp;lt;/ref&amp;gt;'''==&lt;br /&gt;
This section will guide to creating a job and and how to enqueuing the job. &amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
=== Download ===&lt;br /&gt;
Active Job can be installed with latest version using RubyGems:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gem install activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://github.com/rails/rails/tree/master/activejob Souce code of Active Job] available on GitHub, as part of Rails.&amp;lt;ref&amp;gt; GitHub [https://github.com/rails/rails/tree/master/activejob “Active Job -- Make work happen later” 2015] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Create a Job ===&lt;br /&gt;
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”.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki&lt;br /&gt;
  invoke  test_unit&lt;br /&gt;
  create  test/jobs/update_wiki_job_test.rb&lt;br /&gt;
  create  app/jobs/update_wiki_job.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You can also create a job that will run on a specific queue:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki --queue urgent&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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:&amp;lt;ref&amp;gt; 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] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ApplicationJob &amp;lt; ActiveJob::Base&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 4.2 a Job class looks like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :default&lt;br /&gt;
 &lt;br /&gt;
  def perform(*wiki)&lt;br /&gt;
   # Do something later&lt;br /&gt;
  wiki.update_contents&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Adding a Job to the queue ===&lt;br /&gt;
If you wish your Job be processed as soon as the queuing system is free, you can enqueue a Job like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.perform_later wiki&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can add a Job be performed tomorrow at noon:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait_until: Date.tomorrow.noon).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you want your Job be performed a week from now, some queueing backends allow you to set a delay time.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait: 1.week).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Execution of Job ===&lt;br /&gt;
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]).&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt;  If no adapter is set, the job is immediately executed.&amp;lt;br&amp;gt;&lt;br /&gt;
You can set your queueing backend at: /config/application.rb, in this example we use the Sidekiq.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    # Be sure to have the adapter's gem in your Gemfile and follow&lt;br /&gt;
    # the adapter's specific installation and deployment instructions.&lt;br /&gt;
    config.active_job.queue_adapter = :sidekiq&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Play with Queues ===&lt;br /&gt;
Active Job allows to schedule the job to be processed on a specific queue, this became helpful as common adapters support multiple queues.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You can prefix the queue name for all your jobs using config.active_job.queue_name_prefixin application.rb:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# config/application.rb&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    config.active_job.queue_name_prefix = Rails.env&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# app/jobs/guests_cleanup.rb&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# Now your job will run on queue production_low_priority on your&lt;br /&gt;
# production environment and on staging_low_priority on your staging&lt;br /&gt;
# environment&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The default queue name prefix delimiter is '_'. This can be changed by setting config.active_job.queue_name_delimiter in application.rb:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# config/application.rb&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    config.active_job.queue_name_prefix = Rails.env&lt;br /&gt;
    config.active_job.queue_name_delimiter = '.'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# app/jobs/guests_cleanup.rb&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# Now your job will run on queue production.low_priority on your&lt;br /&gt;
# production environment and on staging.low_priority on your staging&lt;br /&gt;
# environment&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you want more control on what queue a job will be run you can pass a :queue option to #set:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
MyJob.set(queue: :another_queue).perform_later(record)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ProcessVideoJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as do&lt;br /&gt;
    video = self.arguments.first&lt;br /&gt;
    if video.owner.premium?&lt;br /&gt;
      :premium_videojobs&lt;br /&gt;
    else&lt;br /&gt;
      :videojobs&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def perform(video)&lt;br /&gt;
    # do process video&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
ProcessVideoJob.perform_later(Video.last)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the code snippets are referred from the source&amp;lt;ref name= &amp;quot;Active-Job-Basics&amp;quot;&amp;gt;Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014]&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=='''References'''==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Schen35</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100652</id>
		<title>Active Job</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100652"/>
		<updated>2016-01-31T14:30:52Z</updated>

		<summary type="html">&lt;p&gt;Schen35: /* How to use http://guides.rubyonrails.org/active_job_basics.html */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
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[https://github.com/nesquena/backburner], Delayed Job[https://github.com/collectiveidea/delayed_job], Qu[https://github.com/bkeepers/qu] and so on. [http://guides.rubyonrails.org/4_2_release_notes.html] Overall, Active Job is a interface which you can work with common queues.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
====Active Job adapters[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters.html]====&lt;br /&gt;
*Backburner[https://github.com/nesquena/backburner]&lt;br /&gt;
*Delayed Job[https://github.com/collectiveidea/delayed_job]&lt;br /&gt;
*Qu[https://github.com/bkeepers/qu]&lt;br /&gt;
*Que[https://github.com/chanks/que]&lt;br /&gt;
*queue_classic[https://github.com/QueueClassic/queue_classic]&lt;br /&gt;
*Resque 1.x[https://github.com/resque/resque]&lt;br /&gt;
*Sidekiq[http://sidekiq.org/]&lt;br /&gt;
*Sneakers[https://github.com/jondot/sneakers]&lt;br /&gt;
*Sucker Punch[https://github.com/brandonhilkert/sucker_punch]&lt;br /&gt;
*Active Job Async Job&lt;br /&gt;
*Active Job Inline[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters/InlineAdapter.html]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=='''How to use &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/active_job_basics.html&amp;lt;/ref&amp;gt;'''==&lt;br /&gt;
This section will guide to creating a job and and how to enqueuing the job. &amp;lt;ref&amp;gt; Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014] &amp;lt;/ref&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
=== Download ===&lt;br /&gt;
Active Job can be installed with latest version using RubyGems:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gem install activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://github.com/rails/rails/tree/master/activejob Souce code of Active Job] available on GitHub, as part of Rails.&amp;lt;ref&amp;gt; GitHub [https://github.com/rails/rails/tree/master/activejob “Active Job -- Make work happen later” 2015] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Create a Job ===&lt;br /&gt;
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”.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki&lt;br /&gt;
  invoke  test_unit&lt;br /&gt;
  create  test/jobs/update_wiki_job_test.rb&lt;br /&gt;
  create  app/jobs/update_wiki_job.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You can also create a job that will run on a specific queue:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki --queue urgent&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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:&amp;lt;ref&amp;gt; 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] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ApplicationJob &amp;lt; ActiveJob::Base&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 4.2 a Job class looks like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :default&lt;br /&gt;
 &lt;br /&gt;
  def perform(*wiki)&lt;br /&gt;
   # Do something later&lt;br /&gt;
  wiki.update_contents&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Adding a Job to the queue ===&lt;br /&gt;
If you wish your Job be processed as soon as the queuing system is free, you can enqueue a Job like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.perform_later wiki&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can add a Job be performed tomorrow at noon:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait_until: Date.tomorrow.noon).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you want your Job be performed a week from now, some queueing backends allow you to set a delay time.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait: 1.week).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Execution of Job ===&lt;br /&gt;
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]).&amp;lt;ref&amp;gt; Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014] &amp;lt;/ref&amp;gt; If no adapter is set, the job is immediately executed.&amp;lt;br&amp;gt;&lt;br /&gt;
You can set your queueing backend at: /config/application.rb, in this example we use the Sidekiq.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    # Be sure to have the adapter's gem in your Gemfile and follow&lt;br /&gt;
    # the adapter's specific installation and deployment instructions.&lt;br /&gt;
    config.active_job.queue_adapter = :sidekiq&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Play with Queues ===&lt;br /&gt;
Active Job allows to schedule the job to be processed on a specific queue, this became helpful as common adapters support multiple queues.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You can prefix the queue name for all your jobs using config.active_job.queue_name_prefixin application.rb:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# config/application.rb&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    config.active_job.queue_name_prefix = Rails.env&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# app/jobs/guests_cleanup.rb&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# Now your job will run on queue production_low_priority on your&lt;br /&gt;
# production environment and on staging_low_priority on your staging&lt;br /&gt;
# environment&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The default queue name prefix delimiter is '_'. This can be changed by setting config.active_job.queue_name_delimiter in application.rb:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# config/application.rb&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    config.active_job.queue_name_prefix = Rails.env&lt;br /&gt;
    config.active_job.queue_name_delimiter = '.'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# app/jobs/guests_cleanup.rb&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# Now your job will run on queue production.low_priority on your&lt;br /&gt;
# production environment and on staging.low_priority on your staging&lt;br /&gt;
# environment&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you want more control on what queue a job will be run you can pass a :queue option to #set:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
MyJob.set(queue: :another_queue).perform_later(record)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ProcessVideoJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as do&lt;br /&gt;
    video = self.arguments.first&lt;br /&gt;
    if video.owner.premium?&lt;br /&gt;
      :premium_videojobs&lt;br /&gt;
    else&lt;br /&gt;
      :videojobs&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def perform(video)&lt;br /&gt;
    # do process video&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
ProcessVideoJob.perform_later(Video.last)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the code snippets are referred from the source:&amp;lt;ref&amp;gt; Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=='''References'''==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Schen35</name></author>
	</entry>
	<entry>
		<id>https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100651</id>
		<title>Active Job</title>
		<link rel="alternate" type="text/html" href="https://wiki.expertiza.ncsu.edu/index.php?title=Active_Job&amp;diff=100651"/>
		<updated>2016-01-31T14:29:03Z</updated>

		<summary type="html">&lt;p&gt;Schen35: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== Introduction ==&lt;br /&gt;
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[https://github.com/nesquena/backburner], Delayed Job[https://github.com/collectiveidea/delayed_job], Qu[https://github.com/bkeepers/qu] and so on. [http://guides.rubyonrails.org/4_2_release_notes.html] Overall, Active Job is a interface which you can work with common queues.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
====Active Job adapters[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters.html]====&lt;br /&gt;
*Backburner[https://github.com/nesquena/backburner]&lt;br /&gt;
*Delayed Job[https://github.com/collectiveidea/delayed_job]&lt;br /&gt;
*Qu[https://github.com/bkeepers/qu]&lt;br /&gt;
*Que[https://github.com/chanks/que]&lt;br /&gt;
*queue_classic[https://github.com/QueueClassic/queue_classic]&lt;br /&gt;
*Resque 1.x[https://github.com/resque/resque]&lt;br /&gt;
*Sidekiq[http://sidekiq.org/]&lt;br /&gt;
*Sneakers[https://github.com/jondot/sneakers]&lt;br /&gt;
*Sucker Punch[https://github.com/brandonhilkert/sucker_punch]&lt;br /&gt;
*Active Job Async Job&lt;br /&gt;
*Active Job Inline[http://edgeapi.rubyonrails.org/classes/ActiveJob/QueueAdapters/InlineAdapter.html]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=='''How to use &amp;lt;ref&amp;gt;http://guides.rubyonrails.org/active_job_basics.html&amp;lt;/ref&amp;gt;'''==&lt;br /&gt;
This section will guide to creating a job and and how to enqueuing the job. &amp;lt;ref&amp;gt; Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014] &amp;lt;/ref&amp;gt; &amp;lt;br&amp;gt;&lt;br /&gt;
=== Download ===&lt;br /&gt;
Active Job can be installed with latest version using RubyGems:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ gem install activejob&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[https://github.com/rails/rails/tree/master/activejob Souce code of Active Job] available on GitHub, as part of Rails.&amp;lt;ref&amp;gt; GitHub [https://github.com/rails/rails/tree/master/activejob “Active Job -- Make work happen later” 2015] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Create a Job ===&lt;br /&gt;
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”.&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki&lt;br /&gt;
  invoke  test_unit&lt;br /&gt;
  create  test/jobs/update_wiki_job_test.rb&lt;br /&gt;
  create  app/jobs/update_wiki_job.rb&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You can also create a job that will run on a specific queue:&amp;lt;br&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
$ bin/rails generate job update_wiki --queue urgent&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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:&amp;lt;ref&amp;gt; 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] &amp;lt;/ref&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ApplicationJob &amp;lt; ActiveJob::Base&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In Rails 4.2 a Job class looks like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :default&lt;br /&gt;
 &lt;br /&gt;
  def perform(*wiki)&lt;br /&gt;
   # Do something later&lt;br /&gt;
  wiki.update_contents&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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. &amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Adding a Job to the queue ===&lt;br /&gt;
If you wish your Job be processed as soon as the queuing system is free, you can enqueue a Job like:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.perform_later wiki&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
Or you can add a Job be performed tomorrow at noon:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait_until: Date.tomorrow.noon).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you want your Job be performed a week from now, some queueing backends allow you to set a delay time.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
UpdateWikiJob.set(wait: 1.week).perform_later(wiki)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Execution of Job ===&lt;br /&gt;
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]).&amp;lt;ref&amp;gt; Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014] &amp;lt;/ref&amp;gt; If no adapter is set, the job is immediately executed.&amp;lt;br&amp;gt;&lt;br /&gt;
You can set your queueing backend at: /config/application.rb, in this example we use the Sidekiq.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    # Be sure to have the adapter's gem in your Gemfile and follow&lt;br /&gt;
    # the adapter's specific installation and deployment instructions.&lt;br /&gt;
    config.active_job.queue_adapter = :sidekiq&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Play with Queues ===&lt;br /&gt;
Active Job allows to schedule the job to be processed on a specific queue, this became helpful as common adapters support multiple queues.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
You can prefix the queue name for all your jobs using config.active_job.queue_name_prefixin application.rb:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# config/application.rb&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    config.active_job.queue_name_prefix = Rails.env&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# app/jobs/guests_cleanup.rb&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# Now your job will run on queue production_low_priority on your&lt;br /&gt;
# production environment and on staging_low_priority on your staging&lt;br /&gt;
# environment&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The default queue name prefix delimiter is '_'. This can be changed by setting config.active_job.queue_name_delimiter in application.rb:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
# config/application.rb&lt;br /&gt;
module YourApp&lt;br /&gt;
  class Application &amp;lt; Rails::Application&lt;br /&gt;
    config.active_job.queue_name_prefix = Rails.env&lt;br /&gt;
    config.active_job.queue_name_delimiter = '.'&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# app/jobs/guests_cleanup.rb&lt;br /&gt;
class UpdateWikiJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as :low_priority&lt;br /&gt;
  #....&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
# Now your job will run on queue production.low_priority on your&lt;br /&gt;
# production environment and on staging.low_priority on your staging&lt;br /&gt;
# environment&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you want more control on what queue a job will be run you can pass a :queue option to #set:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
MyJob.set(queue: :another_queue).perform_later(record)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
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:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class ProcessVideoJob &amp;lt; ActiveJob::Base&lt;br /&gt;
  queue_as do&lt;br /&gt;
    video = self.arguments.first&lt;br /&gt;
    if video.owner.premium?&lt;br /&gt;
      :premium_videojobs&lt;br /&gt;
    else&lt;br /&gt;
      :videojobs&lt;br /&gt;
    end&lt;br /&gt;
  end&lt;br /&gt;
 &lt;br /&gt;
  def perform(video)&lt;br /&gt;
    # do process video&lt;br /&gt;
  end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
ProcessVideoJob.perform_later(Video.last)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
All the code snippets are referred from the source:&amp;lt;ref&amp;gt; Rails Guides [http://guides.rubyonrails.org/active_job_basics.html “Active Job Basics” 2014] &amp;lt;/ref&amp;gt;&lt;/div&gt;</summary>
		<author><name>Schen35</name></author>
	</entry>
</feed>