Skip to content

Instantly share code, notes, and snippets.

@denzuko
Forked from gleicon/restmq.rb
Created October 8, 2010 13:56
Show Gist options
  • Save denzuko/616837 to your computer and use it in GitHub Desktop.
Save denzuko/616837 to your computer and use it in GitHub Desktop.
# Sinatra minimalist RestMQ
# no COMET, just /q/ routes and queue logic
# the core of RestMQ is how it uses Redis' data types
%w(rubygems redis sinatra).each do |lib| require lib; end
QUEUESET = 'QUEUESET' # queue index
UUID_SUFFIX = ':UUID' # queue unique id
QUEUE_SUFFIX = ':queue' # suffix to identify each queue's LIST
reds = Redis.new
module Sinatra
module QueueCheck
def queue_check(queue)
before {
halt 404, 'Not found (empty queue)\n' if queue == nil
}
end
end
register QueueCheck
end
before do
queue = params['splat'].to_s
throw :halt, [404, "Not found"] if queue == nil
end
get '/q' do
b = reds.smembers QUEUESET
check_que b
b.map! do |q| q = '/q/'+q end
b.to_json
end
get '/q/*' do
queue = queue + QUEUE_SUFFIX
b = reds.rpop queue
check_que b
v = reds.get b
"{'value':#{v}'key':#{b}}"
end
post '/q/*' do
value = params['value'].to_s
q1 = queue + QUEUE_SUFFIX
uuid = reds.incr queue + UUID_SUFFIX
reds.sadd QUEUESET, q1
lkey = queue + ':' + uuid.to_s
reds.set lkey, value
reds.lpush q1, lkey
"{ok, #{lkey}}"
end
@gleicon
Copy link

gleicon commented Oct 9, 2010

sweet :)

@gleicon
Copy link

gleicon commented Oct 9, 2010

btw, check this one: http://gist.github.com/524240 /q alone list all queues ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment