I have implemented a hit counter mod in my script and everything went smoothly for a month or two. The counter was at 10,000 plus when it suddenly reset. There are actually two separate counters, the second counters value is much less and has not had a problem. The counter code is executed in html_record_form_basic with the following code:
$count_up= "/path/";
open (COUNT, "<$count_up");
$countup = <COUNT>;
close COUNT;
open (COUNT, ">$count_up");
$countup2 = $countup + 1;
print COUNT $countup2;
close COUNT;
}
elsif ($db_uid =~ "faculty") {
$count_up= "/path/";
open (COUNT, "<$count_up");
$countup = <COUNT>;
close COUNT;
open (COUNT, ">$count_up");
$countup2 = $countup + 1;
print COUNT $countup2;
close COUNT;
}
After some discussion with a coworker, he suggested that maybe this happened as a result of two users updating the code simultaneously. He suggested the following solution:
while($i++ < 5) {
if(link($countup,"$countup.lock") == 0) {
sleep 1;
}
else {
&add1;
unlink "$countup.lock";
last;
}
}
sub add1 {
open (COUNT, "<$count_up");
$countup = <COUNT>;
close COUNT;
open (COUNT, ">$count_up");
$countup2 = $countup + 1;
print COUNT $countup2;
close COUNT;
}
"This will try to set a link on the counter file called the file name with .lock on the end. If it is not successful there is another process updating the file so sleep 1 sec and try again. If it is unsuccessful after 5 trys then forget it. If it is successful then rewrite the file with one more and clear the lock. "
However, when I implemented the code, the counter stopped working altogether. It also slowed the DB down quite a bit.
Any comments, suggestions, anything?
Thanks!
Code:
if ($db_uid =~ "staff") { $count_up= "/path/";
open (COUNT, "<$count_up");
$countup = <COUNT>;
close COUNT;
open (COUNT, ">$count_up");
$countup2 = $countup + 1;
print COUNT $countup2;
close COUNT;
}
elsif ($db_uid =~ "faculty") {
$count_up= "/path/";
open (COUNT, "<$count_up");
$countup = <COUNT>;
close COUNT;
open (COUNT, ">$count_up");
$countup2 = $countup + 1;
print COUNT $countup2;
close COUNT;
}
After some discussion with a coworker, he suggested that maybe this happened as a result of two users updating the code simultaneously. He suggested the following solution:
Code:
$i=0; while($i++ < 5) {
if(link($countup,"$countup.lock") == 0) {
sleep 1;
}
else {
&add1;
unlink "$countup.lock";
last;
}
}
sub add1 {
open (COUNT, "<$count_up");
$countup = <COUNT>;
close COUNT;
open (COUNT, ">$count_up");
$countup2 = $countup + 1;
print COUNT $countup2;
close COUNT;
}
"This will try to set a link on the counter file called the file name with .lock on the end. If it is not successful there is another process updating the file so sleep 1 sec and try again. If it is unsuccessful after 5 trys then forget it. If it is successful then rewrite the file with one more and clear the lock. "
However, when I implemented the code, the counter stopped working altogether. It also slowed the DB down quite a bit.
Any comments, suggestions, anything?
Thanks!