Gossamer Forum
Home : Products : DBMan : Customization :

Hit Counter Mod resetting

Quote Reply
Hit Counter Mod resetting
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:

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!
Quote Reply
Re: [Morgan] Hit Counter Mod resetting In reply to
After opening the file for writing in your original code add:

flock(COUNT, 2);

See if that helps.

eg....

open (COUNT, ">$count_up");
flock(COUNT, 2);

Last edited by:

RedRum: Nov 7, 2001, 2:50 PM
Quote Reply
Re: [Morgan] Hit Counter Mod resetting In reply to
I've had the most success using this version of the Hit Counter which has the flock command inserted and a check to see if that option is enabled in your .cfg file.

http://gossamer-threads.com/p/002878
Topic: Updated MOD: Top 10/Record Hit Counter (More Accurate)
TheFew May 06, 2000

The only modification was to add the count routine to:

sub html_topten {

chop $toparray;
foreach my $toplist (split(/,/,$toparray)) {
%rec = (&get_record($toplist));
#### to display the count add these lines ###########
$hcount_up= "$counter_dir/$rec{$db_key}";
open (COUNT, "<$count_up");
$hcountup = <COUNT>;
close COUNT;
######## end display counts ######

print qq|
# <a href="$db_script_link_url&$db_key=$rec{$db_key}&view_records=1&ww=1">$rec{'Title'}</a> |;

You wouldn't have to have the Top 10 page if you choose not to ... you can just use the hit counter.

Hope this helps

Unoffical DBMan FAQ

http://creativecomputingweb.com/dbman/index.shtml/