eZ Publish  [4.0]
ezruncronjobs.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Created on: <26-Jun-2008 10:16:45 oh>
00004 //
00005 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00006 // SOFTWARE NAME: eZ Publish
00007 // SOFTWARE RELEASE: 4.1.x
00008 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00009 // SOFTWARE LICENSE: GNU General Public License v2.0
00010 // NOTICE: >
00011 //   This program is free software; you can redistribute it and/or
00012 //   modify it under the terms of version 2.0  of the GNU General
00013 //   Public License as published by the Free Software Foundation.
00014 //
00015 //   This program is distributed in the hope that it will be useful,
00016 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 //   GNU General Public License for more details.
00019 //
00020 //   You should have received a copy of version 2.0 of the GNU General
00021 //   Public License along with this program; if not, write to the Free
00022 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00023 //   MA 02110-1301, USA.
00024 //
00025 //
00026 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00027 //
00028 
00029 /*!
00030  Class containing helper functions to execute cronjob parts.
00031 */
00032 class eZRunCronjobs
00033 {
00034     /*!
00035      \static
00036      Function for running a cronjob script.
00037     */
00038     static function runScript( $cli, $scriptFile )
00039     {
00040         $scriptMutex = new eZMutex( $scriptFile );
00041         $lockTS = $scriptMutex->lockTS();
00042         $runScript = false;
00043         if ( $lockTS === false )
00044         {
00045             if ( $scriptMutex->lock() )
00046             {
00047                 $scriptMutex->setMeta( 'pid', getmypid() );
00048                 $runScript = true;
00049             }
00050             else
00051             {
00052                 $cli->error( 'Failed to aquire cronjob part lock: ' . $scriptFile );
00053             }
00054         }
00055         // If the cronjob part has been blocked for  2 * eZRunCronjobs_MaxScriptExecutionTime,
00056         // force stealing of the cronjob part
00057         else if ( $lockTS < time() - 2 * eZRunCronjobs_MaxScriptExecutionTime )
00058         {
00059             $cli->output( 'Forcing to steal the mutex lock: ' . $scriptFile );
00060             $runScript = eZRunCronjobs::stealMutex( $cli, $scriptMutex, true );
00061         }
00062         else if ( $lockTS < time() - eZRunCronjobs_MaxScriptExecutionTime )
00063         {
00064             $cli->output( 'Trying to steal the mutex lock: ' . $scriptFile );
00065             $runScript = eZRunCronjobs::stealMutex( $cli, $scriptMutex );
00066         }
00067         else
00068         {
00069             $cli->output( 'Cronjob part locked by other process: ' . $scriptMutex->meta( 'pid' ) );
00070         }
00071         if ( $runScript )
00072         {
00073             global $script;
00074             global $isQuiet;
00075             global $cronPart;
00076             include( $scriptFile );
00077             $scriptMutex->unlock();
00078         }
00079     }
00080 
00081     /*!
00082      \static
00083      \private
00084 
00085      Steal a script mutex
00086 
00087      \param cli
00088      \param script mutex to steal
00089      \param force stealing of mutex ( optional, false by default )
00090 
00091      \return true if mutex is stole successfully
00092     */
00093     static function stealMutex( $cli, $scriptMutex, $force = false )
00094     {
00095         $cli->output( 'Stealing mutex. Old process has run too long.' );
00096         $oldPid = $scriptMutex->meta( 'pid' );
00097         if ( $force )
00098         {
00099             if ( is_numeric( $oldPid ) &&
00100                  $oldPid != 0 &&
00101                  function_exists( 'posix_kill' ) )
00102             {
00103                 $cli->output( 'Killing process: ' . $oldPid );
00104                 posix_kill( $oldPid, 9 );
00105             }
00106         }
00107         if ( $scriptMutex->steal( $force ) )
00108         {
00109             $scriptMutex->setMeta( 'pid', getmypid() );
00110             return true;
00111         }
00112         else
00113         {
00114             $cli->error( 'Failed to steal cronjob part lock.' );
00115         }
00116         return false;
00117     }
00118 }
00119 
00120 ?>