Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Sidekiq Worker設計のコツ #ebisurb

Sidekiq Worker設計のコツ #ebisurb

https://ebisurb.connpass.com/event/106935/
Ebisu.rb #20 のLT資料です

Masato Nakamura

November 29, 2018
Tweet

More Decks by Masato Nakamura

Other Decks in Programming

Transcript

  1. 3FUSZΛઃఆ͢Δ 4 J E F L J R  8

    P S L F S ઃ ܭ ͷ ί π
  2. 1 class RetryWorker 2 include Sidekiq::Worker 3 sidekiq_options queue: :default,

    retry: 3 4 5 def perform 6 puts "sample RetryWorker" 7 end 8 end 4 J E F L J R  8 P S L F S ઃ ܭ ͷ ί π
  3. 1 --- 2 :concurrency: 5 3 staging: 4 :concurrency: 10

    5 production: 6 :concurrency: 20 7 :queues: 8 - [reservation, 2] 9 - default 10 - low 4 J E F L J R  8 P S L F S ઃ ܭ ͷ ί π
  4. 1 require 'worker/base_worker' 2 3 class OpenRestaurantCsvWorker < BaseWorker 4

    sidekiq_options queue: :default, retry: 3 5 6 # Bad Design 7 # 8 def perform 9 restaurants = Restaurant.open 10 restaurants.each do |restaurant| 11 restaurant.generate_csv 12 end 13 end 14 end 4 J E F L J R  8 P S L F S ઃ ܭ ͷ ί π
  5. 1 require 'worker/base_worker' 2 3 class OpenRestaurantCsvWorker < BaseWorker 4

    sidekiq_options queue: :default, retry: 3 5 6 def self.dispatch 7 restaurant_ids = Restaurant.open.pluck(:id) 8 Sidekiq::Client.push_bulk( 9 'queue' => :default, 10 'class' => self, 11 'args' => restaurant_ids 12 ) 13 end 14 15 def perform(restaurant_id) 16 restaurant = Restaurant.find(restaurant_id) 17 restaurant.generate_csv 18 end 19 end 4 J E F L J R  8 P S L F S ઃ ܭ ͷ ί π
  6. 1 require 'worker/base_worker' 2 3 class DispatchOpenRestaurantCsvWorker < BaseWorker 4

    sidekiq_options queue: :default, retry: 1 5 6 def perform 7 OpenRestaurantCsvWorker.dispatch 8 end 9 end 4 J E F L J R  8 P S L F S ઃ ܭ ͷ ί π
  7. 1 require 'worker/base_worker' 2 3 class NotifyReservationStatusWorker < BaseWorker 4

    sidekiq_options queue: :reservation, retry: 3 5 6 # Bad Design 7 # 8 def perform(reservation_id, status) 9 reservation = Reservation.find(reservation_id) 10 case status 11 when 'cancelled' 12 reservation.notify_cancelled 13 when 'visited' 14 reservation.notify_visited 15 end 16 end 17 end 4 J E F L J R  8 P S L F S ઃ ܭ ͷ ί π
  8. 1 require 'worker/base_worker' 2 3 class NotifyCancelledReservationWorker < BaseWorker 4

    sidekiq_options queue: :reservation, retry: 3 5 6 def perform(reservation_id) 7 reservation = Reservation.find(reservation_id) 8 reservation.notify_cancelled 9 end 10 end 4 J E F L J R  8 P S L F S ઃ ܭ ͷ ί π
  9. 1 require 'worker/base_worker' 2 3 class NotifyVisitedReservationWorker < BaseWorker 4

    sidekiq_options queue: :reservation, retry: 3 5 6 def perform(reservation_id) 7 reservation = Reservation.find(reservation_id) 8 reservation.notify_visited 9 end 10 end 4 J E F L J R  8 P S L F S ઃ ܭ ͷ ί π
  10. 1 class BaseWorker 2 include Sidekiq::Worker 3 sidekiq_retries_exhausted do |msg,

    exception| 4 Sidekiq.logger.fatal [self.class.name.underscore.to_sym, :retries_exhausted, msg] 5 Bugsnag.auto_notify(exception) 6 end 7 end 4 J E F L J R  8 P S L F S ઃ ܭ ͷ ί π