Last active
August 29, 2015 14:12
-
-
Save kusayuzayushko/d74d9e48689de6623221 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "redis" | |
class Test | |
attr_reader :quit | |
def initialize | |
@redis = Redis.new | |
end | |
def trap_signal | |
trap("INT") { | |
puts "get ready to exit" | |
@redis.rpush "TestQueue", @elem # I need to be sure that @emelent always puts back in the database | |
@quit = true} | |
end | |
def run! | |
trap_signal | |
@elem = "test string" | |
@redis.rpush "TestQueue", @elem | |
while !quit | |
@redis.blpop "TestQueue", @elem | |
# Do some work whith @element | |
sleep 1 | |
# And put it back in the database | |
@redis.rpush "TestQueue", @elem | |
end | |
end | |
end | |
Test.new.run! | |
=begin | |
Error is: | |
^Cget ready to exit | |
/usr/lib/ruby/2.1.0/monitor.rb:185:in `lock': can't be called from trap context (ThreadError) | |
from /usr/lib/ruby/2.1.0/monitor.rb:185:in `mon_enter' | |
from /usr/lib/ruby/2.1.0/monitor.rb:209:in `mon_synchronize' | |
from /home/kusayu/.gem/ruby/2.1.0/gems/redis-3.2.0/lib/redis.rb:37:in `synchronize' | |
from /home/kusayu/.gem/ruby/2.1.0/gems/redis-3.2.0/lib/redis.rb:991:in `rpush' | |
from test.rb:13:in `block in trap_signal' | |
from test.rb:24:in `call' | |
from test.rb:24:in `sleep' | |
from test.rb:24:in `run!' | |
from test.rb:32:in `<main>' | |
=end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment