File:  [LON-CAPA] / loncom / lonsql
Revision 1.30: download - view: text, annotated - select for diffs
Mon Apr 2 20:16:31 2001 UTC (23 years, 1 month ago) by harris41
Branches: MAIN
CVS tags: HEAD
ok.. putting in safeguard to make sure lonsql NEVER starts when
the database is down -Scott

    1: #!/usr/bin/perl
    2: # lonsql-based on the preforker:harsha jagasia:date:5/10/00
    3: # 7/25 Gerd Kortemeyer
    4: # many different dates Scott Harrison
    5: # 03/22/2001 Scott Harrison
    6: use IO::Socket;
    7: use Symbol;
    8: use POSIX;
    9: use IO::Select;
   10: use IO::File;
   11: use Socket;
   12: use Fcntl;
   13: use Tie::RefHash;
   14: use DBI;
   15: 
   16: my @metalist;
   17: # ----------------- Code to enable 'find' subroutine listing of the .meta files
   18: require "find.pl";
   19: sub wanted {
   20:     (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
   21:     -f _ &&
   22:     /^.*\.meta$/ &&
   23:     push(@metalist,"$dir/$_");
   24: }
   25: 
   26: {
   27:     my $dbh;
   28:     unless (
   29: 	    $dbh = DBI->connect("DBI:mysql:loncapa","www","123",{ RaiseError =>0,PrintError=>0})
   30: 	    ) { 
   31: 	print "Cannot connect to database!\n";
   32: 	exit;
   33:     }
   34:     else {
   35: 	$dbh->disconnect;
   36:     }
   37: }
   38: $childmaxattempts=10;
   39: $run =0;#running counter to generate the query-id
   40: 
   41: # ------------------------------------ Read httpd access.conf and get variables
   42: open (CONFIG,"/etc/httpd/conf/access.conf") || die "Can't read access.conf";
   43: 
   44: while ($configline=<CONFIG>) {
   45:     if ($configline =~ /PerlSetVar/) {
   46: 	my ($dummy,$varname,$varvalue)=split(/\s+/,$configline);
   47:         chomp($varvalue);
   48:         $perlvar{$varname}=$varvalue;
   49:     }
   50: }
   51: close(CONFIG);
   52: 
   53: # --------------------------------------------- Check if other instance running
   54: 
   55: my $pidfile="$perlvar{'lonDaemons'}/logs/lonsql.pid";
   56: 
   57: if (-e $pidfile) {
   58:    my $lfh=IO::File->new("$pidfile");
   59:    my $pide=<$lfh>;
   60:    chomp($pide);
   61:    if (kill 0 => $pide) { die "already running"; }
   62: }
   63: 
   64: # ------------------------------------------------------------- Read hosts file
   65: $PREFORK=4; # number of children to maintain, at least four spare
   66: 
   67: open (CONFIG,"$perlvar{'lonTabDir'}/hosts.tab") || die "Can't read host file";
   68: 
   69: while ($configline=<CONFIG>) {
   70:     my ($id,$domain,$role,$name,$ip)=split(/:/,$configline);
   71:     chomp($ip);
   72: 
   73:     $hostip{$ip}=$id;
   74: 
   75:     if ($id eq $perlvar{'lonHostID'}) { $thisserver=$name; }
   76: 
   77:     $PREFORK++;
   78: }
   79: close(CONFIG);
   80: 
   81: $unixsock = "mysqlsock";
   82: my $localfile="$perlvar{'lonSockDir'}/$unixsock";
   83: my $server;
   84: unlink ($localfile);
   85: unless ($server=IO::Socket::UNIX->new(Local    =>"$localfile",
   86: 				  Type    => SOCK_STREAM,
   87: 				  Listen => 10))
   88: {
   89:     print "in socket error:$@\n";
   90: }
   91: 
   92: # -------------------------------------------------------- Routines for forking
   93: # global variables
   94: $MAX_CLIENTS_PER_CHILD  = 5;        # number of clients each child should process
   95: %children               = ();       # keys are current child process IDs
   96: $children               = 0;        # current number of children
   97: 
   98: sub REAPER {                        # takes care of dead children
   99:     $SIG{CHLD} = \&REAPER;
  100:     my $pid = wait;
  101:     $children --;
  102:     &logthis("Child $pid died");
  103:     delete $children{$pid};
  104: }
  105: 
  106: sub HUNTSMAN {                      # signal handler for SIGINT
  107:     local($SIG{CHLD}) = 'IGNORE';   # we're going to kill our children
  108:     kill 'INT' => keys %children;
  109:     my $execdir=$perlvar{'lonDaemons'};
  110:     unlink("$execdir/logs/lonsql.pid");
  111:     &logthis("<font color=red>CRITICAL: Shutting down</font>");
  112:     $unixsock = "mysqlsock";
  113:     my $port="$perlvar{'lonSockDir'}/$unixsock";
  114:     unlink(port);
  115:     exit;                           # clean up with dignity
  116: }
  117: 
  118: sub HUPSMAN {                      # signal handler for SIGHUP
  119:     local($SIG{CHLD}) = 'IGNORE';  # we're going to kill our children
  120:     kill 'INT' => keys %children;
  121:     close($server);                # free up socket
  122:     &logthis("<font color=red>CRITICAL: Restarting</font>");
  123:     my $execdir=$perlvar{'lonDaemons'};
  124:     $unixsock = "mysqlsock";
  125:     my $port="$perlvar{'lonSockDir'}/$unixsock";
  126:     unlink(port);
  127:     exec("$execdir/lonsql");         # here we go again
  128: }
  129: 
  130: sub logthis {
  131:     my $message=shift;
  132:     my $execdir=$perlvar{'lonDaemons'};
  133:     my $fh=IO::File->new(">>$execdir/logs/lonsqlfinal.log");
  134:     my $now=time;
  135:     my $local=localtime($now);
  136:     print $fh "$local ($$): $message\n";
  137: }
  138: # ---------------------------------------------------- Fork once and dissociate
  139: $fpid=fork;
  140: exit if $fpid;
  141: die "Couldn't fork: $!" unless defined ($fpid);
  142: 
  143: POSIX::setsid() or die "Can't start new session: $!";
  144: 
  145: # ------------------------------------------------------- Write our PID on disk
  146: 
  147: $execdir=$perlvar{'lonDaemons'};
  148: open (PIDSAVE,">$execdir/logs/lonsql.pid");
  149: print PIDSAVE "$$\n";
  150: close(PIDSAVE);
  151: &logthis("<font color=red>CRITICAL: ---------- Starting ----------</font>");
  152: 
  153: # ----------------------------- Ignore signals generated during initial startup
  154: $SIG{HUP}=$SIG{USR1}='IGNORE';
  155: # ------------------------------------------------------- Now we are on our own    
  156: # Fork off our children.
  157: for (1 .. $PREFORK) {
  158:     make_new_child();
  159: }
  160: 
  161: # Install signal handlers.
  162: $SIG{CHLD} = \&REAPER;
  163: $SIG{INT}  = $SIG{TERM} = \&HUNTSMAN;
  164: $SIG{HUP}  = \&HUPSMAN;
  165: 
  166: # And maintain the population.
  167: while (1) {
  168:     sleep;                          # wait for a signal (i.e., child's death)
  169:     for ($i = $children; $i < $PREFORK; $i++) {
  170:         make_new_child();           # top up the child pool
  171:     }
  172: }
  173: 
  174: 
  175: sub make_new_child {
  176:     my $pid;
  177:     my $sigset;
  178:     
  179:     # block signal for fork
  180:     $sigset = POSIX::SigSet->new(SIGINT);
  181:     sigprocmask(SIG_BLOCK, $sigset)
  182:         or die "Can't block SIGINT for fork: $!\n";
  183:     
  184:     die "fork: $!" unless defined ($pid = fork);
  185:     
  186:     if ($pid) {
  187:         # Parent records the child's birth and returns.
  188:         sigprocmask(SIG_UNBLOCK, $sigset)
  189:             or die "Can't unblock SIGINT for fork: $!\n";
  190:         $children{$pid} = 1;
  191:         $children++;
  192:         return;
  193:     } else {
  194:         # Child can *not* return from this subroutine.
  195:         $SIG{INT} = 'DEFAULT';      # make SIGINT kill us as it did before
  196:     
  197:         # unblock signals
  198:         sigprocmask(SIG_UNBLOCK, $sigset)
  199:             or die "Can't unblock SIGINT for fork: $!\n";
  200: 	
  201: 	
  202:         #open database handle
  203: 	# making dbh global to avoid garbage collector
  204: 	unless (
  205: 		$dbh = DBI->connect("DBI:mysql:loncapa","www","123",{ RaiseError =>0,PrintError=>0})
  206: 		) { 
  207:   	            sleep(10+int(rand(20)));
  208: 		    &logthis("<font color=blue>WARNING: Couldn't connect to database  ($st secs): $@</font>");
  209: 		    print "database handle error\n";
  210: 		    exit;
  211: 
  212: 	  };
  213: 	# make sure that a database disconnection occurs with ending kill signals
  214: 	$SIG{TERM}=$SIG{INT}=$SIG{QUIT}=$SIG{__DIE__}=\&DISCONNECT;
  215: 
  216:         # handle connections until we've reached $MAX_CLIENTS_PER_CHILD
  217:         for ($i=0; $i < $MAX_CLIENTS_PER_CHILD; $i++) {
  218:             $client = $server->accept()     or last;
  219:             
  220:             # do something with the connection
  221: 	    $run = $run+1;
  222: 	    my $userinput = <$client>;
  223: 	    chomp($userinput);
  224: 	    	    
  225: 	    my ($conserver,$querytmp,
  226: 		$customtmp,$customshowtmp)=split(/&/,$userinput);
  227: 	    my $query=unescape($querytmp);
  228: 	    my $custom=unescape($customtmp);
  229: 	    my $customshow=unescape($customshowtmp);
  230: 
  231:             #send query id which is pid_unixdatetime_runningcounter
  232: 	    $queryid = $thisserver;
  233: 	    $queryid .="_".($$)."_";
  234: 	    $queryid .= time."_";
  235: 	    $queryid .= $run;
  236: 	    print $client "$queryid\n";
  237: 	    
  238: 	    &logthis("QUERY: $query");
  239: 	    &logthis("QUERY: $query");
  240: 	    sleep 1;
  241:             #prepare and execute the query
  242: 	    my $sth = $dbh->prepare($query);
  243: 	    my $result;
  244: 	    my @files;
  245: 	    my $subsetflag=0;
  246: 	    if ($query) {
  247: 		unless ($sth->execute())
  248: 		{
  249: 		    &logthis("<font color=blue>WARNING: Could not retrieve from database: $@</font>");
  250: 		    $result="";
  251: 		}
  252: 		else {
  253: 		    my $r1=$sth->fetchall_arrayref;
  254: 		    my @r2;
  255: 		    map {my $a=$_; 
  256: 			 my @b=map {escape($_)} @$a;
  257: 			 push @files,@{$a}[3];
  258: 			 push @r2,join(",", @b)
  259: 			 } (@$r1);
  260: 		    $result=join("&",@r2);
  261: 		}
  262: 	    }
  263: 	    # do custom metadata searching here and build into result
  264: 	    if ($custom or $customshow) {
  265: 		&logthis("am going to do custom query for $custom");
  266: 		if ($query) {
  267: 		    @metalist=map {$perlvar{'lonDocRoot'}.$_.'.meta'} @files;
  268: 		}
  269: 		else {
  270: 		    @metalist=(); pop @metalist;
  271: 		    &find("$perlvar{'lonDocRoot'}/res");
  272: 		}
  273: #		&logthis("FILELIST:" . join(":::",@metalist));
  274: 		# if file is indicated in sql database and
  275: 		# not part of sql-relevant query, do not pattern match.
  276: 		# if file is not in sql database, output error.
  277: 		# if file is indicated in sql database and is
  278: 		# part of query result list, then do the pattern match.
  279: 		my $customresult='';
  280: 		my @r2;
  281: 		foreach my $m (@metalist) {
  282: 		    my $fh=IO::File->new($m);
  283: 		    my @lines=<$fh>;
  284: 		    my $stuff=join('',@lines);
  285: 		    if ($stuff=~/$custom/s) {
  286: 			foreach my $f ('abstract','author','copyright',
  287: 				       'creationdate','keywords','language',
  288: 				       'lastrevisiondate','mime','notes',
  289: 				       'owner','subject','title') {
  290: 			    $stuff=~s/\n?\<$f[^\>]*\>.*?<\/$f[^\>]*\>\n?//;
  291: 			}
  292: 			my $m2=$m; my $docroot=$perlvar{'lonDocRoot'};
  293: 			$m2=~s/^$docroot//;
  294: 			$m2=~s/\.meta$//;
  295: 			unless ($query) {
  296: 			    my $q2="select * from metadata where url like '$m2'";
  297: 			    my $sth = $dbh->prepare($q2);
  298: 			    $sth->execute();
  299: 			    my $r1=$sth->fetchall_arrayref;
  300: 			    map {my $a=$_; 
  301: 				 my @b=map {escape($_)} @$a;
  302: 				 push @files,@{$a}[3];
  303: 				 push @r2,join(",", @b)
  304: 				 } (@$r1);
  305: 			}
  306: #			&logthis("found: $stuff");
  307: 			$customresult.='&custom='.escape($m2).','.escape($stuff);
  308: 		    }
  309: 		}
  310: 		$result=join("&",@r2) unless $query;
  311: 		$result.=$customresult;
  312: 	    }
  313: 	    # reply with result
  314: 	    $result.="\n" if $result;
  315:             &reply("queryreply:$queryid:$result",$conserver);
  316: 
  317:         }
  318:     
  319:         # tidy up gracefully and finish
  320: 	
  321:         #close the database handle
  322: 	$dbh->disconnect
  323: 	   or &logthis("<font color=blue>WARNING: Couldn't disconnect from database  $DBI::errstr ($st secs): $@</font>");
  324:     
  325:         # this exit is VERY important, otherwise the child will become
  326:         # a producer of more and more children, forking yourself into
  327:         # process death.
  328:         exit;
  329:     }
  330: }
  331: 
  332: sub DISCONNECT {
  333:     $dbh->disconnect or 
  334:     &logthis("<font color=blue>WARNING: Couldn't disconnect from database  $DBI::errstr ($st secs): $@</font>");
  335:     exit;
  336: }
  337: 
  338: # -------------------------------------------------- Non-critical communication
  339: 
  340: sub subreply {
  341:     my ($cmd,$server)=@_;
  342:     my $peerfile="$perlvar{'lonSockDir'}/$server";
  343:     my $sclient=IO::Socket::UNIX->new(Peer    =>"$peerfile",
  344:                                       Type    => SOCK_STREAM,
  345:                                       Timeout => 10)
  346:        or return "con_lost";
  347:     print $sclient "$cmd\n";
  348:     my $answer=<$sclient>;
  349:     chomp($answer);
  350:     if (!$answer) { $answer="con_lost"; }
  351:     return $answer;
  352: }
  353: 
  354: sub reply {
  355:   my ($cmd,$server)=@_;
  356:   my $answer;
  357:   if ($server ne $perlvar{'lonHostID'}) { 
  358:     $answer=subreply($cmd,$server);
  359:     if ($answer eq 'con_lost') {
  360: 	$answer=subreply("ping",$server);
  361:         $answer=subreply($cmd,$server);
  362:     }
  363:   } else {
  364:     $answer='self_reply';
  365:   } 
  366:   return $answer;
  367: }
  368: 
  369: # -------------------------------------------------------- Escape Special Chars
  370: 
  371: sub escape {
  372:     my $str=shift;
  373:     $str =~ s/(\W)/"%".unpack('H2',$1)/eg;
  374:     return $str;
  375: }
  376: 
  377: # ----------------------------------------------------- Un-Escape Special Chars
  378: 
  379: sub unescape {
  380:     my $str=shift;
  381:     $str =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  382:     return $str;
  383: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>