|
| 1 | +# Rake |
| 2 | +## Standalone |
| 3 | + |
| 4 | +Rake is a Make-like program implemented in Ruby. Tasks and dependencies are specified in standard Ruby syntax. |
| 5 | + |
| 6 | +### Default task |
| 7 | +``` |
| 8 | +task default: %w[hello] |
| 9 | +
|
| 10 | +task :hello do |
| 11 | + puts 'Hi!' |
| 12 | +end |
| 13 | +
|
| 14 | +task :multiply do |
| 15 | + puts 123 * 2 |
| 16 | +end |
| 17 | +
|
| 18 | +``` |
| 19 | + |
| 20 | +``` |
| 21 | +rake |
| 22 | +rake hello |
| 23 | +rake multiply |
| 24 | +``` |
| 25 | + |
| 26 | +### Namespace |
| 27 | +``` |
| 28 | + namespace :morning do |
| 29 | + task :turn_of_alarm |
| 30 | + .... |
| 31 | + end |
| 32 | +``` |
| 33 | + |
| 34 | +### Invoke task |
| 35 | +``` |
| 36 | + namespace :afternoon do |
| 37 | + task :make_coffee do |
| 38 | + Rake::Task['morning:make_coffee'].invoke |
| 39 | + puts "Ready for the rest of the day!" |
| 40 | + end |
| 41 | + end |
| 42 | + ``` |
| 43 | + |
| 44 | +## With Rails |
| 45 | + |
| 46 | +### Useful tasks |
| 47 | +``` |
| 48 | +rake -T |
| 49 | +
|
| 50 | +rake db:create |
| 51 | +rake db:drop |
| 52 | +rake db:migrate |
| 53 | +rake db:migrate:status |
| 54 | +rake db:rollback |
| 55 | +rake db:seed |
| 56 | +rake middleware |
| 57 | +rake notes |
| 58 | +rake routes |
| 59 | +rake secret |
| 60 | +rake stats |
| 61 | +rake test |
| 62 | +rake assets:precompile |
| 63 | +``` |
| 64 | + |
| 65 | +### Files |
| 66 | +``` |
| 67 | +Rakefile |
| 68 | +lib/tasks/*.rake |
| 69 | +``` |
| 70 | + |
| 71 | +### Custom rake task |
| 72 | + |
| 73 | +``` |
| 74 | +namespace :my do |
| 75 | + desc "This task does nothing" |
| 76 | + task :nothing do |
| 77 | + # Seriously, nothing |
| 78 | + end |
| 79 | +end |
| 80 | +``` |
| 81 | + |
| 82 | +``` |
| 83 | +rake -T |
| 84 | +``` |
| 85 | + |
| 86 | +# Background jobs Jobs |
| 87 | + |
| 88 | +## Delayed Job |
| 89 | + |
| 90 | +* One thread per process |
| 91 | +* Stores tasks in the relational database |
| 92 | + |
| 93 | +## Resque |
| 94 | + |
| 95 | +* One thread per process |
| 96 | +* Stores tasks in Redis |
| 97 | + |
| 98 | +## Sidekiq |
| 99 | + |
| 100 | +* Multiple threads per process |
| 101 | +* Stores tasks in Redis |
| 102 | +* Web interface |
| 103 | + |
| 104 | +# Sidetiq |
| 105 | + |
| 106 | +* Recurrent jobs |
| 107 | +* Icecube problems |
| 108 | +* Do not use |
| 109 | + |
| 110 | + |
| 111 | +## Create worker |
| 112 | +``` |
| 113 | +class HardWorker |
| 114 | + include Sidekiq::Worker |
| 115 | + def perform(name, count) |
| 116 | + # do something |
| 117 | + end |
| 118 | +end |
| 119 | +``` |
| 120 | + |
| 121 | +## Call worker |
| 122 | +``` |
| 123 | +HardWorker.perform_async('bob', 5) |
| 124 | +HardWorker.perform_in(5.minutes, 'bob', 5) |
| 125 | +User.delay.do_some_stuff(current_user.id, 20) |
| 126 | +``` |
| 127 | + |
| 128 | +## Retries |
| 129 | + |
| 130 | +``` |
| 131 | +class LessRetryableWorker |
| 132 | + include Sidekiq::Worker |
| 133 | + sidekiq_options :retry => 5 # Only five retries and then to the Dead Job Queue |
| 134 | +
|
| 135 | + def perform(...) |
| 136 | + end |
| 137 | +end |
| 138 | +``` |
| 139 | + |
| 140 | +## Main notes |
| 141 | + |
| 142 | +* Make your job idempotent and transactional |
| 143 | +* Make sure to configure retries |
| 144 | + |
| 145 | +## Web UI |
| 146 | + |
| 147 | +``` |
| 148 | +gem 'sinatra', :require => nil |
| 149 | +
|
| 150 | +require 'sidekiq/web' |
| 151 | +mount Sidekiq::Web => '/sidekiq' |
| 152 | +``` |
| 153 | + |
| 154 | +``` |
| 155 | +<%= link_to 'Monitoring', sidekiq_web_path %> |
| 156 | +``` |
| 157 | + |
| 158 | +``` |
| 159 | +# config/routes.rb |
| 160 | +authenticate :user, lambda { |u| u.admin? } do |
| 161 | + mount Sidekiq::Web => '/sidekiq' |
| 162 | +end |
| 163 | +``` |
| 164 | + |
| 165 | +# Whenever |
| 166 | + |
| 167 | +* Cron humanized api |
| 168 | +* Custom jobs |
| 169 | +* After 25 retries your job will go to dead queue |
| 170 | +* After 6 month job will be discarded |
| 171 | + |
| 172 | + |
| 173 | +BAD: |
| 174 | +``` |
| 175 | +every 3.hours do |
| 176 | + runner "MyModel.some_process" |
| 177 | + rake "my:rake:task" |
| 178 | + command "/usr/bin/my_great_command" |
| 179 | +end |
| 180 | +
|
| 181 | +every 1.day, :at => '4:30 am' do |
| 182 | + runner "MyModel.task_to_run_at_four_thirty_in_the_morning" |
| 183 | +end |
| 184 | +
|
| 185 | +``` |
| 186 | + |
| 187 | +GOOD: |
| 188 | +``` |
| 189 | +job_type :sidekiq, "export PATH=your_path:$PATH; cd /var/www/unicorn/current && RAILS_ENV=#{environment} bundle exec sidekiq-client :task :output" |
| 190 | +
|
| 191 | +every 3.minutes, :roles => [:app] do |
| 192 | + sidekiq "-q queue1 push Cache::LiveWorker" |
| 193 | +end |
| 194 | +
|
| 195 | +``` |
0 commit comments