Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## What is Resque?
- * Handles background jobs by placing them on queues and handling them at a later time
- * Useful for code that is being executed on an external web service that can be moved into a
- background task
- * Why?
- * The service could take up time and cause delay in your app's loading time
- * You want to move them into a background process
- * Also prevents the blocking of other requests from one that is currently being held
- ## Setting up Resque
- * install or have installed redis!!!
- * start the redis process!
- * In `Gemfile`: add `gem 'resque'`, then run `bundle install`
- * Add a file within `lib/tasks`, called `resque.rake`
- ```ruby
- require 'resque/tasks'
- #load the environment when the worker starts up
- task "resque:setup" => :environment
- ```
- * run this in the terminal **after all changes have been made to the project**:
- `bundle exec rake resque:work QUEUE='*'`
- * The `QUEUE=''` takes in the name of the queue to work on or use `*` to load all queues
- * You can now move your API request code in to a **worker** file
- * Use `Resque.enqueue(workerClassName, additional_arguments)` to move the API request code to a
- worker file. Additional arguments could be `@object.id` (can be use to refetch the object without
- submitting a complex object that is going to be turned into JSON).
- * Create a new directory within `apps` called `workers`
- * Create a file (worker) in the `workers` directory and name it using the `worker_class_name.rb`
- used in the `enqueue` line above.
- * That worker file is basically a **ruby class**!
- ```ruby
- class WorkerClassName
- # 1st requirement
- @queue = :name_of_queue #can be named anything
- # 2nd requirement
- def self.perform(object.id) #takes the additional arguments passed in to enqueue
- object = Object.find(abject.id) #refetch the id that was before to complex to send through
- ... #code referencing your API call!
- end
- end
- ```
- * Complete!
- ## Resque Web Interface
- * from terminal, execute `resque-web`
- * You can check the queues and failed jobs!
- * **May need to restart rake task if new code was added after running the task initially**
- ## Imbedding the Web Interface into your App?
- * You can add a route to also visit the resque web interface instead of launching it from terminal!
- * In `config/routes`: `mount Resque::Server, :at => "/resque"`
- * Need to edit the `Gemfile` again: `gem 'resque', :require => "resque/server"`
- ## Add authentication to the Resque Web Interface
- * If using `Devise`:
- ```ruby
- #routes.rb
- authenticate :admin do
- mount Resque::Server, :at => "/resque"
- end
- ```
- * If not using `Devise`, an alternative way is to create a new `initializer` in
- `config/initializers` called `resque_auth.rb`
- ```ruby
- Resque::Server.use(Rack:;Auth::Basic) do |user, password|
- user == "rtrw"
- password == "jhtrw"
- end
- ```
Add Comment
Please, Sign In to add comment