Skip to content

Instantly share code, notes, and snippets.

@adstage-david
Created December 12, 2014 03:45
Show Gist options
  • Save adstage-david/d1057fb6e4b1a676cce4 to your computer and use it in GitHub Desktop.
Save adstage-david/d1057fb6e4b1a676cce4 to your computer and use it in GitHub Desktop.
Reproduces thread error - run in context of rails app with sidekiq_unique_jobs installed
#!/usr/bin/env ruby
require 'benchmark'
require_relative '../config/environment'
threads = []
def connection(conn)
if conn
conn
else
Sidekiq.redis{|con| con }
end
end
def do_work(conn, i, j)
connection(conn).watch("foo#{i}#{j}")
connection(conn).get("foo#{i}#{j}")
connection(conn).get("foo#{i}#{j}")
connection(conn).multi do
connection(conn).set("foo#{i}#{j}", "bar")
connection(conn).setex("foo#{i}", 100, "#{j}")
end
connection(conn).get("foo#{i}#{j}")
connection(conn).get("foo#{i}")
end
5.times do |a|
threads << Celluloid::Future.new do |t|
100.times do |b|
if rand > 0.5
do_work(nil, a, b)
else
Sidekiq.redis{|conn|
do_work(conn, a, b)
}
end
end
puts "Thread #{a} batch done. "
end
end
threads.map(&:value)
puts "Completed."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment