Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added wait-queue

  • Loading branch information...
commit d4cd2f05d2ec1e97bf1444f35d17e65321561c4b 1 parent 74c69dc
Makoto Inoue authored
30 wait-queue/README.rdoc
View
@@ -0,0 +1,30 @@
+= Queue
+
+Implements a simple function to wait until task is put into a queue. The queue will be deleted once dequeued.
+
+== Starting server with extension
+ > ttserver -ext wait_queue test.tch
+
+== Executing from command line (subscriber)
+ > tcrmgr ext localhost wait_queue foo 1
+
+ # Waits until enqueue is called.
+
+ > bar
+
+== Executing from command line (publisher)
+ > tcrmgr ext localhost enqueue foo bar
+ > bar
+
+== Executing via Ruby (subscriber)
+
+ > ruby wait_queue.rb
+
+ # Waits until enqueue is called.
+
+ > bar
+
+== Executing via Ruby (publisher)
+
+ > ruby enqueue.rb
+ > bar
7 wait-queue/enqueue.rb
View
@@ -0,0 +1,7 @@
+require 'rubygems'
+require 'rufus/tokyo/tyrant' # sudo gem install rufus-tokyo
+
+t = Rufus::Tokyo::Tyrant.new('127.0.0.1', 1978)
+puts t.ext(:enqueue, 'foo', 'bar')
+
+t.close
54 wait-queue/queue.lua
View
@@ -0,0 +1,54 @@
+-- queue.lua example is from Tokyo Tyrant source code
+--
+-- Queue mechanism by the Lua extension of Tokyo Tyrant
+--
+
+
+
+----------------------------------------------------------------
+-- public functions
+----------------------------------------------------------------
+
+
+-- enqueue a record
+function enqueue(key, value)
+ local id = _adddouble(key, 1)
+ print (key)
+ print (value)
+ if not id then
+ return nil
+ end
+ key = string.format("%s\t%012d", key, id)
+ if not _putkeep(key, value) then
+ return "not ok"
+ end
+ return "ok"
+end
+
+-- dequeue a record
+function dequeue(key, max)
+ max = tonumber(max)
+ if not max or max < 1 then
+ max = 1
+ end
+ key = string.format("%s\t", key)
+ local keys = _fwmkeys(key, max)
+ local res = ""
+ for i = 1, #keys do
+ local key = keys[i]
+ local value = _get(key)
+ if _out(key) and value then
+ res = res .. value .. "\n"
+ end
+ end
+ return res
+end
+
+-- get the queue size
+function queuesize(key)
+ key = string.format("%s\t", key)
+ local keys = _fwmkeys(key)
+ return #keys
+end
+
+-- END OF FILE
15 wait-queue/wait-queue.lua
View
@@ -0,0 +1,15 @@
+require "queue"
+
+function wait_queue(key, max)
+ r = ""
+ repeat
+ sleep(0.1)
+ r = dequeue(key, max)
+ until (r ~= "")
+
+ return r
+end
+
+function sleep(n)
+ os.execute("sleep " .. tonumber(n))
+end
6 wait-queue/wait_queue.rb
View
@@ -0,0 +1,6 @@
+require 'rubygems'
+require 'rufus/tokyo/tyrant' # sudo gem install rufus-tokyo
+
+t = Rufus::Tokyo::Tyrant.new('127.0.0.1', 1978)
+puts t.ext(:wait_queue, 'foo', 1)
+t.close
Please sign in to comment.
Something went wrong with that request. Please try again.