[LON-CAPA-cvs] cvs: loncom / lonsql

albertel lon-capa-cvs-allow@mail.lon-capa.org
Thu, 12 Apr 2007 00:00:56 -0000


albertel		Wed Apr 11 20:00:56 2007 EDT

  Modified files:              
    /loncom	lonsql 
  Log:
  - switch to using lonnet.pm
  - eliminate unneeded code
  - remove unused packages
  
  
Index: loncom/lonsql
diff -u loncom/lonsql:1.80 loncom/lonsql:1.81
--- loncom/lonsql:1.80	Tue Apr  3 13:51:45 2007
+++ loncom/lonsql	Wed Apr 11 20:00:55 2007
@@ -3,7 +3,7 @@
 # The LearningOnline Network
 # lonsql - LON TCP-MySQL-Server Daemon for handling database requests.
 #
-# $Id: lonsql,v 1.80 2007/04/03 17:51:45 raeburn Exp $
+# $Id: lonsql,v 1.81 2007/04/12 00:00:55 albertel Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -105,20 +105,16 @@
 use LONCAPA;
 use LONCAPA::Configuration;
 use LONCAPA::lonmetadata();
+use Apache::lonnet;
 
 use IO::Socket;
 use Symbol;
 use POSIX;
 use IO::Select;
-use IO::File;
-use Socket;
-use Fcntl;
-use Tie::RefHash;
 use DBI;
 use File::Find;
 use localenroll;
 use GDBM_File;
-use Storable qw(thaw);
 
 ########################################################
 ########################################################
@@ -206,8 +202,7 @@
 #
 # Read loncapa_apache.conf and loncapa.conf
 #
-my $perlvarref=LONCAPA::Configuration::read_conf('loncapa.conf');
-my %perlvar=%{$perlvarref};
+my %perlvar=%{&LONCAPA::Configuration::read_conf('loncapa.conf')};
 #
 # Write the /home/www/.my.cnf file 
 my $conf_file = '/home/www/.my.cnf';
@@ -253,27 +248,13 @@
 #
 my $pidfile="$perlvar{'lonDaemons'}/logs/lonsql.pid";
 if (-e $pidfile) {
-   my $lfh=IO::File->new("$pidfile");
+   open(my $lfh,"$pidfile");
    my $pide=<$lfh>;
    chomp($pide);
    if (kill 0 => $pide) { die "already running"; }
 }
 
-#
-# Read hosts file
-#
-my $thisserver;
-my %hostname;
 my $PREFORK=4; # number of children to maintain, at least four spare
-open (CONFIG,"$perlvar{'lonTabDir'}/hosts.tab") || die "Can't read host file";
-while (my $configline=<CONFIG>) {
-    my ($id,$domain,$role,$name)=split(/:/,$configline);
-    $name=~s/\s//g;
-    $thisserver=$name if ($id eq $perlvar{'lonHostID'});
-    $hostname{$id}=$name;
-    #$PREFORK++;
-}
-close(CONFIG);
 #
 #$PREFORK=int($PREFORK/4);
 
@@ -399,13 +380,13 @@
 	    my $query=unescape($query);
             #
             #send query id which is pid_unixdatetime_runningcounter
-	    my $queryid = $thisserver;
+	    my $queryid = &Apache::lonnet::hostname($perlvar{'lonHostID'});
 	    $queryid .="_".($$)."_";
 	    $queryid .= time."_";
 	    $queryid .= $run;
 	    print $client "$queryid\n";
 	    #
-	    # &logthis("QUERY: $query - $arg1 - $arg2 - $arg3");
+	    # &logthis("QUERY: $query - $arg1 - $arg2 - $arg3 - $queryid");
 	    sleep 1;
             #
             my $result='';
@@ -485,7 +466,7 @@
             # result does not need to be escaped because it has already been
             # escaped.
             #$result=&escape($result);
-            &reply("queryreply:$queryid:$result",$conserver);
+            &Apache::lonnet::reply("queryreply:$queryid:$result",$conserver);
         }
         # tidy up gracefully and finish
         #
