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資料です

Fff61dd9a0e2876d2e71d6ef36434d45?s=128

Masato Nakamura

November 29, 2018
Tweet

Transcript

  1. 4JEFLJR8PSLFSઃܭͷίπ &CJTVSC.BTBUP/BLBNVSB !N@OBLBNVSB

  2. ॲཧ݁Ռ͕௥͍΍͍͢Α͏ʹ ॲཧ͕ਖ਼͘͠ऴΘΔΑ͏ʹ

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

    P S L F S ઃ ܭ ͷ ί π
  4. 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 ઃ ܭ ͷ ί π
  5. 2VFVFΛ෼͚ͯ8FJHIUΛઃఆ͢Δ 4 J E F L J R  8

    P S L F S ઃ ܭ ͷ ί π
  6. 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 ઃ ܭ ͷ ί π
  7. 8PSLFSͷॲཧ࣌ؒΛ୹͘͢Δ 4 J E F L J R  8

    P S L F S ઃ ܭ ͷ ί π
  8. 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 ઃ ܭ ͷ ί π
  9. 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 ઃ ܭ ͷ ί π
  10. 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 ઃ ܭ ͷ ί π
  11. 8PSLFS಺෦Ͱঢ়ଶΛ࣋ͨͳ͍ 4 J E F L J R  8

    P S L F S ઃ ܭ ͷ ί π
  12. 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 ઃ ܭ ͷ ί π
  13. 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 ઃ ܭ ͷ ί π
  14. 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 ઃ ܭ ͷ ί π
  15. %FBE࣌ͷॲཧΛڞ௨Խ͢Δ 4 J E F L J R  8

    P S L F S ઃ ܭ ͷ ί π
  16. 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 ઃ ܭ ͷ ί π
  17.  3FUSZΛઃఆ͢Δ  2VFVFΛ෼͚ͯ8FJHIUΛઃఆ͢Δ  8PSLFSͷॲཧ࣌ؒΛ୹͘͢Δ  8PSLFS಺෦Ͱঢ়ଶΛ࣋ͨͳ͍  %FBE࣌ͷॲཧΛڞ௨Խ͢Δ

    4 J E F L J R  8 P S L F S ઃ ܭ ͷ ί π
  18. IUUQTHJUIVCDPNNQFSIBNTJEFLJRXJLJ 4 J E F L J R  8

    P S L F S ઃ ܭ ͷ ί π