2006-02-24 01:56:31 +00:00
|
|
|
<?php
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
/**
|
2012-05-04 06:29:11 +00:00
|
|
|
* Job queue base code.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
2010-09-03 18:24:42 +00:00
|
|
|
*
|
|
|
|
|
* @file
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @defgroup JobQueue JobQueue
|
|
|
|
|
*/
|
2006-02-24 01:56:31 +00:00
|
|
|
|
2007-04-04 05:22:37 +00:00
|
|
|
/**
|
2007-04-21 12:33:41 +00:00
|
|
|
* Class to both describe a background job and handle jobs.
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
*
|
|
|
|
|
* @ingroup JobQueue
|
2007-04-04 05:22:37 +00:00
|
|
|
*/
|
2006-06-18 12:42:16 +00:00
|
|
|
abstract class Job {
|
2011-02-19 21:56:54 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var Title
|
|
|
|
|
*/
|
|
|
|
|
var $title;
|
|
|
|
|
|
2006-05-11 22:40:38 +00:00
|
|
|
var $command,
|
2006-03-11 17:13:49 +00:00
|
|
|
$params,
|
2006-02-25 01:38:06 +00:00
|
|
|
$id,
|
2006-03-11 17:13:49 +00:00
|
|
|
$removeDuplicates,
|
2006-02-24 01:56:31 +00:00
|
|
|
$error;
|
|
|
|
|
|
2007-04-21 12:33:41 +00:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
|
* Abstract functions
|
|
|
|
|
*------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Run the job
|
|
|
|
|
* @return boolean success
|
|
|
|
|
*/
|
|
|
|
|
abstract function run();
|
|
|
|
|
|
2006-02-24 01:56:31 +00:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
|
* Static functions
|
|
|
|
|
*------------------------------------------------------------------------*/
|
2006-06-18 12:42:16 +00:00
|
|
|
|
2007-05-11 07:36:05 +00:00
|
|
|
/**
|
2008-04-14 07:45:50 +00:00
|
|
|
* Pop a job of a certain type. This tries less hard than pop() to
|
|
|
|
|
* actually find a job; it may be adversely affected by concurrent job
|
2007-05-11 07:36:05 +00:00
|
|
|
* runners.
|
2011-05-28 18:59:42 +00:00
|
|
|
*
|
|
|
|
|
* @param $type string
|
|
|
|
|
*
|
|
|
|
|
* @return Job
|
2007-05-11 07:36:05 +00:00
|
|
|
*/
|
2009-12-13 17:57:21 +00:00
|
|
|
static function pop_type( $type ) {
|
2007-05-11 07:36:05 +00:00
|
|
|
wfProfilein( __METHOD__ );
|
|
|
|
|
|
|
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
|
|
|
|
|
2012-02-24 18:45:24 +00:00
|
|
|
$dbw->begin( __METHOD__ );
|
2011-07-12 08:08:58 +00:00
|
|
|
|
2009-12-13 17:57:21 +00:00
|
|
|
$row = $dbw->selectRow(
|
|
|
|
|
'job',
|
|
|
|
|
'*',
|
|
|
|
|
array( 'job_cmd' => $type ),
|
|
|
|
|
__METHOD__,
|
2011-07-12 08:08:58 +00:00
|
|
|
array( 'LIMIT' => 1, 'FOR UPDATE' )
|
2009-12-13 17:57:21 +00:00
|
|
|
);
|
2007-05-11 07:36:05 +00:00
|
|
|
|
2009-12-13 17:57:21 +00:00
|
|
|
if ( $row === false ) {
|
2012-02-24 18:45:24 +00:00
|
|
|
$dbw->commit( __METHOD__ );
|
2007-05-11 07:36:05 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Ensure we "own" this row */
|
|
|
|
|
$dbw->delete( 'job', array( 'job_id' => $row->job_id ), __METHOD__ );
|
|
|
|
|
$affected = $dbw->affectedRows();
|
2012-02-24 18:45:24 +00:00
|
|
|
$dbw->commit( __METHOD__ );
|
2007-05-11 07:36:05 +00:00
|
|
|
|
2009-12-13 17:57:21 +00:00
|
|
|
if ( $affected == 0 ) {
|
2007-05-11 07:36:05 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-10 00:00:34 +00:00
|
|
|
wfIncrStats( 'job-pop' );
|
2007-05-11 07:36:05 +00:00
|
|
|
$namespace = $row->job_namespace;
|
|
|
|
|
$dbkey = $row->job_title;
|
|
|
|
|
$title = Title::makeTitleSafe( $namespace, $dbkey );
|
2010-04-17 16:49:20 +00:00
|
|
|
$job = Job::factory( $row->job_cmd, $title, Job::extractBlob( $row->job_params ),
|
|
|
|
|
$row->job_id );
|
2007-05-11 07:36:05 +00:00
|
|
|
|
2011-03-10 11:39:23 +00:00
|
|
|
$job->removeDuplicates();
|
2011-03-10 02:27:15 +00:00
|
|
|
|
2007-05-11 07:36:05 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
|
return $job;
|
|
|
|
|
}
|
|
|
|
|
|
2006-02-24 01:56:31 +00:00
|
|
|
/**
|
|
|
|
|
* Pop a job off the front of the queue
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
*
|
2009-12-13 17:57:21 +00:00
|
|
|
* @param $offset Integer: Number of jobs to skip
|
2006-02-24 01:56:31 +00:00
|
|
|
* @return Job or false if there's no jobs
|
|
|
|
|
*/
|
2009-12-13 17:57:21 +00:00
|
|
|
static function pop( $offset = 0 ) {
|
2006-06-18 12:42:16 +00:00
|
|
|
wfProfileIn( __METHOD__ );
|
2006-02-24 01:56:31 +00:00
|
|
|
|
2007-01-22 23:50:42 +00:00
|
|
|
$dbr = wfGetDB( DB_SLAVE );
|
2006-02-25 01:12:35 +00:00
|
|
|
|
2008-04-14 07:45:50 +00:00
|
|
|
/* Get a job from the slave, start with an offset,
|
2007-03-25 15:55:39 +00:00
|
|
|
scan full set afterwards, avoid hitting purged rows
|
2007-04-21 12:33:41 +00:00
|
|
|
|
2008-04-14 07:45:50 +00:00
|
|
|
NB: If random fetch previously was used, offset
|
|
|
|
|
will always be ahead of few entries
|
2007-03-25 15:55:39 +00:00
|
|
|
*/
|
2012-06-05 22:58:54 +00:00
|
|
|
|
|
|
|
|
$conditions = self::defaultQueueConditions();
|
|
|
|
|
|
2011-03-20 16:52:57 +00:00
|
|
|
$offset = intval( $offset );
|
2011-06-30 20:32:41 +00:00
|
|
|
$options = array( 'ORDER BY' => 'job_id', 'USE INDEX' => 'PRIMARY' );
|
|
|
|
|
|
|
|
|
|
$row = $dbr->selectRow( 'job', '*',
|
|
|
|
|
array_merge( $conditions, array( "job_id >= $offset" ) ),
|
|
|
|
|
__METHOD__,
|
|
|
|
|
$options
|
2011-03-20 16:52:57 +00:00
|
|
|
);
|
2007-04-21 12:33:41 +00:00
|
|
|
|
2007-03-25 15:55:39 +00:00
|
|
|
// Refetching without offset is needed as some of job IDs could have had delayed commits
|
|
|
|
|
// and have lower IDs than jobs already executed, blame concurrency :)
|
2007-04-21 12:33:41 +00:00
|
|
|
//
|
2009-12-13 17:57:21 +00:00
|
|
|
if ( $row === false ) {
|
|
|
|
|
if ( $offset != 0 ) {
|
2011-06-30 20:32:41 +00:00
|
|
|
$row = $dbr->selectRow( 'job', '*', $conditions, __METHOD__, $options );
|
2009-12-13 17:57:21 +00:00
|
|
|
}
|
2007-04-21 12:33:41 +00:00
|
|
|
|
2009-12-13 17:57:21 +00:00
|
|
|
if ( $row === false ) {
|
2007-03-25 15:55:39 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
|
return false;
|
2007-04-21 12:33:41 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-02-25 01:12:35 +00:00
|
|
|
// Try to delete it from the master
|
2007-01-22 23:50:42 +00:00
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
2006-06-18 12:42:16 +00:00
|
|
|
$dbw->delete( 'job', array( 'job_id' => $row->job_id ), __METHOD__ );
|
2006-02-24 01:56:31 +00:00
|
|
|
$affected = $dbw->affectedRows();
|
2012-02-24 18:45:24 +00:00
|
|
|
$dbw->commit( __METHOD__ );
|
2006-02-24 01:56:31 +00:00
|
|
|
|
2006-02-25 01:12:35 +00:00
|
|
|
if ( !$affected ) {
|
|
|
|
|
// Failed, someone else beat us to it
|
|
|
|
|
// Try getting a random row
|
2006-03-11 17:13:49 +00:00
|
|
|
$row = $dbw->selectRow( 'job', array( 'MIN(job_id) as minjob',
|
2008-08-04 05:07:53 +00:00
|
|
|
'MAX(job_id) as maxjob' ), '1=1', __METHOD__ );
|
2006-02-25 01:12:35 +00:00
|
|
|
if ( $row === false || is_null( $row->minjob ) || is_null( $row->maxjob ) ) {
|
|
|
|
|
// No jobs to get
|
2006-06-18 12:42:16 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2006-02-25 01:12:35 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
// Get the random row
|
2006-03-11 17:13:49 +00:00
|
|
|
$row = $dbw->selectRow( 'job', '*',
|
2009-12-13 17:57:21 +00:00
|
|
|
'job_id >= ' . mt_rand( $row->minjob, $row->maxjob ), __METHOD__ );
|
2006-02-25 01:12:35 +00:00
|
|
|
if ( $row === false ) {
|
|
|
|
|
// Random job gone before we got the chance to select it
|
|
|
|
|
// Give up
|
2006-06-18 12:42:16 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2006-02-25 01:12:35 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
// Delete the random row
|
2006-06-18 12:42:16 +00:00
|
|
|
$dbw->delete( 'job', array( 'job_id' => $row->job_id ), __METHOD__ );
|
2006-02-25 01:12:35 +00:00
|
|
|
$affected = $dbw->affectedRows();
|
2012-02-24 18:45:24 +00:00
|
|
|
$dbw->commit( __METHOD__ );
|
2007-04-21 12:33:41 +00:00
|
|
|
|
2006-02-25 01:12:35 +00:00
|
|
|
if ( !$affected ) {
|
|
|
|
|
// Random job gone before we exclusively deleted it
|
|
|
|
|
// Give up
|
2006-06-18 12:42:16 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2006-02-25 01:12:35 +00:00
|
|
|
return false;
|
2006-03-11 17:13:49 +00:00
|
|
|
}
|
2006-02-24 01:56:31 +00:00
|
|
|
}
|
2007-04-21 12:33:41 +00:00
|
|
|
|
2006-02-25 01:12:35 +00:00
|
|
|
// If execution got to here, there's a row in $row that has been deleted from the database
|
|
|
|
|
// by this thread. Hence the concurrent pop was successful.
|
2011-03-10 00:00:34 +00:00
|
|
|
wfIncrStats( 'job-pop' );
|
2006-02-25 01:12:35 +00:00
|
|
|
$namespace = $row->job_namespace;
|
|
|
|
|
$dbkey = $row->job_title;
|
2006-02-24 01:56:31 +00:00
|
|
|
$title = Title::makeTitleSafe( $namespace, $dbkey );
|
2012-05-02 15:04:29 +00:00
|
|
|
|
|
|
|
|
if ( is_null( $title ) ) {
|
2012-06-05 22:58:54 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2012-05-02 15:04:29 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2006-06-18 12:42:16 +00:00
|
|
|
$job = Job::factory( $row->job_cmd, $title, Job::extractBlob( $row->job_params ), $row->job_id );
|
2007-04-21 12:33:41 +00:00
|
|
|
|
2006-05-31 23:56:41 +00:00
|
|
|
// Remove any duplicates it may have later in the queue
|
2011-03-10 11:39:23 +00:00
|
|
|
$job->removeDuplicates();
|
2011-03-10 02:27:15 +00:00
|
|
|
|
2006-06-18 12:42:16 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2006-02-24 01:56:31 +00:00
|
|
|
return $job;
|
|
|
|
|
}
|
|
|
|
|
|
2007-04-21 12:33:41 +00:00
|
|
|
/**
|
2007-06-21 19:11:24 +00:00
|
|
|
* Create the appropriate object to handle a specific job
|
|
|
|
|
*
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @param $command String: Job command
|
|
|
|
|
* @param $title Title: Associated title
|
|
|
|
|
* @param $params Array: Job parameters
|
|
|
|
|
* @param $id Int: Job identifier
|
2007-06-21 19:11:24 +00:00
|
|
|
* @return Job
|
2006-06-18 12:42:16 +00:00
|
|
|
*/
|
2012-05-02 15:04:29 +00:00
|
|
|
static function factory( $command, Title $title, $params = false, $id = 0 ) {
|
2007-06-21 19:11:24 +00:00
|
|
|
global $wgJobClasses;
|
|
|
|
|
if( isset( $wgJobClasses[$command] ) ) {
|
|
|
|
|
$class = $wgJobClasses[$command];
|
|
|
|
|
return new $class( $title, $params, $id );
|
2006-06-18 12:42:16 +00:00
|
|
|
}
|
2007-06-21 19:11:24 +00:00
|
|
|
throw new MWException( "Invalid job command `{$command}`" );
|
2006-06-18 12:42:16 +00:00
|
|
|
}
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2011-10-26 04:15:09 +00:00
|
|
|
/**
|
|
|
|
|
* @param $params
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2006-06-18 12:42:16 +00:00
|
|
|
static function makeBlob( $params ) {
|
|
|
|
|
if ( $params !== false ) {
|
|
|
|
|
return serialize( $params );
|
|
|
|
|
} else {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-26 04:15:09 +00:00
|
|
|
/**
|
|
|
|
|
* @param $blob
|
|
|
|
|
* @return bool|mixed
|
|
|
|
|
*/
|
2006-06-18 12:42:16 +00:00
|
|
|
static function extractBlob( $blob ) {
|
|
|
|
|
if ( (string)$blob !== '' ) {
|
|
|
|
|
return unserialize( $blob );
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-04-21 12:33:41 +00:00
|
|
|
/**
|
|
|
|
|
* Batch-insert a group of jobs into the queue.
|
|
|
|
|
* This will be wrapped in a transaction with a forced commit.
|
|
|
|
|
*
|
|
|
|
|
* This may add duplicate at insert time, but they will be
|
|
|
|
|
* removed later on, when the first one is popped.
|
|
|
|
|
*
|
|
|
|
|
* @param $jobs array of Job objects
|
|
|
|
|
*/
|
|
|
|
|
static function batchInsert( $jobs ) {
|
2011-03-18 07:06:18 +00:00
|
|
|
if ( !count( $jobs ) ) {
|
2008-07-22 22:44:34 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
|
|
|
|
$rows = array();
|
2012-01-03 15:08:05 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var $job Job
|
|
|
|
|
*/
|
2011-03-18 07:06:18 +00:00
|
|
|
foreach ( $jobs as $job ) {
|
2008-07-22 22:44:34 +00:00
|
|
|
$rows[] = $job->insertFields();
|
|
|
|
|
if ( count( $rows ) >= 50 ) {
|
|
|
|
|
# Do a small transaction to avoid slave lag
|
2012-02-24 18:45:24 +00:00
|
|
|
$dbw->begin( __METHOD__ );
|
2008-07-22 22:44:34 +00:00
|
|
|
$dbw->insert( 'job', $rows, __METHOD__, 'IGNORE' );
|
2012-02-24 18:45:24 +00:00
|
|
|
$dbw->commit( __METHOD__ );
|
2008-07-22 22:44:34 +00:00
|
|
|
$rows = array();
|
2007-04-21 12:33:41 +00:00
|
|
|
}
|
2008-07-22 22:44:34 +00:00
|
|
|
}
|
2011-03-18 07:06:18 +00:00
|
|
|
if ( $rows ) { // last chunk
|
2012-02-24 18:45:24 +00:00
|
|
|
$dbw->begin( __METHOD__ );
|
2007-04-21 12:33:41 +00:00
|
|
|
$dbw->insert( 'job', $rows, __METHOD__, 'IGNORE' );
|
2012-02-24 18:45:24 +00:00
|
|
|
$dbw->commit( __METHOD__ );
|
2007-04-21 12:33:41 +00:00
|
|
|
}
|
2011-03-18 07:06:18 +00:00
|
|
|
wfIncrStats( 'job-insert', count( $jobs ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Insert a group of jobs into the queue.
|
|
|
|
|
*
|
|
|
|
|
* Same as batchInsert() but does not commit and can thus
|
|
|
|
|
* be rolled-back as part of a larger transaction. However,
|
|
|
|
|
* large batches of jobs can cause slave lag.
|
|
|
|
|
*
|
|
|
|
|
* @param $jobs array of Job objects
|
|
|
|
|
*/
|
|
|
|
|
static function safeBatchInsert( $jobs ) {
|
|
|
|
|
if ( !count( $jobs ) ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
|
|
|
|
$rows = array();
|
|
|
|
|
foreach ( $jobs as $job ) {
|
|
|
|
|
$rows[] = $job->insertFields();
|
|
|
|
|
if ( count( $rows ) >= 500 ) {
|
|
|
|
|
$dbw->insert( 'job', $rows, __METHOD__, 'IGNORE' );
|
|
|
|
|
$rows = array();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( $rows ) { // last chunk
|
|
|
|
|
$dbw->insert( 'job', $rows, __METHOD__, 'IGNORE' );
|
|
|
|
|
}
|
|
|
|
|
wfIncrStats( 'job-insert', count( $jobs ) );
|
2007-04-21 12:33:41 +00:00
|
|
|
}
|
|
|
|
|
|
2012-06-05 22:58:54 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* SQL conditions to apply on most JobQueue queries
|
|
|
|
|
*
|
|
|
|
|
* Whenever we exclude jobs types from the default queue, we want to make
|
|
|
|
|
* sure that queries to the job queue actually ignore them.
|
|
|
|
|
*
|
|
|
|
|
* @return array SQL conditions suitable for Database:: methods
|
|
|
|
|
*/
|
|
|
|
|
static function defaultQueueConditions( ) {
|
|
|
|
|
global $wgJobTypesExcludedFromDefaultQueue;
|
|
|
|
|
$conditions = array();
|
|
|
|
|
if ( count( $wgJobTypesExcludedFromDefaultQueue ) > 0 ) {
|
|
|
|
|
$dbr = wfGetDB( DB_SLAVE );
|
|
|
|
|
foreach ( $wgJobTypesExcludedFromDefaultQueue as $cmdType ) {
|
|
|
|
|
$conditions[] = "job_cmd != " . $dbr->addQuotes( $cmdType );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $conditions;
|
|
|
|
|
}
|
|
|
|
|
|
2006-02-24 01:56:31 +00:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
|
* Non-static functions
|
|
|
|
|
*------------------------------------------------------------------------*/
|
|
|
|
|
|
2011-02-28 16:37:34 +00:00
|
|
|
/**
|
|
|
|
|
* @param $command
|
|
|
|
|
* @param $title
|
|
|
|
|
* @param $params array
|
|
|
|
|
* @param int $id
|
|
|
|
|
*/
|
2006-06-18 12:42:16 +00:00
|
|
|
function __construct( $command, $title, $params = false, $id = 0 ) {
|
2006-02-24 01:56:31 +00:00
|
|
|
$this->command = $command;
|
|
|
|
|
$this->title = $title;
|
|
|
|
|
$this->params = $params;
|
2006-02-25 01:38:06 +00:00
|
|
|
$this->id = $id;
|
2006-02-24 01:56:31 +00:00
|
|
|
|
|
|
|
|
// A bit of premature generalisation
|
|
|
|
|
// Oh well, the whole class is premature generalisation really
|
|
|
|
|
$this->removeDuplicates = true;
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-31 23:56:41 +00:00
|
|
|
/**
|
|
|
|
|
* Insert a single job into the queue.
|
2010-04-17 16:49:20 +00:00
|
|
|
* @return bool true on success
|
2006-05-31 23:56:41 +00:00
|
|
|
*/
|
2006-02-24 01:56:31 +00:00
|
|
|
function insert() {
|
2006-05-31 23:56:41 +00:00
|
|
|
$fields = $this->insertFields();
|
2006-02-24 01:56:31 +00:00
|
|
|
|
2007-01-22 23:50:42 +00:00
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
2007-04-21 12:33:41 +00:00
|
|
|
|
2006-02-24 01:56:31 +00:00
|
|
|
if ( $this->removeDuplicates ) {
|
2006-06-18 12:42:16 +00:00
|
|
|
$res = $dbw->select( 'job', array( '1' ), $fields, __METHOD__ );
|
2006-02-25 00:29:15 +00:00
|
|
|
if ( $dbw->numRows( $res ) ) {
|
2011-10-26 04:15:09 +00:00
|
|
|
return true;
|
2006-02-25 00:29:15 +00:00
|
|
|
}
|
2006-02-24 01:56:31 +00:00
|
|
|
}
|
2011-03-10 00:00:34 +00:00
|
|
|
wfIncrStats( 'job-insert' );
|
2010-04-17 16:49:20 +00:00
|
|
|
return $dbw->insert( 'job', $fields, __METHOD__ );
|
2006-02-24 01:56:31 +00:00
|
|
|
}
|
2007-04-21 12:33:41 +00:00
|
|
|
|
2011-10-26 04:15:09 +00:00
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2006-05-31 23:56:41 +00:00
|
|
|
protected function insertFields() {
|
2009-11-09 14:34:03 +00:00
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
2006-05-31 23:56:41 +00:00
|
|
|
return array(
|
2009-11-09 14:34:03 +00:00
|
|
|
'job_id' => $dbw->nextSequenceValue( 'job_job_id_seq' ),
|
2006-05-31 23:56:41 +00:00
|
|
|
'job_cmd' => $this->command,
|
|
|
|
|
'job_namespace' => $this->title->getNamespace(),
|
|
|
|
|
'job_title' => $this->title->getDBkey(),
|
2012-01-03 15:14:35 +00:00
|
|
|
'job_timestamp' => $dbw->timestamp(),
|
2006-06-18 12:42:16 +00:00
|
|
|
'job_params' => Job::makeBlob( $this->params )
|
2006-05-31 23:56:41 +00:00
|
|
|
);
|
|
|
|
|
}
|
2006-02-24 01:56:31 +00:00
|
|
|
|
2011-03-10 11:39:23 +00:00
|
|
|
/**
|
|
|
|
|
* Remove jobs in the job queue which are duplicates of this job.
|
|
|
|
|
* This is deadlock-prone and so starts its own transaction.
|
|
|
|
|
*/
|
|
|
|
|
function removeDuplicates() {
|
|
|
|
|
if ( !$this->removeDuplicates ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$fields = $this->insertFields();
|
|
|
|
|
unset( $fields['job_id'] );
|
|
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
2012-02-24 18:45:24 +00:00
|
|
|
$dbw->begin( __METHOD__ );
|
2011-03-10 11:39:23 +00:00
|
|
|
$dbw->delete( 'job', $fields, __METHOD__ );
|
|
|
|
|
$affected = $dbw->affectedRows();
|
2012-02-24 18:45:24 +00:00
|
|
|
$dbw->commit( __METHOD__ );
|
2011-03-10 11:39:23 +00:00
|
|
|
if ( $affected ) {
|
|
|
|
|
wfIncrStats( 'job-dup-delete', $affected );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-26 04:15:09 +00:00
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2006-06-18 12:42:16 +00:00
|
|
|
function toString() {
|
|
|
|
|
$paramString = '';
|
|
|
|
|
if ( $this->params ) {
|
|
|
|
|
foreach ( $this->params as $key => $value ) {
|
|
|
|
|
if ( $paramString != '' ) {
|
|
|
|
|
$paramString .= ' ';
|
2006-06-15 06:35:24 +00:00
|
|
|
}
|
2006-06-18 12:42:16 +00:00
|
|
|
$paramString .= "$key=$value";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( is_object( $this->title ) ) {
|
|
|
|
|
$s = "{$this->command} " . $this->title->getPrefixedDBkey();
|
|
|
|
|
if ( $paramString !== '' ) {
|
|
|
|
|
$s .= ' ' . $paramString;
|
|
|
|
|
}
|
|
|
|
|
return $s;
|
|
|
|
|
} else {
|
|
|
|
|
return "{$this->command} $paramString";
|
2006-02-24 01:56:31 +00:00
|
|
|
}
|
2006-06-18 12:42:16 +00:00
|
|
|
}
|
|
|
|
|
|
2008-07-22 22:44:34 +00:00
|
|
|
protected function setLastError( $error ) {
|
|
|
|
|
$this->error = $error;
|
|
|
|
|
}
|
|
|
|
|
|
2006-06-18 12:42:16 +00:00
|
|
|
function getLastError() {
|
|
|
|
|
return $this->error;
|
|
|
|
|
}
|
|
|
|
|
}
|