@@ -602,7 +583,7 @@
     my $customresult='';
     my @results;
     foreach my $metafile (@metalist) {
-        my $fh=IO::File->new($metafile);
+        open(my $fh,$metafile);
         my @lines=<$fh>;
         my $stuff=join('',@lines);
         if ($stuff=~/$custom/s) {
@@ -737,7 +718,7 @@
         while (my ($key,$value) = each(%$hashref)) {
             $key = &unescape($key);
             next if ($key =~ /^error: 2 /);
-            $curr_perms{$key}=&thaw_unescape($value);
+            $curr_perms{$key}=&Apache::lonnet::thaw_unescape($value);
         }
         if (!&untie_user_hash($hashref)) {
             &logthis("error: ".($!+0)." untie (GDBM) Failed");
@@ -755,16 +736,6 @@
     return %access;
 }
 
-sub thaw_unescape {
-    my ($value)=@_;
-    if ($value =~ /^__FROZEN__/) {
-        substr($value,0,10,undef);
-        $value=&unescape($value);
-        return &thaw($value);
-    }
-    return &unescape($value);
-}
-
 ###########################################
 sub check_table {
     my ($table_id) = @_;
@@ -821,78 +792,12 @@
 sub logthis {
     my $message=shift;
     my $execdir=$perlvar{'lonDaemons'};
-    my $fh=IO::File->new(">>$execdir/logs/lonsql.log");
+    open(my $fh,">>$execdir/logs/lonsql.log");
     my $now=time;
     my $local=localtime($now);
     print $fh "$local ($$): $message\n";
 }
 
-# -------------------------------------------------- Non-critical communication
-
-########################################################
-########################################################
-
-=pod
-
-=item &subreply
-
-Sends a command to a server.  Called only by &reply.
-
-Inputs: $cmd,$server
-
-Returns: The results of the message or 'con_lost' on error.
-
-=cut
-
-########################################################
-########################################################
-sub subreply {
-    my ($cmd,$server)=@_;
-    my $peerfile="$perlvar{'lonSockDir'}/".$hostname{$server};
-    my $sclient=IO::Socket::UNIX->new(Peer    =>"$peerfile",
-                                      Type    => SOCK_STREAM,
-                                      Timeout => 10)
-       or return "con_lost";
-    print $sclient "sethost:$server:$cmd\n";
-    my $answer=<$sclient>;
-    chomp($answer);
-    $answer="con_lost" if (!$answer);
-    return $answer;
-}
-
-########################################################
-########################################################
-
-=pod
-
-=item &reply
-
-Sends a command to a server.
-
-Inputs: $cmd,$server
-
-Returns: The results of the message or 'con_lost' on error.
-
-=cut
-
-########################################################
-########################################################
-sub reply {
-  my ($cmd,$server)=@_;
-  my $answer;
-  if ($server ne $perlvar{'lonHostID'}) { 
-    $answer=subreply($cmd,$server);
-    if ($answer eq 'con_lost') {
-	$answer=subreply("ping",$server);
-        $answer=subreply($cmd,$server);
-    }
-  } else {
-    $answer='self_reply';
-    $answer=subreply($cmd,$server);
-  } 
-  return $answer;
-}
-
 ########################################################
 ########################################################
 
@@ -928,31 +833,6 @@
 
 =pod
 
-=item &propath
-
-Inputs: user name, user domain
-
-Returns: The full path to the users directory.
-
-=cut
-
-########################################################
-########################################################
-sub propath {
-    my ($udom,$uname)=@_;
-    $udom=~s/\W//g;
-    $uname=~s/\W//g;
-    my $subdir=$uname.'__';
-    $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
-    my $proname="$perlvar{'lonUsersDir'}/$udom/$subdir/$uname";
-    return $proname;
-} 
-
-########################################################
-########################################################
-
-=pod
-
 =item &courselog
 
 Inputs: $path, $command