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

Redisでバッチ処理を冗長化しつつ排他制御

Affc58cd0f63630fb0c5bcec49902546?s=47 ryonext
September 17, 2014

 Redisでバッチ処理を冗長化しつつ排他制御

Affc58cd0f63630fb0c5bcec49902546?s=128

ryonext

September 17, 2014
Tweet

Transcript

  1. 3FEJTͰόονॲཧΛ ৑௕Խͭͭ͠ഉଞ੍ޚ

  2. ࣗݾ঺հ

  3. ͜͏͍͏ύλʔϯ ͓Εͷ3BJMT"QQ ֎෦ͷ"1* 6TFS

  4. ຖճ"1*ϦΫΤετ͢Δͱ w Ԡ౴଎౓తʹͭΒ͍ w "1*αʔόʹෛՙ͕͔͔Δ

  5. ͜͏͍͏෩ʹ͢Δ ͓Εͷ3BJMT"QQ ֎෦ͷ"1* 6TFS ϢʔβʔϦΫΤετ όονॲཧ

  6. 3BJMTͳΒ w XIFOFWFS DBQJTUSBOPͰBQQαʔόʹσϓϩ ΠͷλΠϛϯάͰDSPO࡞੒Ͱ͖ͯ௒ḿΔ

  7. 㵺㱿㵺 ɻP ଴ͯΑʁ"QQαʔόͬͯ৑௕Խ͞ΕͯΔΑͳʁ

  8. c"QQ "QQcᵎ ☋ʆ ᵊࡾ

  9. ◠㷩◠ ☛Ͳͬͪʹ΋ొ࿥͓͍ͯͨ͠Αʔ # Begin Whenever generated tasks for: oreno_app PATH=/var/www/oreno_app/shared/bundle/ruby/2.1.0/bin:/usr/local/rbenv/

    versions/2.1.2/bin:/usr/local/rbenv/libexec:/usr/local/sbin:/usr/local/ bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games ! 0,5,10,15,20,25,30,35,40,45,50,55 * * * * /bin/bash -l -c 'cd /var/www/ oreno_app/releases/20140917060508 && RAILS_ENV=production bundle exec rake kankore:ensei --silent >> log/crontab.log 2>&1' ! # End Whenever generated tasks for: oreno_app
  10. ‧‪‬‭▓▒░ `Т` ░▒▓‭‬‪‧͏Θ͋͋͋͋͋͋

  11. ՕॴͰ૸Δͱ w ࢿݯͷແବݣ͍ײ w Կ͔ΛΠϯΫϦϝϯτ͢ΔͳͲɺͭ૸ͬͨΒக ໋తͳॲཧͩͱࢮ͵

  12. ͜ΕͰղܾʂʁ ͓Εͷ3BJMT"QQ ͓Εͷ3BJMT"QQ όον࣮ߦαʔό

  13. ͍͕ͭ͜མͪͨΒࢮ͵ όον࣮ߦαʔό ʀŋAТŋ

  14. ͦ͜Ͱ3FEJT task nanika: :environment do sleep(rand(5)) # 実行時間をずらす redis =

    Redis.new unless redis.get(‘task_executed') redis.setex('task_executed', 300, Time.now) # やりたいタスクを書く end end
  15. ͜͏͢Δ͜ͱͰ w ઌʹಈ͍ͨํ͸3FEJTʹΩʔ͕ͳ͚Ε͹ΩʔΛ ࡞ͬͯλεΫ࣮ߦ w Ωʔ͕͋ͬͨΒଈ࠲ʹ΍ΊΔ w Ωʔ͸Ұఆ࣌ؒͰʢ࣍ͷDSPO࣮ߦ·Ͱʹʣফ͑ Δ w

    ͭ·Γɺ৑௕Խͭͭ͠ഉଞ੍ޚͰ͖Δ
  16. ͱ͍͏͜ͱΛ΍ͬͯ͘ΕΔ(FN ͕͋Γ·ͨ͠ w IUUQTHJUIVCDPNLFOOSFEJTNVUFY

  17. ͜͏͍͏ײ͡Ͱॻ͚·͢ Redis::Classy.db = Redis.new() mutex = Redis::Mutex.new('lock_access', expire: 180) begin

    mutex.lock! # やりたい処理を書く rescue Redis::Mutex::LockError puts 'locked' end
  18. ͓͠·͍