Skip to content

Commit 2c092c1

Browse files
author
Chris Beer
committed
initial commit of lockit, sans tests
1 parent 1ac05be commit 2c092c1

File tree

6 files changed

+146
-4
lines changed

6 files changed

+146
-4
lines changed

README.rdoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
= lockit
22

3-
Description goes here.
3+
A ruby implementation of LockIt: A Simple File-based Convention for Resource Locking
44

55
== Contributing to lockit
66

Rakefile

+2-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@ Jeweler::Tasks.new do |gem|
1515
gem.name = "lockit"
1616
gem.homepage = "http://github.com/cbeer/lockit"
1717
gem.license = "MIT"
18-
gem.summary = %Q{TODO: one-line summary of your gem}
19-
gem.description = %Q{TODO: longer description of your gem}
20-
gem.email = "[email protected]"
18+
gem.summary = "A ruby implementation of LockIt: A Simple File-based Convention for Resource Locking"
19+
gem.email = "[email protected]"
2120
gem.authors = ["Chris Beer"]
2221
# Include your dependencies below. Runtime dependencies are required when using your gem,
2322
# and development dependencies are only needed for development (ie running rake tasks, tests, etc)

TODO

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- Can a process obtain a lock within a locked file system hierarchy?
2+
3+
- "Once the lock release time has expired, that is, the current time is later than the release time, the lock file is considered stale." .. How should the library distinguish between stale + non-stale lock files? TRUE/FALSE/STALE?

bin/lockit

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/ruby
2+
require 'optparse'
3+
4+
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
5+
require 'lockit'
6+
7+
options = {}
8+
9+
optparse = OptionParser.new do |opts|
10+
opts.banner = "Usage: lockit [options] [dir]"
11+
12+
# This displays the help screen, all programs are
13+
# assumed to have this option.
14+
opts.on( '-h', '--help', 'Display this screen' ) do
15+
puts opts
16+
exit
17+
end
18+
end
19+
20+
optparse.parse!
21+
22+
options[:dir] = ARGV.shift
23+
options[:dir] ||= Dir.pwd
24+
25+
response = LockIt::Dir.new(options[:dir]).lock
26+
27+
exit 1 unless response

bin/unlockit

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/ruby
2+
require 'optparse'
3+
4+
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')
5+
require 'lockit'
6+
7+
options = {}
8+
9+
optparse = OptionParser.new do |opts|
10+
opts.banner = "Usage: unlock [options] [dir]"
11+
12+
# This displays the help screen, all programs are
13+
# assumed to have this option.
14+
opts.on( '-h', '--help', 'Display this screen' ) do
15+
puts opts
16+
exit
17+
end
18+
end
19+
20+
optparse.parse!
21+
22+
options[:dir] = ARGV.shift
23+
options[:dir] ||= Dir.pwd
24+
25+
response = LockIt::Dir.new(options[:dir]).unlock
26+
27+
exit 1 unless response

lib/lockit.rb

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
require 'fileutils'
2+
module LockIt
3+
FILENAME='lock.txt'
4+
5+
module Mixin
6+
def lock args = {}
7+
return false if locked?
8+
write_lock args
9+
self
10+
end
11+
12+
def revise_lock args
13+
return false unless locked?
14+
# xxx is it my lock to revise?
15+
write_lock args
16+
self
17+
end
18+
19+
def try_lock args = {}
20+
return false if locked?
21+
lock args
22+
end
23+
24+
def locked?
25+
return true if closest_lock_file
26+
false
27+
end
28+
29+
def unlock
30+
unlock!
31+
end
32+
33+
def unlock!
34+
FileUtils.rm lock_file
35+
end
36+
37+
def lock_info
38+
f = closest_lock_file
39+
header, obtained, id, release = open(f).read.split("\n").first.split(" ")
40+
info = {}
41+
info[:file] = f
42+
info[:obtained] = obtained
43+
info[:id] = id
44+
info[:release] = release if release
45+
46+
info
47+
end
48+
49+
private
50+
def closest_lock_file
51+
d = []
52+
last = nil
53+
while pwd = File.expand_path(File.join([self.path, d].flatten)) and pwd != last
54+
f = File.join(pwd, LockIt::FILENAME)
55+
return f if File.exists? f
56+
d << '..'
57+
last = pwd
58+
end
59+
return false
60+
end
61+
62+
def lock_file
63+
File.join(self.path, LockIt::FILENAME)
64+
end
65+
def write_lock args
66+
File.open(lock_file, 'w') do |f|
67+
f.write(lock_content(args))
68+
end
69+
end
70+
71+
def lock_content args
72+
s = "Lock: #{Time.now.utc.strftime("%Y-%m-%dT%H:%M:%S+00:00")} #{uniqid}"
73+
s += " #{args[:release].utc.strftime("%Y-%m-%dT%H:%M:%S+00:00")}" if args[:release] and args[:release].respond_to? :utc
74+
end
75+
76+
def uniqid
77+
Process.pid
78+
end
79+
80+
end
81+
82+
83+
class Dir < ::Dir
84+
include LockIt::Mixin
85+
end
86+
end

0 commit comments

Comments
 (0)