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

[livejournal] r16495: Bugfix/temporary hack for spawning bin/w...

Committer: gariev
Bugfix/temporary hack for spawning bin/worker/esn-cluster-subs processes
U   branches/r63.4/cgi-bin/LJ/ESN.pm
Modified: branches/r63.4/cgi-bin/LJ/ESN.pm
===================================================================
--- branches/r63.4/cgi-bin/LJ/ESN.pm	2010-04-02 08:01:12 UTC (rev 16494)
+++ branches/r63.4/cgi-bin/LJ/ESN.pm	2010-04-02 16:36:10 UTC (rev 16495)
@@ -240,12 +240,44 @@
 
     my $pid = fork;
     
-    if ($pid) {
-        my $status = waitpid($pid, 0);
-        $job->did_something($status);
+    ##
+    ## list of exit codes of child processes:
+    ## 0 - job didn't do the work (did_something==0)
+    ## 1 - did_something>0 (it's boolean value, actually)
+    ## 2 - exception was thrown, reason and message are lost :(
+    ##
+    ## This is a hack, the more solid solution will be later. 
+    ## 
+    if (!defined $pid) {
+        die "fork failed: $!";
+    } elsif ($pid) {
+        
+        ## Must use default CHLD handler, otherwise, waitpid will return -1
+        ## and no exit status of child process can be collected
+        local $SIG{CHLD}; 
+        
+        my $wpid = waitpid($pid, 0);
+        if ($wpid!=$pid) {
+            die "Something strange: waitpid($pid,0) returned $wpid";
+        }
+        my $status = $? >> 8;
+        if ($status==0 || $status==1) {
+            return $job->did_something($status);
+        } elsif ($status==2) {
+            die "Job died";
+        } else {
+            die "Job did something strange";
+        }
     } else {
-        $class->do_work($job);
-        exit $job->did_something;
+        eval {
+            $class->do_work($job);
+        };
+        if ($@) {
+            warn $@;
+            exit 2;
+        } else {
+            exit( $job->did_something ? 1 : 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