ctcherry-starling 0.9.10
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +56 -0
- data/LICENSE +20 -0
- data/README.rdoc +90 -0
- data/Rakefile +56 -0
- data/bin/starling +6 -0
- data/bin/starling_top +57 -0
- data/etc/sample-config.yml +9 -0
- data/etc/starling.redhat +66 -0
- data/etc/starling.ubuntu +71 -0
- data/lib/starling.rb +168 -0
- data/lib/starling/handler.rb +237 -0
- data/lib/starling/persistent_queue.rb +156 -0
- data/lib/starling/queue_collection.rb +147 -0
- data/lib/starling/server.rb +125 -0
- data/lib/starling/server_runner.rb +317 -0
- data/spec/starling_server_spec.rb +216 -0
- data/starling.gemspec +21 -0
- metadata +109 -0
data/CHANGELOG
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
== 0.9.9
|
2
|
+
* remove dependency on SyslogLogger so that starling works on windows.
|
3
|
+
* fix config file loading so relative paths are expanded properly <jacob@jacobatzen.dk>
|
4
|
+
* clean up redhat init.d script <gaffneyc@gmail.com>
|
5
|
+
* add a 'fetch' command that is non-blocking equivalent to 'get' <aaron.hawkins@shawkvalue.com>
|
6
|
+
* implement the 'delete' method to allow queues to be deleted <timshadel@gmail.com>
|
7
|
+
|
8
|
+
== 0.9.8
|
9
|
+
* add fix to enable relative paths <david@motorator.com>
|
10
|
+
* fix tests so they don't run 10 times due to a stupid bug with how the server is forked <seth@mojodna.net>
|
11
|
+
* fix some other tests <romeda@gmail.com>
|
12
|
+
* fix some error messages <romeda@gmail.com>
|
13
|
+
* probably some other things <romeda@gmail.com>
|
14
|
+
|
15
|
+
== 0.9.7.9
|
16
|
+
* properly complain if the spool directory isn't writable <seth@mojodna.net>
|
17
|
+
* assume group and user privileges in a working order <seth@mojodna.net>
|
18
|
+
* support string user / group names in addition to uid/gids <seth@mojodna.net>
|
19
|
+
|
20
|
+
== 0.9.7.7
|
21
|
+
* added init.d scripts for redhat and ubuntu by Mike Perham <mperham@gmail.com>
|
22
|
+
* fixed dependencies for SyslogLogger, eventmachine and memcache-client by Mike Perham <mperham@gmail.com>
|
23
|
+
* added starling_top script to monitor starling server by Mike Perham <mperham@gmail.com>
|
24
|
+
* fixed starling_top to use 22122 as port by Abdul-Rahman Advany <abdulrahman@advany.com>
|
25
|
+
|
26
|
+
== 0.9.7.6 2008-06-24
|
27
|
+
* removed client code (will be added to different project) by Abdul-Rahman Advany <abdulrahman@advany.com>
|
28
|
+
|
29
|
+
== 0.9.7.5 2008-05-04
|
30
|
+
* added worker class, using starling client you can now run them in background by Abdul-Rahman Advany <abdulrahman@advany.com>
|
31
|
+
- handles creation of threadpool
|
32
|
+
- handles fetching of messages and passing these to the threads
|
33
|
+
- handles pushing of processed messages to starling again if needed
|
34
|
+
|
35
|
+
== 0.9.7 2008-05-03
|
36
|
+
* merged branch of AnotherBritt and Glenn Rempe by Abdul-Rahman Advany <abdulrahman@advany.com>
|
37
|
+
* rspeced tests by Abdul-Rahman Advany <abdulrahman@advany.com>
|
38
|
+
|
39
|
+
== 0.9.6 2008-04-30
|
40
|
+
* logging of message lifecycle as :age by AnotherBritt <?>
|
41
|
+
* added some extra logging options by AnotherBritt <?>
|
42
|
+
* added some test for epoll by AnotherBritt <?>
|
43
|
+
|
44
|
+
== 0.9.5.4 2008-04-28
|
45
|
+
|
46
|
+
* Bumped version number by Glenn Rempe <glenn.rempe@gmail.com>
|
47
|
+
* Purged all old RubyForge config. Now GitHub friendly by Glenn Rempe <glenn.rempe@gmail.com>
|
48
|
+
* New gemspec for GitHub gem auto-build-serve by Glenn Rempe <glenn.rempe@gmail.com>
|
49
|
+
|
50
|
+
== 0.9.4 2008-01-31 ==
|
51
|
+
* Evented code added using EventMachine by Chris Wanstrath <chris@ozmm.org>
|
52
|
+
|
53
|
+
== 2007-11-02
|
54
|
+
|
55
|
+
* Initial release
|
56
|
+
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2007 FIXME full name
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
= Name
|
2
|
+
|
3
|
+
Starling - a light weight server for reliable distributed message passing.
|
4
|
+
|
5
|
+
= Description
|
6
|
+
|
7
|
+
Starling is a powerful but simple messaging server that enables reliable
|
8
|
+
distributed queuing with an absolutely minimal overhead. It speaks the
|
9
|
+
MemCache protocol for maximum cross-platform compatibility. Any language
|
10
|
+
that speaks MemCache can take advantage of Starling's queue facilities.
|
11
|
+
|
12
|
+
= Installation
|
13
|
+
|
14
|
+
This fork of the Starling source is hosted at GitHub and can be found at:
|
15
|
+
|
16
|
+
http://github.com/starling/starling/tree/master
|
17
|
+
|
18
|
+
The original source was to be found at RubyForge but no longer exists there.
|
19
|
+
|
20
|
+
GitHub serves gems prefixed by a username to differentiate different forks.
|
21
|
+
This project can be installed with:
|
22
|
+
|
23
|
+
# THIS COMMAND ONE TIME ONLY
|
24
|
+
gem sources -a http://gems.github.com/
|
25
|
+
|
26
|
+
# As often as you like
|
27
|
+
sudo gem install starling-starling
|
28
|
+
|
29
|
+
See http://gems.github.com/ if you want more info about GitHub and gems.
|
30
|
+
|
31
|
+
= Quick Start Usage
|
32
|
+
|
33
|
+
# View the Starling help and usage message
|
34
|
+
starling --help
|
35
|
+
|
36
|
+
# In a console window start the Starling server. By default
|
37
|
+
# it runs verbosely in the foreground, listening on 127.0.0.1:22122
|
38
|
+
# and stores its files under /tmp/starling:
|
39
|
+
starling
|
40
|
+
|
41
|
+
# In a new console test the put and get of messages on a queue:
|
42
|
+
|
43
|
+
irb
|
44
|
+
>> require 'starling'
|
45
|
+
=> true
|
46
|
+
>> starling = Starling.new('127.0.0.1:22122')
|
47
|
+
=> MemCache: 1 servers, 1 buckets, ns: nil, ro: false
|
48
|
+
>> starling.set('my_queue', 12345)
|
49
|
+
=> nil
|
50
|
+
>> starling.get('my_queue')
|
51
|
+
=> 12345
|
52
|
+
|
53
|
+
# You can do a simple loop over a queue with something like:
|
54
|
+
>> loop { puts starling.get('my_queue'); sleep 1 }
|
55
|
+
12345
|
56
|
+
nil
|
57
|
+
nil
|
58
|
+
...
|
59
|
+
|
60
|
+
For more information run the following in a new console:
|
61
|
+
|
62
|
+
'gem server'
|
63
|
+
|
64
|
+
This will start a gem server on http://localhost:8808/ which you can view in your
|
65
|
+
browser to see the RDocs for the gem. Or generate rdocs by running the following
|
66
|
+
in a new console:
|
67
|
+
|
68
|
+
'rdoc'
|
69
|
+
|
70
|
+
= Known Issues
|
71
|
+
|
72
|
+
* Starling is "slow" as far as messaging systems are concerned. In practice,
|
73
|
+
it's fast enough.
|
74
|
+
|
75
|
+
== TODO
|
76
|
+
|
77
|
+
* Implement memcached instead of memcache as a client interface (to make it faster)
|
78
|
+
|
79
|
+
= Authors
|
80
|
+
|
81
|
+
* Blaine Cook <romeda@gmail.com>
|
82
|
+
* Chris Wanstrath <chris@ozmm.org>
|
83
|
+
* AnotherBritt <?>
|
84
|
+
* Glenn Rempe <?>
|
85
|
+
* Abdul-Rahman Advany <abdulrahman@advany.com>
|
86
|
+
|
87
|
+
= Copyright
|
88
|
+
|
89
|
+
Starling - a light-weight server for reliable distributed message passing.
|
90
|
+
Copyright 2007-2008 Blaine Cook <blaine@twitter.com>, Twitter Inc.
|
data/Rakefile
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake/rdoctask'
|
3
|
+
require 'spec/rake/spectask'
|
4
|
+
require File.join("lib", "starling", "server")
|
5
|
+
|
6
|
+
task :default => :spec
|
7
|
+
|
8
|
+
task :install do
|
9
|
+
sh %{gem build starling.gemspec}
|
10
|
+
sh %{sudo gem install starling-*.gem}
|
11
|
+
end
|
12
|
+
|
13
|
+
Spec::Rake::SpecTask.new do |t|
|
14
|
+
t.ruby_opts = ['-rtest/unit']
|
15
|
+
t.spec_files = FileList['spec/*_spec.rb']
|
16
|
+
t.fail_on_error = true
|
17
|
+
end
|
18
|
+
|
19
|
+
Rake::RDocTask.new do |rd|
|
20
|
+
rd.main = "README.rdoc"
|
21
|
+
rd.rdoc_files.include("README.rdoc", "lib/**/*.rb")
|
22
|
+
rd.rdoc_dir = 'doc'
|
23
|
+
end
|
24
|
+
|
25
|
+
desc "Generate a gemspec"
|
26
|
+
task :gemspec do
|
27
|
+
gemspec =<<-EOF
|
28
|
+
# this file is automatically generated
|
29
|
+
Gem::Specification.new do |s|
|
30
|
+
s.name = "starling"
|
31
|
+
s.version = "#{StarlingServer::VERSION}"
|
32
|
+
s.authors = ["Blaine Cook", "Chris Wanstrath", "Britt Selvitelle", "Glenn Rempe", "Abdul-Rahman Advany", "Seth Fitzsimmons"]
|
33
|
+
s.email = ["blaine@twitter.com", "chris@ozmm.org", "abdulrahman@advany.com", "starlingmq@groups.google.com"]
|
34
|
+
s.homepage = "http://github.com/starling/starling/"
|
35
|
+
s.summary = "Starling is a lightweight, transactional, distributed queue server"
|
36
|
+
s.description = s.summary
|
37
|
+
|
38
|
+
s.files = #{Dir.glob("**/*").select { |f| File.file?(f) }.inspect}
|
39
|
+
s.executables = ["starling", "starling_top"]
|
40
|
+
s.require_paths = ["lib"]
|
41
|
+
|
42
|
+
s.has_rdoc = true
|
43
|
+
s.rdoc_options = ["--quiet", "--title", "starling documentation", "--opname", "index.html", "--line-numbers", "--main", "README.rdoc", "--inline-source"]
|
44
|
+
s.extra_rdoc_files = ["README.rdoc", "CHANGELOG", "LICENSE"]
|
45
|
+
|
46
|
+
s.add_dependency 'memcache-client', [">= 1.6.2"]
|
47
|
+
s.add_dependency 'eventmachine', [">= 0.12.0"]
|
48
|
+
end
|
49
|
+
EOF
|
50
|
+
|
51
|
+
open("starling.gemspec", "w") do |f|
|
52
|
+
f << gemspec
|
53
|
+
end
|
54
|
+
|
55
|
+
puts "gemspec successfully created."
|
56
|
+
end
|
data/bin/starling
ADDED
data/bin/starling_top
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'optparse'
|
4
|
+
require 'ostruct'
|
5
|
+
require 'socket'
|
6
|
+
|
7
|
+
@options = OpenStruct.new
|
8
|
+
@options.hostname = 'localhost'
|
9
|
+
@options.port = 22122
|
10
|
+
|
11
|
+
op = OptionParser.new do |opts|
|
12
|
+
opts.banner = "STARLING TOP\nUsage: startop [options]"
|
13
|
+
opts.separator "General Options:"
|
14
|
+
opts.on("-h HOSTNAME", "--hostname=HOSTNAME", "Hostname [default: localhost]") do |h|
|
15
|
+
@options.hostname = h
|
16
|
+
end
|
17
|
+
opts.on("-p PORT", "--port=PORT", Integer, "Port [default: 22122]") do |p|
|
18
|
+
@options.port = p
|
19
|
+
end
|
20
|
+
opts.on_tail("--help", "Show this message") do
|
21
|
+
puts opts
|
22
|
+
exit
|
23
|
+
end
|
24
|
+
end
|
25
|
+
op.parse!
|
26
|
+
|
27
|
+
|
28
|
+
def stats_data
|
29
|
+
data = ''
|
30
|
+
sock = TCPSocket.new(@options.hostname, @options.port)
|
31
|
+
sock.print("stats\r\n")
|
32
|
+
sock.flush
|
33
|
+
# memcached does not close the socket once it is done writing
|
34
|
+
# the stats data. We need to read line by line until we detect
|
35
|
+
# the END line and then stop/close on our side.
|
36
|
+
stats = sock.gets
|
37
|
+
while true
|
38
|
+
data += stats
|
39
|
+
break if stats.strip == 'END'
|
40
|
+
stats = sock.gets
|
41
|
+
end
|
42
|
+
sock.close
|
43
|
+
data
|
44
|
+
end
|
45
|
+
|
46
|
+
def parse(stats_data)
|
47
|
+
stats = []
|
48
|
+
stats_data.each_line do |line|
|
49
|
+
stats << "#{$1}: #{$2}" if line =~ /STAT (\w+) (\S+)/
|
50
|
+
end
|
51
|
+
stats.sort
|
52
|
+
end
|
53
|
+
|
54
|
+
stats = parse(stats_data)
|
55
|
+
stats.each do |stat|
|
56
|
+
puts stat
|
57
|
+
end
|
data/etc/starling.redhat
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
#!/bin/bash
|
2
|
+
#
|
3
|
+
# Make sure the /var/run/starling, /var/log/starling and /var/spool/starling
|
4
|
+
# all exist and are owned by starling:starling
|
5
|
+
#
|
6
|
+
# starling This shell script takes care of starting and stopping
|
7
|
+
# the starling server
|
8
|
+
# chkconfig: 345 98 98
|
9
|
+
# description: The starling queue server
|
10
|
+
# processname: starling
|
11
|
+
# pidfile: /var/run/starling/starling.pid
|
12
|
+
# logfile: /var/log/starling/starling.log
|
13
|
+
|
14
|
+
# Source function library.
|
15
|
+
. /etc/rc.d/init.d/functions
|
16
|
+
|
17
|
+
# Source networking configuration.
|
18
|
+
[ -f /etc/sysconfig/network ] && . /etc/sysconfig/network
|
19
|
+
|
20
|
+
DUSER=starling
|
21
|
+
DGROUP=starling
|
22
|
+
LOGFILE=/var/log/starling/starling.log
|
23
|
+
SPOOLDIR=/var/spool/starling
|
24
|
+
PORT=22122
|
25
|
+
LISTEN=0.0.0.0
|
26
|
+
PIDFILE=/var/run/starling/starling.pid
|
27
|
+
|
28
|
+
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
|
29
|
+
NAME=starling
|
30
|
+
INSTALL_DIR=/usr/local/bin
|
31
|
+
DAEMON=$INSTALL_DIR/$NAME
|
32
|
+
OPTS="-h $LISTEN -p $PORT -d -q $SPOOLDIR -P $PIDFILE -L $LOGFILE -u $DUSER -g $DGROUP"
|
33
|
+
|
34
|
+
start() {
|
35
|
+
echo -n $"Starting starling: "
|
36
|
+
|
37
|
+
daemon --pidfile=$PIDFILE $DAEMON $OPTS
|
38
|
+
echo
|
39
|
+
}
|
40
|
+
|
41
|
+
stop() {
|
42
|
+
echo -n $"Stopping starling: "
|
43
|
+
|
44
|
+
killproc -p $PIDFILE starling
|
45
|
+
echo
|
46
|
+
}
|
47
|
+
|
48
|
+
case "$1" in
|
49
|
+
start)
|
50
|
+
start
|
51
|
+
;;
|
52
|
+
stop)
|
53
|
+
stop
|
54
|
+
;;
|
55
|
+
restart)
|
56
|
+
stop
|
57
|
+
sleep 3
|
58
|
+
start
|
59
|
+
;;
|
60
|
+
status)
|
61
|
+
status -p $PIDFILE starling
|
62
|
+
;;
|
63
|
+
*)
|
64
|
+
echo $"Usage: $0 {start|stop|restart|status}"
|
65
|
+
exit 1
|
66
|
+
esac
|
data/etc/starling.ubuntu
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
#! /bin/sh
|
2
|
+
### BEGIN INIT INFO
|
3
|
+
# Provides: starling
|
4
|
+
# Required-Start: $local_fs $remote_fs
|
5
|
+
# Required-Stop: $local_fs $remote_fs
|
6
|
+
# Default-Start: 2 3 4 5
|
7
|
+
# Default-Stop: S 0 1 6
|
8
|
+
# Short-Description: Starling queue server
|
9
|
+
# Description: The Starling distributed, transactional queue server
|
10
|
+
### END INIT INFO
|
11
|
+
# Author: Twitter
|
12
|
+
# Version: 0.9.7.7
|
13
|
+
|
14
|
+
set -e
|
15
|
+
|
16
|
+
DUSER=starling
|
17
|
+
DGROUP=starling
|
18
|
+
LOGFILE=/var/log/starling/starling.log
|
19
|
+
SPOOLDIR=/var/spool/starling
|
20
|
+
PORT=22122
|
21
|
+
LISTEN=0.0.0.0
|
22
|
+
PIDFILE=/var/run/starling/starling.pid
|
23
|
+
|
24
|
+
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
|
25
|
+
NAME=starling
|
26
|
+
DESC="Starling"
|
27
|
+
INSTALL_DIR=/usr/local/bin
|
28
|
+
DAEMON=$INSTALL_DIR/$NAME
|
29
|
+
SCRIPTNAME=/etc/init.d/$NAME
|
30
|
+
OPTS="-h $LISTEN -p $PORT -d -q $SPOOLDIR -P $PIDFILE -L $LOGFILE"
|
31
|
+
|
32
|
+
. /lib/lsb/init-functions
|
33
|
+
|
34
|
+
|
35
|
+
# Gracefully exit if the package has been removed.
|
36
|
+
test -x $DAEMON || exit 0
|
37
|
+
|
38
|
+
d_start() {
|
39
|
+
log_begin_msg "Starting Starling Server..."
|
40
|
+
start-stop-daemon -c $DUSER:$DGROUP --start --quiet --pidfile $PIDFILE --exec $DAEMON \
|
41
|
+
-- $OPTS || log_end_msg 1
|
42
|
+
log_end_msg 0
|
43
|
+
}
|
44
|
+
|
45
|
+
d_stop() {
|
46
|
+
log_begin_msg "Stopping Starling Server..."
|
47
|
+
start-stop-daemon -c $DUSER:$DGROUP --stop --quiet --pidfile $PIDFILE \
|
48
|
+
|| log_end_msg 1
|
49
|
+
log_end_msg 0
|
50
|
+
}
|
51
|
+
|
52
|
+
case "$1" in
|
53
|
+
start)
|
54
|
+
d_start
|
55
|
+
;;
|
56
|
+
stop)
|
57
|
+
d_stop
|
58
|
+
;;
|
59
|
+
restart|force-reload|reload)
|
60
|
+
d_stop
|
61
|
+
sleep 2
|
62
|
+
d_start
|
63
|
+
;;
|
64
|
+
*)
|
65
|
+
echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
|
66
|
+
exit 3
|
67
|
+
;;
|
68
|
+
esac
|
69
|
+
|
70
|
+
exit 0
|
71
|
+
|
data/lib/starling.rb
ADDED
@@ -0,0 +1,168 @@
|
|
1
|
+
require 'memcache'
|
2
|
+
|
3
|
+
class Starling < MemCache
|
4
|
+
|
5
|
+
WAIT_TIME = 0.25
|
6
|
+
alias_method :_original_get, :get
|
7
|
+
alias_method :_original_delete, :delete
|
8
|
+
|
9
|
+
##
|
10
|
+
# fetch an item from a queue.
|
11
|
+
|
12
|
+
def get(*args)
|
13
|
+
loop do
|
14
|
+
response = _original_get(*args)
|
15
|
+
return response unless response.nil?
|
16
|
+
sleep WAIT_TIME
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
##
|
21
|
+
# will return the next item or nil
|
22
|
+
|
23
|
+
def fetch(*args)
|
24
|
+
_original_get(*args)
|
25
|
+
end
|
26
|
+
|
27
|
+
##
|
28
|
+
# Delete the key (queue) from all Starling servers. This is necessary
|
29
|
+
# because the random way a server is chosen in #get_server_for_key
|
30
|
+
# implies that the queue could easily be spread across the entire
|
31
|
+
# Starling cluster.
|
32
|
+
|
33
|
+
def delete(key, expiry = 0)
|
34
|
+
with_servers do
|
35
|
+
_original_delete(key, expiry)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
##
|
40
|
+
# Provides a way to work with a specific list of servers by
|
41
|
+
# forcing all calls to #get_server_for_key to use a specific
|
42
|
+
# server, and changing that server each time that the call
|
43
|
+
# yields to the block provided. This helps work around the
|
44
|
+
# normally random nature of the #get_server_for_key method.
|
45
|
+
#
|
46
|
+
# Acquires the mutex for the entire duration of the call
|
47
|
+
# since unrelated calls to #get_server_for_key might be
|
48
|
+
# adversely affected by the non_random result.
|
49
|
+
def with_servers(my_servers = @servers.dup)
|
50
|
+
return unless block_given?
|
51
|
+
with_lock do
|
52
|
+
my_servers.each do |server|
|
53
|
+
@force_server = server
|
54
|
+
yield
|
55
|
+
end
|
56
|
+
@force_server = nil
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
##
|
61
|
+
# insert +value+ into +queue+.
|
62
|
+
#
|
63
|
+
# +expiry+ is expressed as a UNIX timestamp
|
64
|
+
#
|
65
|
+
# If +raw+ is true, +value+ will not be Marshalled. If +raw+ = :yaml, +value+
|
66
|
+
# will be serialized with YAML, instead.
|
67
|
+
|
68
|
+
def set(queue, value, expiry = 0, raw = false)
|
69
|
+
retries = 0
|
70
|
+
begin
|
71
|
+
if raw == :yaml
|
72
|
+
value = YAML.dump(value)
|
73
|
+
raw = true
|
74
|
+
end
|
75
|
+
|
76
|
+
super(queue, value, expiry, raw)
|
77
|
+
rescue MemCache::MemCacheError => e
|
78
|
+
retries += 1
|
79
|
+
sleep WAIT_TIME
|
80
|
+
retry unless retries > 3
|
81
|
+
raise e
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
##
|
86
|
+
# returns the number of items in +queue+. If +queue+ is +:all+, a hash of all
|
87
|
+
# queue sizes will be returned.
|
88
|
+
|
89
|
+
def sizeof(queue, statistics = nil)
|
90
|
+
statistics ||= stats
|
91
|
+
|
92
|
+
if queue == :all
|
93
|
+
queue_sizes = {}
|
94
|
+
available_queues(statistics).each do |queue|
|
95
|
+
queue_sizes[queue] = sizeof(queue, statistics)
|
96
|
+
end
|
97
|
+
return queue_sizes
|
98
|
+
end
|
99
|
+
|
100
|
+
statistics.inject(0) { |m,(k,v)| m + v["queue_#{queue}_items"].to_i }
|
101
|
+
end
|
102
|
+
|
103
|
+
##
|
104
|
+
# returns a list of available (currently allocated) queues.
|
105
|
+
|
106
|
+
def available_queues(statistics = nil)
|
107
|
+
statistics ||= stats
|
108
|
+
|
109
|
+
statistics.map { |k,v|
|
110
|
+
v.keys
|
111
|
+
}.flatten.uniq.grep(/^queue_(.*)_items/).map { |v|
|
112
|
+
v.gsub(/^queue_/, '').gsub(/_items$/, '')
|
113
|
+
}.reject { |v|
|
114
|
+
v =~ /_total$/ || v =~ /_expired$/
|
115
|
+
}
|
116
|
+
end
|
117
|
+
|
118
|
+
##
|
119
|
+
# iterator to flush +queue+. Each element will be passed to the provided
|
120
|
+
# +block+
|
121
|
+
|
122
|
+
def flush(queue)
|
123
|
+
sizeof(queue).times do
|
124
|
+
v = get(queue)
|
125
|
+
yield v if block_given?
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
private
|
130
|
+
|
131
|
+
def get_server_for_key(key)
|
132
|
+
raise ArgumentError, "illegal character in key #{key.inspect}" if key =~ /\s/
|
133
|
+
raise ArgumentError, "key too long #{key.inspect}" if key.length > 250
|
134
|
+
raise MemCacheError, "No servers available" if @servers.empty?
|
135
|
+
return @force_server if @force_server
|
136
|
+
|
137
|
+
bukkits = @buckets.dup
|
138
|
+
bukkits.nitems.times do |try|
|
139
|
+
n = rand(bukkits.nitems)
|
140
|
+
server = bukkits[n]
|
141
|
+
return server if server.alive?
|
142
|
+
bukkits.delete_at(n)
|
143
|
+
end
|
144
|
+
|
145
|
+
raise MemCacheError, "No servers available (all dead)"
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
|
150
|
+
class MemCache
|
151
|
+
|
152
|
+
protected
|
153
|
+
|
154
|
+
##
|
155
|
+
# Ensure that everything within the given block is executed
|
156
|
+
# within the locked mutex if this client is multithreaded.
|
157
|
+
# If the client isn't multithreaded, the block is simply executed.
|
158
|
+
def with_lock
|
159
|
+
return unless block_given?
|
160
|
+
begin
|
161
|
+
@mutex.lock if @multithread
|
162
|
+
yield
|
163
|
+
ensure
|
164
|
+
@mutex.unlock if @multithread
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
end
|