dustin-elock-client 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.txt +10 -0
- data/lib/elock-client.rb +53 -0
- metadata +56 -0
data/README.txt
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
Ruby Client for elock
|
2
|
+
|
3
|
+
This library provides an easy-to-use ruby client for
|
4
|
+
elock (https://github.com/dustin/elock).
|
5
|
+
|
6
|
+
== Example Usage (automatic lock management)
|
7
|
+
|
8
|
+
require 'elock-client'
|
9
|
+
l = ELock.new 'localhost'
|
10
|
+
l.with_lock('some_lock') { puts "I got the lock!" }
|
data/lib/elock-client.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'socket'
|
2
|
+
require 'fcntl'
|
3
|
+
|
4
|
+
# A client for elock.
|
5
|
+
class ELock
|
6
|
+
|
7
|
+
# Construct a new ELock client pointed at the given server.
|
8
|
+
def initialize(host, port=11400)
|
9
|
+
connect host, port
|
10
|
+
end
|
11
|
+
|
12
|
+
# Acquire a lock. This method should return very quickly except in the
|
13
|
+
# case where a timeout is requested and the lock is unavailable.
|
14
|
+
def lock(name, timeout=nil)
|
15
|
+
do_cmd(timeout.nil? ? "lock #{name}" : "lock #{name} #{timeout}")
|
16
|
+
end
|
17
|
+
|
18
|
+
# Release a lock from elock.
|
19
|
+
def unlock(name)
|
20
|
+
do_cmd("unlock #{name}")
|
21
|
+
end
|
22
|
+
|
23
|
+
# Unlock all locks obtained by this client.
|
24
|
+
def unlock_all
|
25
|
+
do_cmd("unlock_all")
|
26
|
+
end
|
27
|
+
|
28
|
+
# Run a block while holding the named lock.
|
29
|
+
def with_lock(name, timeout=nil)
|
30
|
+
yield if lock(name, timeout).first == 200
|
31
|
+
ensure
|
32
|
+
unlock name
|
33
|
+
end
|
34
|
+
|
35
|
+
# Close this connction
|
36
|
+
def close
|
37
|
+
@socket = @socket.close
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def connect(host, port)
|
43
|
+
@socket = TCPSocket.new(host, port.to_i)
|
44
|
+
@socket.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
|
45
|
+
end
|
46
|
+
|
47
|
+
def do_cmd(cmd)
|
48
|
+
@socket.write(cmd + "\r\n")
|
49
|
+
res = @socket.gets("\r\n")
|
50
|
+
[res.to_i, res[4..-3]]
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dustin-elock-client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: "0.1"
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dustin Sallings
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-05-29 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: elock-client provides a simple ruby interface to the elock distributed lock server.
|
17
|
+
email: dustin@spy.net
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.txt
|
24
|
+
files:
|
25
|
+
- README.txt
|
26
|
+
- elock-client.gemspec
|
27
|
+
- lib/elock-client.rb
|
28
|
+
has_rdoc: true
|
29
|
+
homepage: http://github.com/dustin/elock-ruby
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options:
|
32
|
+
- --main
|
33
|
+
- README.txt
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: "0"
|
41
|
+
version:
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
version:
|
48
|
+
requirements: []
|
49
|
+
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 1.0.1
|
52
|
+
signing_key:
|
53
|
+
specification_version: 2
|
54
|
+
summary: Ruby client for the elock distributed lock server.
|
55
|
+
test_files: []
|
56
|
+
|