Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

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

ryonext
September 17, 2014

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

ryonext

September 17, 2014
Tweet

More Decks by ryonext

Other Decks in Programming

Transcript

  1. ◠㷩◠ ☛Ͳͬͪʹ΋ొ࿥͓͍ͯͨ͠Αʔ # 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
  2. ͦ͜Ͱ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
  3. ͜͏͍͏ײ͡Ͱॻ͚·͢ Redis::Classy.db = Redis.new() mutex = Redis::Mutex.new('lock_access', expire: 180) begin

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