Skip to content

Commit

Permalink
feat: Add task for expire all resque locks
Browse files Browse the repository at this point in the history
  • Loading branch information
bibendi committed Dec 11, 2017
1 parent 9339287 commit b0adbfa
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
3 changes: 1 addition & 2 deletions lib/resque/integration/engine.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# coding: utf-8

require 'redis'
require 'redis/version'
require 'rails/engine'
Expand All @@ -12,6 +10,7 @@ class Engine < Rails::Engine
rake_tasks do
load 'resque/integration/tasks/hooks.rake'
load 'resque/integration/tasks/resque.rake'
load 'resque/integration/tasks/lock.rake'
end

# Читает конфиг-файлы config/resque.yml и config/resque.local.yml,
Expand Down
37 changes: 37 additions & 0 deletions lib/resque/integration/tasks/lock.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require 'resque/integration/unique'

namespace :resque do
namespace :lock do
task expire_all: :environment do
puts "Start expiring all resque locks"

redis = ::Resque.redis
cursor = 0
batch_size = 10_000
timeout = ::Resque::Integration::Unique::LOCK_TIMEOUT
count = 0
pattern = "lock:*"

loop do
cursor, keys = redis.scan(cursor, count: batch_size, match: pattern)
cursor = cursor.to_i

unless keys.empty?
redis.pipelined do
keys.each do |key|
redis.expire(key, timeout)
end
end

count += keys.size
puts "Expired #{count}..."
end

break if cursor.zero?
end

puts "Expired total #{count} keys."
puts "Done."
end
end
end

0 comments on commit b0adbfa

Please sign in to comment.