Guest User

Untitled

a guest
Apr 7th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. ## What is Resque?
  2. * Handles background jobs by placing them on queues and handling them at a later time
  3. * Useful for code that is being executed on an external web service that can be moved into a
  4. background task
  5. * Why?
  6. * The service could take up time and cause delay in your app's loading time
  7. * You want to move them into a background process
  8. * Also prevents the blocking of other requests from one that is currently being held
  9.  
  10. ## Setting up Resque
  11. * install or have installed redis!!!
  12. * start the redis process!
  13. * In `Gemfile`: add `gem 'resque'`, then run `bundle install`
  14. * Add a file within `lib/tasks`, called `resque.rake`
  15.  
  16. ```ruby
  17. require 'resque/tasks'
  18.  
  19. #load the environment when the worker starts up
  20. task "resque:setup" => :environment
  21. ```
  22.  
  23. * run this in the terminal **after all changes have been made to the project**:
  24. `bundle exec rake resque:work QUEUE='*'`
  25. * The `QUEUE=''` takes in the name of the queue to work on or use `*` to load all queues
  26. * You can now move your API request code in to a **worker** file
  27. * Use `Resque.enqueue(workerClassName, additional_arguments)` to move the API request code to a
  28. worker file. Additional arguments could be `@object.id` (can be use to refetch the object without
  29. submitting a complex object that is going to be turned into JSON).
  30. * Create a new directory within `apps` called `workers`
  31. * Create a file (worker) in the `workers` directory and name it using the `worker_class_name.rb`
  32. used in the `enqueue` line above.
  33. * That worker file is basically a **ruby class**!
  34.  
  35. ```ruby
  36. class WorkerClassName
  37. # 1st requirement
  38. @queue = :name_of_queue #can be named anything
  39.  
  40. # 2nd requirement
  41. def self.perform(object.id) #takes the additional arguments passed in to enqueue
  42. object = Object.find(abject.id) #refetch the id that was before to complex to send through
  43. ... #code referencing your API call!
  44. end
  45.  
  46. end
  47. ```
  48.  
  49. * Complete!
  50.  
  51. ## Resque Web Interface
  52. * from terminal, execute `resque-web`
  53. * You can check the queues and failed jobs!
  54. * **May need to restart rake task if new code was added after running the task initially**
  55.  
  56. ## Imbedding the Web Interface into your App?
  57. * You can add a route to also visit the resque web interface instead of launching it from terminal!
  58. * In `config/routes`: `mount Resque::Server, :at => "/resque"`
  59. * Need to edit the `Gemfile` again: `gem 'resque', :require => "resque/server"`
  60.  
  61. ## Add authentication to the Resque Web Interface
  62. * If using `Devise`:
  63.  
  64. ```ruby
  65. #routes.rb
  66. authenticate :admin do
  67. mount Resque::Server, :at => "/resque"
  68. end
  69. ```
  70.  
  71. * If not using `Devise`, an alternative way is to create a new `initializer` in
  72. `config/initializers` called `resque_auth.rb`
  73.  
  74. ```ruby
  75. Resque::Server.use(Rack:;Auth::Basic) do |user, password|
  76. user == "rtrw"
  77. password == "jhtrw"
  78. end
  79. ```
Add Comment
Please, Sign In to add comment