Skip to content
This repository
Browse code

move to new thrift_client, and factor out kestrel_client.

  • Loading branch information...
commit 1ff3cae7c6cbe752892af6c9a646df7876096fed 1 parent 1b87f9c
authored
2  Gemfile
... ...
@@ -1,5 +1,5 @@
1 1
 # use "bundle install" to update gems; "gem install bundler" to install bundler.
2 2
 source :rubygems
3 3
 source "http://gems.local.twitter.com"
4  
-gem "thrift_client", "0.8.0"
  4
+gem "thrift_client", "0.8.1"
5 5
 gem "thrift", "0.8.0"
75  src/main/ruby/kestrel_client.rb
... ...
@@ -0,0 +1,75 @@
  1
+require "rubygems"
  2
+require "thrift"
  3
+require "thrift_client"
  4
+require "kestrel"
  5
+
  6
+class KestrelClient
  7
+  DEFAULTS = { :transport_wrapper => Thrift::FramedTransport }
  8
+  DEFAULT_ITEM_TIMEOUT_MSEC = 60000
  9
+
  10
+  def initialize(servers = nil, options = {})
  11
+    if servers.nil? or servers.empty?
  12
+      STDERR.puts "No servers specified, using 127.0.0.1:2229"
  13
+      servers = ['127.0.0.1:2229']
  14
+    else
  15
+      servers = Array(servers)
  16
+    end
  17
+
  18
+    @client = ThriftClient.new(Kestrel::Client, servers, DEFAULTS.merge(options))
  19
+  end
  20
+
  21
+  def version
  22
+    @client.get_version()
  23
+  end
  24
+
  25
+  def queue(name)
  26
+    Queue.new(@client, name)
  27
+  end
  28
+
  29
+  class Queue
  30
+    def initialize(client, name)
  31
+      @client = client
  32
+      @name = name
  33
+    end
  34
+
  35
+    def peek
  36
+      @client.peek(@name)
  37
+    end
  38
+
  39
+    def put(items, expiration_msec = 0)
  40
+      @client.put(@name, Array(items), expiration_msec)
  41
+    end
  42
+
  43
+    def get(max_items = 1, timeout_msec = 0, auto_abort_msec = 0)
  44
+      @client.get(@name, max_items, timeout_msec, auto_abort_msec)
  45
+    end
  46
+
  47
+    def open(max_items = 1, timeout_msec = 0)
  48
+      get(max_items, timeout_msec, DEFAULT_ITEM_TIMEOUT_MSEC)
  49
+    end
  50
+
  51
+    def confirm(items)
  52
+      items = Array(items)
  53
+      case items[0]
  54
+      when Item
  55
+        @client.confirm(@name, items.map { |x| x.id })
  56
+      else
  57
+        @client.confirm(@name, items)
  58
+      end
  59
+    end
  60
+
  61
+    def abort(items)
  62
+      items = Array(items)
  63
+      case items[0]
  64
+      when Item
  65
+        @client.abort(@name, items.map { |x| x.id })
  66
+      else
  67
+        @client.abort(@name, items)
  68
+      end
  69
+    end
  70
+
  71
+    def flush
  72
+      @client.flush_queue(@name)
  73
+    end
  74
+  end
  75
+end
59  src/scripts/console
... ...
@@ -1,67 +1,12 @@
1 1
 #!/usr/bin/env ruby
2 2
 $: << File.dirname(__FILE__) + "/../../../target/gen-rb"
  3
+$: << File.dirname(__FILE__) + "/../../../src/main/ruby"
3 4
 
4 5
 require "rubygems"
5 6
 require "bundler/setup"
6  
-
7  
-require "thrift"
8  
-require "thrift_client"
9  
-require "kestrel"
  7
+require "kestrel_client"
10 8
 require "irb"
11 9
 
12  
-class KestrelClient
13  
-  DEFAULTS = { :transport_wrapper => Thrift::FramedTransport }
14  
-  DEFAULT_ITEM_TIMEOUT_MSEC = 60000
15  
-
16  
-  def initialize(servers = nil, options = {})
17  
-    if servers.nil? or servers.empty?
18  
-      STDERR.puts "No servers specified, using 127.0.0.1:2229"
19  
-      servers = ['127.0.0.1:2229']
20  
-    else
21  
-      servers = Array(servers)
22  
-    end
23  
-
24  
-    @client = ThriftClient.new(Kestrel::Client, servers, DEFAULTS.merge(options))
25  
-  end
26  
-
27  
-  def queue(name)
28  
-    Queue.new(@client, name)
29  
-  end
30  
-
31  
-  class Queue
32  
-    def initialize(client, name)
33  
-      @client = client
34  
-      @name = name
35  
-    end
36  
-
37  
-    def peek
38  
-      @client.peek(@name)
39  
-    end
40  
-
41  
-    def put(items, expiration_msec = 0)
42  
-      @client.put(@name, Array(items), expiration_msec)
43  
-    end
44  
-
45  
-    def get(max_items = 1, timeout_msec = 0, auto_abort_msec = 0)
46  
-      @client.get(@name, max_items, timeout_msec, auto_abort_msec)
47  
-    end
48  
-
49  
-    def open(max_items = 1, timeout_msec = 0)
50  
-      get(max_items, timeout_msec, DEFAULT_ITEM_TIMEOUT_MSEC)
51  
-    end
52  
-
53  
-    def confirm(items)
54  
-      items = Array(items)
55  
-      case items[0]
56  
-      when Item
57  
-        @client.confirm(@name, items.map { |x| x.id })
58  
-      else
59  
-        @client.confirm(@name, items)
60  
-      end
61  
-    end
62  
-  end
63  
-end
64  
-
65 10
 puts "Hint: the client is in the variable `$client`"
66 11
 $client = KestrelClient.new ARGV.shift
67 12
 

0 notes on commit 1ff3cae

Please sign in to comment.
Something went wrong with that request. Please try again.