Fri May 01 10:25:26 2009: Request 45622 was acted upon.
Transaction: Ticket created by yulmathieu
       Queue: Win32-Job
     Subject: Patch for running job in the background
   Broken in: (no value)
    Severity: Normal
       Owner: Nobody
  Requestors: math...@closetwork.org
      Status: new
 Ticket <URL: https://rt.cpan.org/Ticket/Display.html?id=45622 >


Here's a patch that adds the start() method, so you can start the jobs,
without waiting for them, like run() or watch(). It's useful with a
library like AnyEvent. 

Also added: 
- is_running() -> check if any of the processes are still running
- Constants STILL_ACTIVE_EXITCODE and KILLED_EXITCODE for comparing with
the exitcode returned by status.

Index: C:/Documents and Settings/mlongtin/My
Documents/dbpro/svn/branches/newscheduler/extralibs/Win32-Job/Job.xs
===================================================================
--- C:/Documents and Settings/mlongtin/My
Documents/dbpro/svn/branches/newscheduler/extralibs/Win32-Job/Job.xs
(revision 470)
+++ C:/Documents and Settings/mlongtin/My
Documents/dbpro/svn/branches/newscheduler/extralibs/Win32-Job/Job.xs
(revision 473)
@@ -638,6 +638,12 @@
     OUTPUT:
        RETVAL
 
+void
+start(self)
+       JOB_T   self
+    CODE:
+       resume_threads(aTHX_ self->procs);
+
 HV*
 status(self)
        JOB_T   self
Index: C:/Documents and Settings/mlongtin/My
Documents/dbpro/svn/branches/newscheduler/extralibs/Win32-Job/Job.pm
===================================================================
--- C:/Documents and Settings/mlongtin/My
Documents/dbpro/svn/branches/newscheduler/extralibs/Win32-Job/Job.pm
(revision 470)
+++ C:/Documents and Settings/mlongtin/My
Documents/dbpro/svn/branches/newscheduler/extralibs/Win32-Job/Job.pm
(revision 473)
@@ -20,8 +20,21 @@
     )
 );
 
+use constant STILL_ACTIVE_EXITCODE => 259;
+use constant KILLED_EXITCODE => 293;
+
 Win32::Job->bootstrap($VERSION);
 
+sub is_running {
+    my ($self) = @_;
+    
+    my $status = $self->status;
+    my $processes_running = grep
+        { $_->{exitcode} == STILL_ACTIVE_EXITCODE }
+        values %$status;
+    return $processes_running;
+} # sub is_running
+
 1;
 
 __END__
@@ -286,6 +299,32 @@
    }, 1);
    print "Mod1 built ok!\n" if $ok;
 
+=item 4
+
+start()
+
+    start()
+    
+Start the job, but don't wait for it to finish. Has no return value.
You can
+check whether it's finished with is_running() or looking at the result of
+status(). 
+
+Here's equivalent code to the watch() example, except using start():
+
+   use Win32::Job;
+   $job = Win32::Job->new;
+   $job->spawn("cmd", q{cmd /C "cd Mod1 && nmake"}, {
+       stdin  => 'NUL', # the NUL device
+       stdout => 'stdout.log',
+       stderr => 'stdout.log',
+   });
+   $job->start();
+   while ( $job->is_running() ) {
+       sleep(1);
+       $job->kill if -s "stdout.log" > 1_000_000;
+   }
+   print "Mod1 built ok!\n" if $ok;
+
 =item 5
 
 status()
@@ -304,9 +343,12 @@
 
 =item exitcode
 
-The exit code returned by the process. If the process was killed because
-of a timeout, the value is 293.
+The exit code returned by the process. If the process was killed, the
value is
+293, if the process is still running, the value is 259.
 
+You can also use the constant Win32::Job::KILLED_EXITCODE or
+Win32::Job::STILL_ACTIVE_EXITCODE.
+
 =item time
 
 The time accumulated by the process. This is yet another subhash containing
@@ -326,6 +368,14 @@
 Sets the exit code to all processes killed to 293, which you can check
 for in the status() return value.
 
+=item 7
+
+is_running()
+
+    is_running()
+    
+Returns the number of processes currently running.
+
 =back
 
 =head1 SEE ALSO



Reply via email to