Skip to content

Commit

Permalink
Merge pull request #1067 from pbras/ezp23312-async-processes
Browse files Browse the repository at this point in the history
fix EZP-23312: asynchronous publisher: old processes are filling up the ...
  • Loading branch information
yannickroger committed Sep 18, 2014
2 parents 05a0158 + ff8d0dc commit 36722f5
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
16 changes: 14 additions & 2 deletions bin/php/ezasynchronouspublisher.php
Expand Up @@ -35,16 +35,27 @@

$options = $script->getOptions(
// options definition
"[n|daemon][p:|pid-file:]",
"[n|daemon][p:|pid-file:][cleanup-interval:]",
// arguments definition
"",
// options documentation
array( 'daemon' => 'Run in the background',
'pid-file' => 'PID file' ) );
'pid-file' => 'PID file',
'cleanup-interval' => 'Number of seconds between each db table cleanup. Default value is 43200 (12 hours)' ) );
$sys = eZSys::instance();

$script->initialize();

$cleanup = 0;
if ( isset( $options['cleanup-interval'] ) )
{
$cleanup = $options['cleanup-interval'];
if ( !is_int( $cleanup ) || $cleanup < 1 )
{
$script->shutdown( 3, "Invalid value for cleanup-interval:'$cleanup'" );
}
}

if ( isset( $options['pid-file'] ) )
{
$pidFile = $options['pid-file'];
Expand Down Expand Up @@ -192,6 +203,7 @@
$processor = ezpContentPublishingQueueProcessor::instance();
$processor->setOutput( $output );
$processor->setSignalHandler( $daemonSignalHandler );
$processor->setCleanupInterval( $cleanup );
$processor->run();

eZScript::instance()->shutdown( 0 );
Expand Down
64 changes: 64 additions & 0 deletions kernel/private/classes/ezpcontentpublishingqueueprocessor.php
Expand Up @@ -28,6 +28,9 @@ public function __construct()
// output to log by default
$this->setOutput( new ezpAsynchronousPublisherLogOutput );

// initiates timer for the DB cleanup process
$this->cleanupLastTime = time();

// Queue reader handler
$this->queueReader = $this->contentINI->variable( 'PublishingSettings', 'AsynchronousPublishingQueueReader' );
$reflection = new ReflectionClass( $this->queueReader );
Expand Down Expand Up @@ -78,6 +81,7 @@ public function run()
while ( $this->canProcess )
{
try {
$this->cleanupFinishedProcesses();
/** @var ezpContentPublishingProcess $publishingItem */
$publishingItem = $this->getNextItem();
if ( $publishingItem === false )
Expand Down Expand Up @@ -149,6 +153,41 @@ private function cleanupDeadProcesses()
}
}

/**
* Removes FINISHED processes rows from the db, older than one week, all in one db call
* method self-manages the removal, based on the defined treshhold
*
* @return void
*/
private function cleanupFinishedProcesses()
{
if ( time() < ( $this->cleanupLastTime + $this->cleanupInterval ) )
{
return;
}

$processes = count( ezpContentPublishingProcess::fetchProcesses( ezpContentPublishingProcess::STATUS_FINISHED ) );
if ( $processes > 0 )
{
//Remove all objects at once
// this is required as the MySQL connection might be closed anytime by a fork
try
{
eZDebug::writeNotice( "ASYNC:: removing processes entries marked as STATUS_FINISHED in database.");
$db = eZDB::instance();
eZDB::setInstance( null );
$lastWeek = time() - (7 * 24 * 60 * 60);
$processTable = ezpContentPublishingProcess::definition()['name'];
$db->query( "DELETE from ". $processTable. " WHERE status =". ezpContentPublishingProcess::STATUS_FINISHED. " AND finished < ". $lastWeek );
$this->cleanupLastTime = time();
}
catch( eZDBException $e )
{
// Do nothing, this will be retried until the DB is back up
}
}
}

/**
* Child process signal handler
*/
Expand Down Expand Up @@ -263,6 +302,19 @@ private function getNextItem()
return call_user_func( array( $this->queueReader, 'next' ) );
}

/**
* Sets the cleanup variables
* @param int $interval
* @param \
*/
public function setCleanupInterval( $interval )
{
if ( $interval > 0 )
{
$this->cleanupInterval = $interval;
}
}

/**
* @var eZINI
*/
Expand Down Expand Up @@ -304,5 +356,17 @@ private function getNextItem()
* @var ezpContentPublishingQueueReaderInterface
*/
private $queueReader;

/**
* Time counter for cleanup of finished processes
* @var int
*/
private $cleanupLastTime;

/**
* Interval for cleanup of finished processes. Default value is 12 hours in seconds
* @var int
*/
private $cleanupInterval = 43200;
}
?>

0 comments on commit 36722f5

Please sign in to comment.