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,62 +1,81 | |||
|
1 | 1 | require 'ftools' |
|
2 | 2 | |
|
3 | 3 | # DirInit::Manager handles directory initialization and clean-up when |
|
4 | 4 | # there are many concurrent processes that wants to modify the |
|
5 | 5 | # directory in the same way. |
|
6 | 6 | # |
|
7 | 7 | # An example usage is when each process wants to copy some temporary |
|
8 | 8 | # files to the directory and delete these files after finishing its |
|
9 | 9 | # job. Problems may occur when the first process delete the files |
|
10 | 10 | # while the second process is still using the files. |
|
11 | 11 | # |
|
12 | 12 | # This library maintain a reference counter on the processes using the |
|
13 | 13 | # directory. It locks the dir to manage critical section when |
|
14 | 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 | 35 | module DirInit |
|
17 | 36 | |
|
18 | 37 | class Manager |
|
19 | 38 | |
|
20 | 39 | def initialize(dir_name, usage_filename='.usage_counter') |
|
21 | 40 | @dir_name = dir_name |
|
22 | 41 | @usage_filename = usage_filename |
|
23 | 42 | end |
|
24 | 43 | |
|
25 | 44 | # Check if someone has initialized the dir. If not, call block. |
|
26 | 45 | |
|
27 | 46 | def setup # :yields: block |
|
28 | 47 | dir = File.new(@dir_name) |
|
29 | 48 | dir.flock(File::LOCK_EX) |
|
30 | 49 | begin |
|
31 | 50 | counter_filename = get_counter_filename |
|
32 | 51 | if File.exist? counter_filename |
|
33 | 52 | # someone is here |
|
34 | 53 | f = File.new(counter_filename,"r+") |
|
35 | 54 | counter = f.read.to_i |
|
36 | 55 | f.seek(0) |
|
37 | 56 | f.write("#{counter+1}\n") |
|
38 | 57 | f.close |
|
39 | 58 | else |
|
40 | 59 | # i'm the first, create the counter file |
|
41 | 60 | counter = 0 |
|
42 | 61 | f = File.new(counter_filename,"w") |
|
43 | 62 | f.write("1\n") |
|
44 | 63 | f.close |
|
45 | 64 | end |
|
46 | 65 | |
|
47 | 66 | # if no one is here |
|
48 | 67 | if counter == 0 |
|
49 | 68 | if block_given? |
|
50 | 69 | yield |
|
51 | 70 | end |
|
52 | 71 | end |
|
53 | 72 | |
|
54 | 73 | rescue |
|
55 | 74 | raise |
|
56 | 75 | |
|
57 | 76 | ensure |
|
58 | 77 | # make sure it unlock the directory |
|
59 | 78 | dir.flock(File::LOCK_UN) |
|
60 | 79 | end |
|
61 | 80 | end |
|
62 | 81 |
You need to be logged in to leave comments.
Login now