Description:
added more comments on dir_init.rb
git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@383 6386c4cd-e34a-4fa8-8920-d93eb39b512e
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r180:a8ad25e1c3fc - - 1 file changed: 19 inserted, 0 deleted
@@ -1,38 +1,57 | |||||
|
1 | require 'ftools' |
|
1 | require 'ftools' |
|
2 |
|
2 | ||
|
3 | # DirInit::Manager handles directory initialization and clean-up when |
|
3 | # DirInit::Manager handles directory initialization and clean-up when |
|
4 | # there are many concurrent processes that wants to modify the |
|
4 | # there are many concurrent processes that wants to modify the |
|
5 | # directory in the same way. |
|
5 | # directory in the same way. |
|
6 | # |
|
6 | # |
|
7 | # An example usage is when each process wants to copy some temporary |
|
7 | # An example usage is when each process wants to copy some temporary |
|
8 | # files to the directory and delete these files after finishing its |
|
8 | # files to the directory and delete these files after finishing its |
|
9 | # job. Problems may occur when the first process delete the files |
|
9 | # job. Problems may occur when the first process delete the files |
|
10 | # while the second process is still using the files. |
|
10 | # while the second process is still using the files. |
|
11 | # |
|
11 | # |
|
12 | # This library maintain a reference counter on the processes using the |
|
12 | # This library maintain a reference counter on the processes using the |
|
13 | # directory. It locks the dir to manage critical section when |
|
13 | # directory. It locks the dir to manage critical section when |
|
14 | # updating the reference counter. |
|
14 | # updating the reference counter. |
|
|
15 | + # | ||
|
|
16 | + # Example usage: | ||
|
|
17 | + # | ||
|
|
18 | + # dman = DirInit::Manager.new("mydir") | ||
|
|
19 | + # | ||
|
|
20 | + # dman.setup do | ||
|
|
21 | + # # do some initialization | ||
|
|
22 | + # end | ||
|
|
23 | + # | ||
|
|
24 | + # #... do anything you want | ||
|
|
25 | + # | ||
|
|
26 | + # dman.teardown do | ||
|
|
27 | + # # clean up | ||
|
|
28 | + # end | ||
|
|
29 | + # | ||
|
|
30 | + # DirInit::Manager ensures that the block passed to <tt>setup</tt> | ||
|
|
31 | + # only runs once by the first process in the concurrent dir usage and | ||
|
|
32 | + # block passed to <tt>teardown</tt> runs once by the last process in | ||
|
|
33 | + # that concurrent activities leaving that dir. | ||
|
15 |
|
34 | ||
|
16 | module DirInit |
|
35 | module DirInit |
|
17 |
|
36 | ||
|
18 | class Manager |
|
37 | class Manager |
|
19 |
|
38 | ||
|
20 | def initialize(dir_name, usage_filename='.usage_counter') |
|
39 | def initialize(dir_name, usage_filename='.usage_counter') |
|
21 | @dir_name = dir_name |
|
40 | @dir_name = dir_name |
|
22 | @usage_filename = usage_filename |
|
41 | @usage_filename = usage_filename |
|
23 | end |
|
42 | end |
|
24 |
|
43 | ||
|
25 | # Check if someone has initialized the dir. If not, call block. |
|
44 | # Check if someone has initialized the dir. If not, call block. |
|
26 |
|
45 | ||
|
27 | def setup # :yields: block |
|
46 | def setup # :yields: block |
|
28 | dir = File.new(@dir_name) |
|
47 | dir = File.new(@dir_name) |
|
29 | dir.flock(File::LOCK_EX) |
|
48 | dir.flock(File::LOCK_EX) |
|
30 | begin |
|
49 | begin |
|
31 | counter_filename = get_counter_filename |
|
50 | counter_filename = get_counter_filename |
|
32 | if File.exist? counter_filename |
|
51 | if File.exist? counter_filename |
|
33 | # someone is here |
|
52 | # someone is here |
|
34 | f = File.new(counter_filename,"r+") |
|
53 | f = File.new(counter_filename,"r+") |
|
35 | counter = f.read.to_i |
|
54 | counter = f.read.to_i |
|
36 | f.seek(0) |
|
55 | f.seek(0) |
|
37 | f.write("#{counter+1}\n") |
|
56 | f.write("#{counter+1}\n") |
|
38 | f.close |
|
57 | f.close |
You need to be logged in to leave comments.
Login now