@@ -47,14 +47,17 @@ later, pull those jobs off the queue and process them.
4747Resque jobs are Ruby classes (or modules) which respond to the
4848` perform ` method. Here's an example:
4949
50- class Archive
51- @queue = :file_serve
5250
53- def self.perform(repo_id, branch = 'master')
54- repo = Repository.find(repo_id)
55- repo.create_archive(branch)
56- end
57- end
51+ ``` ruby
52+ class Archive
53+ @queue = :file_serve
54+
55+ def self .perform (repo_id , branch = ' master' )
56+ repo = Repository .find(repo_id)
57+ repo.create_archive(branch)
58+ end
59+ end
60+ ```
5861
5962The ` @queue ` class instance variable determines which queue ` Archive `
6063jobs will be placed in. Queues are arbitrary and created on the fly -
@@ -63,24 +66,30 @@ you can name them whatever you want and have as many as you want.
6366To place an ` Archive ` job on the ` file_serve ` queue, we might add this
6467to our application's pre-existing ` Repository ` class:
6568
66- class Repository
67- def async_create_archive(branch)
68- Resque.enqueue(Archive, self.id, branch)
69- end
70- end
69+ ``` ruby
70+ class Repository
71+ def async_create_archive (branch )
72+ Resque .enqueue(Archive , self .id, branch)
73+ end
74+ end
75+ ```
7176
7277Now when we call ` repo.async_create_archive('masterbrew') ` in our
7378application, a job will be created and placed on the ` file_serve `
7479queue.
7580
7681Later, a worker will run something like this code to process the job:
7782
78- klass, args = Resque.reserve(:file_serve)
79- klass.perform(*args) if klass.respond_to? :perform
83+ ``` ruby
84+ klass, args = Resque .reserve(:file_serve )
85+ klass.perform(* args) if klass.respond_to? :perform
86+ ```
8087
8188Which translates to:
8289
83- Archive.perform(44, 'masterbrew')
90+ ``` ruby
91+ Archive .perform(44 , ' masterbrew' )
92+ ```
8493
8594Let's start a worker to run ` file_serve ` jobs:
8695
@@ -129,25 +138,33 @@ needs to be crunched later into a queue.
129138Jobs are persisted to queues as JSON objects. Let's take our ` Archive `
130139example from above. We'll run the following code to create a job:
131140
132- repo = Repository.find(44)
133- repo.async_create_archive('masterbrew')
141+ ``` ruby
142+ repo = Repository .find(44 )
143+ repo.async_create_archive(' masterbrew' )
144+ ```
134145
135146The following JSON will be stored in the ` file_serve ` queue:
136147
137- {
138- 'class': 'Archive',
139- 'args': [ 44, 'masterbrew' ]
140- }
148+ ``` javascript
149+ {
150+ ' class' : ' Archive' ,
151+ ' args' : [ 44 , ' masterbrew' ]
152+ }
153+ ```
141154
142155Because of this your jobs must only accept arguments that can be JSON encoded.
143156
144157So instead of doing this:
145158
146- Resque.enqueue(Archive, self, branch)
159+ ``` ruby
160+ Resque .enqueue(Archive , self , branch)
161+ ```
147162
148163do this:
149164
150- Resque.enqueue(Archive, self.id, branch)
165+ ``` ruby
166+ Resque .enqueue(Archive , self .id, branch)
167+ ```
151168
152169This is why our above example (and all the examples in ` examples/ ` )
153170uses object IDs instead of passing around the objects.
@@ -187,15 +204,17 @@ Workers
187204
188205Resque workers are rake tasks that run forever. They basically do this:
189206
190- start
191- loop do
192- if job = reserve
193- job.process
194- else
195- sleep 5
196- end
197- end
198- shutdown
207+ ``` ruby
208+ start
209+ loop do
210+ if job = reserve
211+ job.process
212+ else
213+ sleep 5
214+ end
215+ end
216+ shutdown
217+ ```
199218
200219Starting a worker is simple. Here's our example from earlier:
201220
@@ -214,13 +233,17 @@ This will load the environment before starting a worker. Alternately
214233we can define a ` resque:setup ` task with a dependency on the
215234` environment ` rake task:
216235
217- task "resque:setup" => :environment
236+ ``` ruby
237+ task " resque:setup" => :environment
238+ ```
218239
219240GitHub's setup task looks like this:
220241
221- task "resque:setup" => :environment do
222- Grit::Git.git_timeout = 10.minutes
223- end
242+ ``` ruby
243+ task " resque:setup" => :environment do
244+ Grit ::Git .git_timeout = 10 .minutes
245+ end
246+ ```
224247
225248We don't want the ` git_timeout ` as high as 10 minutes in our web app,
226249but in the Resque workers it's fine.
@@ -435,11 +458,13 @@ Nginx: <http://www.modrails.com/documentation/Users%20guide%20Nginx.html#deployi
435458If you want to load Resque on a subpath, possibly alongside other
436459apps, it's easy to do with Rack's ` URLMap ` :
437460
438- require 'resque/server'
461+ ``` ruby
462+ require ' resque/server'
439463
440- run Rack::URLMap.new \
441- "/" => Your::App.new,
442- "/resque" => Resque::Server.new
464+ run Rack ::URLMap .new \
465+ " /" => Your ::App .new ,
466+ " /resque" => Resque ::Server .new
467+ ```
443468
444469Check ` examples/demo/config.ru ` for a functional example (including
445470HTTP basic auth).
@@ -555,7 +580,9 @@ First install the gem.
555580
556581Next include it in your application.
557582
558- require 'resque'
583+ ``` ruby
584+ require ' resque'
585+ ```
559586
560587Now start your application:
561588
@@ -566,8 +593,10 @@ That's it! You can now create Resque jobs from within your app.
566593To start a worker, create a Rakefile in your app's root (or add this
567594to an existing Rakefile):
568595
569- require 'your/app'
570- require 'resque/tasks'
596+ ``` ruby
597+ require ' your/app'
598+ require ' resque/tasks'
599+ ```
571600
572601Now:
573602
@@ -596,7 +625,9 @@ That's it! You can now create Resque jobs from within your app.
596625
597626To start a worker, add this to your Rakefile in ` RAILS_ROOT ` :
598627
599- require 'resque/tasks'
628+ ``` ruby
629+ require ' resque/tasks'
630+ ```
600631
601632Now:
602633
@@ -649,11 +680,13 @@ Here's our `config/resque.yml`:
649680
650681And our initializer:
651682
652- rails_root = ENV['RAILS_ROOT'] || File.dirname(__FILE__) + '/../..'
653- rails_env = ENV['RAILS_ENV'] || 'development'
683+ ``` ruby
684+ rails_root = ENV [' RAILS_ROOT' ] || File .dirname(__FILE__ ) + ' /../..'
685+ rails_env = ENV [' RAILS_ENV' ] || ' development'
654686
655- resque_config = YAML.load_file(rails_root + '/config/resque.yml')
656- Resque.redis = resque_config[rails_env]
687+ resque_config = YAML .load_file(rails_root + ' /config/resque.yml' )
688+ Resque .redis = resque_config[rails_env]
689+ ```
657690
658691Easy peasy! Why not just use ` RAILS_ROOT ` and ` RAILS_ENV ` ? Because
659692this way we can tell our Sinatra app about the config file:
@@ -665,7 +698,9 @@ Now everyone is on the same page.
665698Also, you could disable jobs queueing by setting 'inline' attribute.
666699For example, if you want to run all jobs in the same process for cucumber, try:
667700
668- Resque.inline = ENV['RAILS_ENV'] == "cucumber"
701+ ``` ruby
702+ Resque .inline = ENV [' RAILS_ENV' ] == " cucumber"
703+ ```
669704
670705
671706Plugins and Hooks
@@ -692,7 +727,9 @@ in your Redis server.
692727
693728Simply use the ` Resque.redis.namespace ` accessor:
694729
695- Resque.redis.namespace = "resque:GitHub"
730+ ``` ruby
731+ Resque .redis.namespace = " resque:GitHub"
732+ ```
696733
697734We recommend sticking this in your initializer somewhere after Redis
698735is configured.
0 commit comments