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,107 +1,126 | |||
|
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 | |
|
63 | 82 | # Check if I am the last one using the dir. If true, call block. |
|
64 | 83 | |
|
65 | 84 | def teardown |
|
66 | 85 | dir = File.new(@dir_name) |
|
67 | 86 | dir.flock(File::LOCK_EX) |
|
68 | 87 | begin |
|
69 | 88 | counter_filename = get_counter_filename |
|
70 | 89 | if File.exist? counter_filename |
|
71 | 90 | # someone is here |
|
72 | 91 | f = File.new(counter_filename,"r+") |
|
73 | 92 | counter = f.read.to_i |
|
74 | 93 | f.seek(0) |
|
75 | 94 | f.write("#{counter-1}\n") |
|
76 | 95 | f.close |
|
77 | 96 | |
|
78 | 97 | if counter == 1 |
|
79 | 98 | # i'm the last one |
|
80 | 99 | |
|
81 | 100 | File.delete(counter_filename) |
|
82 | 101 | if block_given? |
|
83 | 102 | yield |
|
84 | 103 | end |
|
85 | 104 | end |
|
86 | 105 | else |
|
87 | 106 | # This is BAD |
|
88 | 107 | raise "Error: reference count missing" |
|
89 | 108 | end |
|
90 | 109 | |
|
91 | 110 | rescue |
|
92 | 111 | raise |
|
93 | 112 | |
|
94 | 113 | ensure |
|
95 | 114 | # make sure it unlock the directory |
|
96 | 115 | dir.flock(File::LOCK_UN) |
|
97 | 116 | end |
|
98 | 117 | end |
|
99 | 118 | |
|
100 | 119 | protected |
|
101 | 120 | |
|
102 | 121 | def get_counter_filename |
|
103 | 122 | return File.join(@dir_name,@usage_filename) |
|
104 | 123 | end |
|
105 | 124 | |
|
106 | 125 | end |
|
107 | 126 | end |
You need to be logged in to leave comments.
Login now