Igor Gariev (gariev) wrote in changelog,
Igor Gariev
gariev
changelog

[livejournal] r20172: LJSUP-9747: Update admin tools

Committer: gariev
LJSUP-9747: Update admin tools
U   trunk/cgi-bin/LJ/NewWorker.pm
Modified: trunk/cgi-bin/LJ/NewWorker.pm
===================================================================
--- trunk/cgi-bin/LJ/NewWorker.pm	2011-09-29 08:59:27 UTC (rev 20171)
+++ trunk/cgi-bin/LJ/NewWorker.pm	2011-09-29 10:58:21 UTC (rev 20172)
@@ -4,6 +4,7 @@
 
 use Getopt::Long;
 use LJ::Worker::ErrorLog;
+use POSIX;
 
 require 'ljlib.pl';
 require 'ljprotocol.pl';
@@ -100,23 +101,15 @@
     # Check environment
 
     my $ljhome = $ENV{LJHOME} || "/home/lj";
-    return $err->("\$LJHOME not defined") unless $ljhome;
-    return $err->("\$LJHOME not a directory") unless -d $ljhome;
+    die "\$LJHOME not a directory" unless -d $ljhome;
 
     # make sure it's set
     $ENV{LJHOME} = $ljhome;
 
     my $bin  = "$ljhome/bin/worker";
-    return $err->("LJHOME/bin/worker directory doesn't exist") unless -d $bin;
-    return $err->("bogus app name") unless $name =~ /^[\w\-]+(\.[a-z]+)?$/i;
+    die "LJHOME/bin/worker directory doesn't exist" unless -d $bin;
+    die "piddir $piddir doesn't exist" unless -d $piddir;
 
-    return if $self->work_as_script;
-
-    my $rv = eval "use POSIX (); 1;";
-    return $err->("couldn't load POSIX.pm") unless $rv;
-
-    return $err->("piddir $piddir doesn't exist") unless -d $piddir;
-
     # Start
     if ($quantity) {
 
@@ -129,10 +122,11 @@
         umask 0;
 
         ## Fork()s processes, make sure we started and exit parent.
+        
+        $self->pre_fork(); 
 
         my $pid;
-        for(my $count = 0; $count < $quantity; ++$count) {
-            $self->pre_fork(); 
+        for (my $count = 0; $count < $quantity; ++$count) {
             defined($pid = fork) or die "Cannot fork $!";
             if ($pid) { # Parent process.
                 # Wait child started (pid file created), report 'OK' and exit.
@@ -141,12 +135,13 @@
                 while (! (-r $pidfile) ) {
                     sleep 1;
                     unless ($maxwait-- > 0) {
-                        print STDERR "ERROR Processes for $name worker failed to start.\n";
-                        exit 0;
+                        print STDERR "# Worker $name (pid=$pid) failed to start.\n";
+                        exit 1;
                     }
                 }
-                $self->after_fork($pid);
             } else { # Child process.
+                $self->after_fork();
+
                 # Detach ourselves from the terminal
                 die "Cannot detach from controlling terminal"
                     unless POSIX::setsid();
@@ -159,8 +154,6 @@
                 # Catch all signals we needed
                 $self->catch_signals();
 
-                # Catch debug output and redirect it to webnoded or debug udp socket
-                $self->catch_output();
 
                 # Create pid-file
                 $pidfile = "$piddir/$name-$$.pid";
@@ -168,20 +161,27 @@
                 print PID "start time = ", time(), "\n";
                 close(PID);
 
+                # Catch debug output and redirect it to debug udp socket
+                $self->catch_output();
+
                 # And finally go to work.
                 $self->run();
 
                 # Before exit from child, call at_exit() and remove pid file
-                $self->at_exit(); unlink $pidfile;
+                $self->at_exit(); 
+                unlink $pidfile;
 
                 # And exit.
                 exit 0;
             }
         }
-        print STDERR "OK ALL $name workers started.\n"; # Complete loop in parent process.
+        print STDERR "# $quantity worker(s) $name started.\n"; # Complete loop in parent process.
+        exit 0;
     } else {
         # Catch all signals we needed
         $self->catch_signals();
+        $self->pre_fork(); 
+        $self->after_fork();
 
         # Run at console
         $self->run();
@@ -189,11 +189,8 @@
         # Before exit, call at_exit()
         $self->at_exit();
     }
-
-    # Exit:
-    #  - from parent process to daemonize all it's childs;
-    #  - if there was return from run().
     exit 0;
+
 }
 
 ##########################

Tags: gariev, livejournal, pm
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments