elock-client 0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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!" }
@@ -0,0 +1,18 @@
1
+ # -*- mode: ruby -*-
2
+
3
+ require 'time'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "elock-client"
7
+ s.version = `git describe`.gsub('-', '.')
8
+ s.date = Time.now.strftime '%Y-%m-%d'
9
+ s.summary = "Ruby client for the elock distributed lock server."
10
+ s.email = "dustin@spy.net"
11
+ s.homepage = "http://github.com/dustin/elock-ruby"
12
+ s.description = "elock-client provides a simple ruby interface to the elock distributed lock server."
13
+ s.has_rdoc = true
14
+ s.authors = ["Dustin Sallings"]
15
+ s.files = ["README.txt", "elock-client.gemspec", "lib/elock-client.rb"]
16
+ s.rdoc_options = ["--main", "README.txt"]
17
+ s.extra_rdoc_files = ["README.txt"]
18
+ end
@@ -0,0 +1,62 @@
1
+ require 'socket'
2
+ require 'fcntl'
3
+
4
+ # Exception thrown from with_lock when the lock was not acquired.
5
+ class Locked < RuntimeError
6
+ end
7
+
8
+ # A client for elock.
9
+ class ELock
10
+
11
+ # Construct a new ELock client pointed at the given server.
12
+ def initialize(host, port=11400)
13
+ connect host, port
14
+ end
15
+
16
+ # Acquire a lock. This method should return very quickly except in the
17
+ # case where a timeout is requested and the lock is unavailable.
18
+ def lock(name, timeout=nil)
19
+ do_cmd(timeout.nil? ? "lock #{name}" : "lock #{name} #{timeout}")
20
+ end
21
+
22
+ # Release a lock from elock.
23
+ def unlock(name)
24
+ do_cmd("unlock #{name}")
25
+ end
26
+
27
+ # Unlock all locks obtained by this client.
28
+ def unlock_all
29
+ do_cmd("unlock_all")
30
+ end
31
+
32
+ # Run a block while holding the named lock.
33
+ # raises Locked if the lock could not be acquired.
34
+ def with_lock(name, timeout=nil)
35
+ if lock(name, timeout).first == 200
36
+ yield
37
+ else
38
+ raise Locked
39
+ end
40
+ ensure
41
+ unlock name
42
+ end
43
+
44
+ # Close this connction
45
+ def close
46
+ @socket = @socket.close
47
+ end
48
+
49
+ private
50
+
51
+ def connect(host, port)
52
+ @socket = TCPSocket.new(host, port.to_i)
53
+ @socket.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC)
54
+ end
55
+
56
+ def do_cmd(cmd)
57
+ @socket.write(cmd + "\r\n")
58
+ res = @socket.gets("\r\n")
59
+ [res.to_i, res[4..-3]]
60
+ end
61
+
62
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: elock-client
3
+ version: !ruby/object:Gem::Version
4
+ version: "0.3"
5
+ platform: ruby
6
+ authors:
7
+ - Dustin Sallings
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-09 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
+ licenses: []
31
+
32
+ post_install_message:
33
+ rdoc_options:
34
+ - --main
35
+ - README.txt
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: "0"
43
+ version:
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: "0"
49
+ version:
50
+ requirements: []
51
+
52
+ rubyforge_project:
53
+ rubygems_version: 1.3.5
54
+ signing_key:
55
+ specification_version: 3
56
+ summary: Ruby client for the elock distributed lock server.
57
+ test_files: []
58
+