Permalink
Browse files

- Implemented: Shell logger, logging executor process to STDERR

  • Loading branch information...
1 parent 7658ef4 commit c0347df537ba10c6b6b11eb48eece3a761107b80 @kore committed May 13, 2010
Showing with 566 additions and 1 deletion.
  1. +3 −0 src/environment.php
  2. +26 −0 src/executor.php
  3. +11 −1 src/job_provider/shell.php
  4. +119 −0 src/logger/cli.php
  5. +67 −0 src/logger/dummy.php
  6. +119 −0 src/logger/shell.php
  7. +159 −0 tests/logger/shell_tests.php
  8. +60 −0 tests/logger_suite.php
  9. +2 −0 tests/suite.php
View
@@ -26,6 +26,9 @@
/*
* Includes all classes, which are required to use the Native PHP job queue.
*/
+require __DIR__ . '/logger.php';
+require __DIR__ . '/logger/dummy.php';
+require __DIR__ . '/logger/shell.php';
require __DIR__ . '/executor.php';
require __DIR__ . '/job_provider.php';
require __DIR__ . '/job_provider/shell.php';
View
@@ -36,6 +36,26 @@
class Executor
{
/**
+ * Logger used to log the execution
+ *
+ * @var Logger
+ */
+ protected $logger;
+
+ /**
+ * Construct from logger
+ *
+ * Defaults to a dummy logger, which does nothing
+ *
+ * @param Logger $logger
+ * @return void
+ */
+ public function __construct( Logger $logger = null )
+ {
+ $this->logger = $logger === null ? new DummyLogger() : $logger;
+ }
+
+ /**
* Run jobs
*
* Run all jobs provided by the job provider.
@@ -51,14 +71,18 @@ class Executor
*/
public function run( JobProvider $jobs, $parallel = 4 )
{
+ $this->logger->startExecutor( $this, $jobs );
+
$forks = array();
+ $jobNr = 0;
while ( $jobs->hasJobs() ||
count( $forks ) )
{
while ( ( count( $forks ) < $parallel ) &&
( $job = $jobs->getNextJob() ) )
{
+ $this->logger->progressJob( ++$jobNr );
if ( ( $forks[] = pcntl_fork() ) === 0 )
{
// We are the newly forked child, just execute the job
@@ -77,6 +101,8 @@ public function run( JobProvider $jobs, $parallel = 4 )
}
} while ( count( $forks ) >= $parallel );
}
+
+ $this->logger->finishedExecutor();
}
}
View
@@ -31,7 +31,7 @@
* Constructed from an array of shell commands, it returns those, to all be
* executed.
*/
-class ShellJobProvider implements JobProvider
+class ShellJobProvider implements JobProvider, \Countable
{
/**
* Array of shell commands, which are provided by this job provider and
@@ -89,5 +89,15 @@ public function getNextJob()
return shell_exec( $command );
};
}
+
+ /**
+ * Return numer of shell commands
+ *
+ * @return int
+ */
+ public function count()
+ {
+ return count( $this->shellCmds );
+ }
}
View
@@ -0,0 +1,119 @@
+<?php
+/**
+ * Native PHP job queue
+ *
+ * This file is part of njq.
+ *
+ * njq is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation; version 3 of the License.
+ *
+ * njq is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with njq; if not, write to the Free Software Foundation, Inc., 51
+ * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * @package VCSWrapper
+ * @subpackage Core
+ * @version $Revision: 954 $
+ * @license http://www.gnu.org/licenses/lgpl-3.0.txt LGPLv3
+ */
+
+namespace njq;
+
+/*
+ * CLI logger
+ *
+ * Prints the current executor status to STDERR. If the job provider implements
+ * Countable also a progress bar is printed.
+ */
+class ShellLogger implements Logger
+{
+ /**
+ * Number of jobs to execute
+ *
+ * @var int
+ */
+ protected $count;
+
+ /**
+ * Stream to write to
+ *
+ * @var resource
+ */
+ protected $stream;
+
+ /**
+ * Visual process indicators
+ *
+ * @var array
+ */
+ protected $processIndicators = array( '|', '/', '-', '\\' );
+
+ /**
+ * Construct from output stream to write to
+ *
+ * Defaults to STDERR.
+ *
+ * @param resource $output
+ * @return void
+ */
+ public function __construct( $output = STDERR )
+ {
+ $this->stream = $output;
+ }
+
+ /**
+ * Method called, when the executor run is started
+ *
+ * @param Executor $executor
+ * @return void
+ */
+ public function startExecutor( Executor $executoar, JobProvider $jobProvider )
+ {
+ if ( $jobProvider instanceof \Countable )
+ {
+ $this->count = count( $jobProvider );
+ }
+ }
+
+ /**
+ * Method called, when all jobs are executed
+ *
+ * @return void
+ */
+ public function finishedExecutor()
+ {
+ fprint( $this->stream, "\n" );
+ }
+
+ /**
+ * Method called, when all jobs are executed
+ *
+ * @return void
+ */
+ public function progressJob( $nr )
+ {
+ if ( $this->count )
+ {
+ fwrite( $this->stream, sprintf( " \r% 4d / %d (% 2.2F%%) %s ",
+ $nr + 1,
+ $this->count,
+ ( $nr + 1 ) / $this->count * 100,
+ $this->processIndicators[$nr % count( $this->processIndicators )]
+ ) );
+ }
+ else
+ {
+ fwrite( $this->stream, sprintf( " \r% 4d %s ",
+ $nr + 1,
+ $this->processIndicators[$nr % count( $this->processIndicators )]
+ ) );
+ }
+ }
+}
+
View
@@ -0,0 +1,67 @@
+<?php
+/**
+ * Native PHP job queue
+ *
+ * This file is part of njq.
+ *
+ * njq is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation; version 3 of the License.
+ *
+ * njq is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with njq; if not, write to the Free Software Foundation, Inc., 51
+ * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * @package VCSWrapper
+ * @subpackage Core
+ * @version $Revision: 954 $
+ * @license http://www.gnu.org/licenses/lgpl-3.0.txt LGPLv3
+ */
+
+namespace njq;
+
+/*
+ * CLI logger
+ *
+ * Prints the current executor status to STDERR. If the job provider implements
+ * Countable also a progress bar is printed.
+ */
+class DummyLogger implements Logger
+{
+ /**
+ * Method called, when the executor run is started
+ *
+ * @param Executor $executor
+ * @return void
+ */
+ public function startExecutor( Executor $executoar, JobProvider $jobProvider )
+ {
+ // Intentionally do nothing
+ }
+
+ /**
+ * Method called, when all jobs are executed
+ *
+ * @return void
+ */
+ public function finishedExecutor()
+ {
+ // Intentionally do nothing
+ }
+
+ /**
+ * Method called, when all jobs are executed
+ *
+ * @return void
+ */
+ public function progressJob( $nr )
+ {
+ // Intentionally do nothing
+ }
+}
+
View
@@ -0,0 +1,119 @@
+<?php
+/**
+ * Native PHP job queue
+ *
+ * This file is part of njq.
+ *
+ * njq is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation; version 3 of the License.
+ *
+ * njq is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with njq; if not, write to the Free Software Foundation, Inc., 51
+ * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ *
+ * @package VCSWrapper
+ * @subpackage Core
+ * @version $Revision: 954 $
+ * @license http://www.gnu.org/licenses/lgpl-3.0.txt LGPLv3
+ */
+
+namespace njq;
+
+/*
+ * CLI logger
+ *
+ * Prints the current executor status to STDERR. If the job provider implements
+ * Countable also a progress bar is printed.
+ */
+class ShellLogger implements Logger
+{
+ /**
+ * Number of jobs to execute
+ *
+ * @var int
+ */
+ protected $count;
+
+ /**
+ * Stream to write to
+ *
+ * @var resource
+ */
+ protected $stream;
+
+ /**
+ * Visual process indicators
+ *
+ * @var array
+ */
+ protected $processIndicators = array( '|', '/', '-', '\\' );
+
+ /**
+ * Construct from output stream to write to
+ *
+ * Defaults to STDERR.
+ *
+ * @param resource $output
+ * @return void
+ */
+ public function __construct( $output = STDERR )
+ {
+ $this->stream = $output;
+ }
+
+ /**
+ * Method called, when the executor run is started
+ *
+ * @param Executor $executor
+ * @return void
+ */
+ public function startExecutor( Executor $executoar, JobProvider $jobProvider )
+ {
+ if ( $jobProvider instanceof \Countable )
+ {
+ $this->count = count( $jobProvider );
+ }
+ }
+
+ /**
+ * Method called, when all jobs are executed
+ *
+ * @return void
+ */
+ public function finishedExecutor()
+ {
+ fwrite( $this->stream, "\n" );
+ }
+
+ /**
+ * Method called, when all jobs are executed
+ *
+ * @return void
+ */
+ public function progressJob( $nr )
+ {
+ if ( $this->count )
+ {
+ \fwrite( $this->stream, \sprintf( " \r% 4d / %d (% 2.2F%%) %s ",
+ $nr,
+ $this->count,
+ $nr / $this->count * 100,
+ $this->processIndicators[$nr % count( $this->processIndicators )]
+ ) );
+ }
+ else
+ {
+ \fwrite( $this->stream, \sprintf( " \r% 4d %s ",
+ $nr,
+ $this->processIndicators[$nr % count( $this->processIndicators )]
+ ) );
+ }
+ }
+}
+
Oops, something went wrong.

0 comments on commit c0347df

Please sign in to comment.