Skip to content

Commit

Permalink
- Implemented: Shell logger, logging executor process to STDERR
Browse files Browse the repository at this point in the history
  • Loading branch information
kore committed May 13, 2010
1 parent 7658ef4 commit c0347df
Show file tree
Hide file tree
Showing 9 changed files with 566 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/environment.php
Expand Up @@ -26,6 +26,9 @@
/* /*
* Includes all classes, which are required to use the Native PHP job queue. * 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__ . '/executor.php';
require __DIR__ . '/job_provider.php'; require __DIR__ . '/job_provider.php';
require __DIR__ . '/job_provider/shell.php'; require __DIR__ . '/job_provider/shell.php';
Expand Down
26 changes: 26 additions & 0 deletions src/executor.php
Expand Up @@ -35,6 +35,26 @@
*/ */
class Executor 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 jobs
* *
Expand All @@ -51,14 +71,18 @@ class Executor
*/ */
public function run( JobProvider $jobs, $parallel = 4 ) public function run( JobProvider $jobs, $parallel = 4 )
{ {
$this->logger->startExecutor( $this, $jobs );

$forks = array(); $forks = array();
$jobNr = 0;
while ( $jobs->hasJobs() || while ( $jobs->hasJobs() ||
count( $forks ) ) count( $forks ) )
{ {


while ( ( count( $forks ) < $parallel ) && while ( ( count( $forks ) < $parallel ) &&
( $job = $jobs->getNextJob() ) ) ( $job = $jobs->getNextJob() ) )
{ {
$this->logger->progressJob( ++$jobNr );
if ( ( $forks[] = pcntl_fork() ) === 0 ) if ( ( $forks[] = pcntl_fork() ) === 0 )
{ {
// We are the newly forked child, just execute the job // We are the newly forked child, just execute the job
Expand All @@ -77,6 +101,8 @@ public function run( JobProvider $jobs, $parallel = 4 )
} }
} while ( count( $forks ) >= $parallel ); } while ( count( $forks ) >= $parallel );
} }

$this->logger->finishedExecutor();
} }
} }


12 changes: 11 additions & 1 deletion src/job_provider/shell.php
Expand Up @@ -31,7 +31,7 @@
* Constructed from an array of shell commands, it returns those, to all be * Constructed from an array of shell commands, it returns those, to all be
* executed. * executed.
*/ */
class ShellJobProvider implements JobProvider class ShellJobProvider implements JobProvider, \Countable
{ {
/** /**
* Array of shell commands, which are provided by this job provider and * Array of shell commands, which are provided by this job provider and
Expand Down Expand Up @@ -89,5 +89,15 @@ public function getNextJob()
return shell_exec( $command ); return shell_exec( $command );
}; };
} }

/**
* Return numer of shell commands
*
* @return int
*/
public function count()
{
return count( $this->shellCmds );
}
} }


119 changes: 119 additions & 0 deletions src/logger/cli.php
@@ -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 )]
) );
}
}
}

67 changes: 67 additions & 0 deletions src/logger/dummy.php
@@ -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
}
}

119 changes: 119 additions & 0 deletions src/logger/shell.php
@@ -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 )]
) );
}
}
}

0 comments on commit c0347df

Please sign in to comment.