[LON-CAPA-cvs] cvs: loncom /debugging_tools db_copy.pl

albertel lon-capa-cvs@mail.lon-capa.org
Fri, 13 Oct 2006 18:34:07 -0000


albertel		Fri Oct 13 14:34:07 2006 EDT

  Modified files:              
    /loncom/debugging_tools	db_copy.pl 
  Log:
  - locking works
  - better info about how long it'll take
  
  
Index: loncom/debugging_tools/db_copy.pl
diff -u loncom/debugging_tools/db_copy.pl:1.3 loncom/debugging_tools/db_copy.pl:1.4
--- loncom/debugging_tools/db_copy.pl:1.3	Tue Oct  3 14:01:01 2006
+++ loncom/debugging_tools/db_copy.pl	Fri Oct 13 14:34:07 2006
@@ -19,57 +19,73 @@
 
 my  %perlvar=%{&LONCAPA::Configuration::read_conf('loncapa.conf')};
 
+my $do_locks = 1;
 {
-    my $straight;
     sub lock_db {
-	my ($fname) = @_;
-	my $dbref;
-	$fname = &Cwd::abs_path($fname);
-	if ($fname =~ m/^\Q$perlvar{'lonUsersDir'}\E/) {
-	    $dbref=&LONCAPA::locking_hash_tie($fname,&GDBM_READER());
-	    $straight=0;
-	} else {
-	    if (tie(my %db,'GDBM_File',$fname,&GDBM_READER(),0640)) {
-		$dbref = \%db;
-	    }
-	    $straight=1;
-	}
-	return $dbref;
+        my ($fname) = @_;
+        my $sym;
+        if ($do_locks) {
+            open($sym,">>$fname.lock");
+            flock($sym,(LOCK_EX));
+        }
+        return $sym;
     }
 
     sub unlock_db {
-	my ($dbref) = @_;
-	if ($straight) {
-	    untie($dbref);
-	} else {
-	    &LONCAPA::locking_hash_untie($dbref);
-	}
+        my ($sym) = @_;
+        if (ref($sym)) {
+            flock($sym,(LOCK_UN));
+        }
     }
 }
 
+my $count=0;
+sub count_db {
+    return if ($_!~m/\.db$/);
+    $count++;
+}
+
+my $done=0;
+my $starttime;
+my $last_100_start_time;
 sub process_db {
     return if ($_!~m/\.db$/);
-    my $file = $_;
-    my $dbref =&lock_db($file);
-    print("attempting $file\n");
-    my %newdb;
-    my $new_file = $file.'.new';
-    system("$dump_db -f $file|$create_db -f $new_file");
+    if (!-e "$_.old") {
+	my $file = $_;
+	my $dbref =&lock_db($file);
+	#print("attempting $file\n");
+	system("$dump_db -f $file|$create_db -f $file.new");
 #    print("finishing $dbref\n");
-    untie($dbref);
-    system("/bin/mv $file $file.old");
-    system("/bin/mv $file.new $file");
-    &unlock_db($dbref);
+	rename($file,"$file.old");
+	rename("$file.new","$file");
+	&unlock_db($dbref);
+    }
+    $done++;
+    if (!($done %100)) {
+        print("$_\n");
+        my $took = time()-$starttime;
+        my $togo = int(($took/$done) * ($count-$done));
+        my $total = $togo+$took;
+        my $per = $took/$done;
+        my $last_per = (time()-$last_100_start_time)/100;
+        printf("%6d in %6d, togo %6d, overall %6d, %.4f (for one), %.4f)\n",
+	       $done,$took,$togo,$total,$per,$last_per);
+        $last_100_start_time = time();
+    }
 }
 
 sub main {
-    find(
-	 {
-	     no_chdir   => 1,
-	     wanted     => \&process_db,
-	 }, 
-	 $perlvar{'lonUsersDir'}
-	 );
+    my $dir = $perlvar{'lonUsersDir'}.'/temp/y/';
+    print("Doing $dir\n");
+    &find({ no_chdir   => 1,
+            wanted     => \&count_db, },
+          $dir);
+    print("Found $count db to do\n");
+    $last_100_start_time = $starttime = time();
+    &find({ no_chdir   => 1,
+            wanted     => \&process_db, },
+          $dir);
+
 }
 
 &main();