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
|
|
|
/**
|
2010-09-03 18:24:42 +00:00
|
|
|
* Job queue base code
|
|
|
|
|
*
|
|
|
|
|
* @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
|
|
|
|
|
|
|
|
if ( !defined( 'MEDIAWIKI' ) ) {
|
|
|
|
|
die( "This file is part of MediaWiki, it is not a valid entry point\n" );
|
|
|
|
|
}
|
|
|
|
|
|
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 );
|
|
|
|
|
|
2009-12-13 17:57:21 +00:00
|
|
|
$row = $dbw->selectRow(
|
|
|
|
|
'job',
|
|
|
|
|
'*',
|
|
|
|
|
array( 'job_cmd' => $type ),
|
|
|
|
|
__METHOD__,
|
|
|
|
|
array( 'LIMIT' => 1 )
|
|
|
|
|
);
|
2007-05-11 07:36:05 +00:00
|
|
|
|
2009-12-13 17:57:21 +00:00
|
|
|
if ( $row === false ) {
|
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();
|
2011-03-10 11:39:23 +00:00
|
|
|
$dbw->commit();
|
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 ) {
|
2011-03-20 16:52:57 +00:00
|
|
|
global $wgJobTypesExcludedFromDefaultQueue;
|
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
|
|
|
*/
|
2011-03-20 16:52:57 +00:00
|
|
|
$conditions = array();
|
|
|
|
|
if ( count( $wgJobTypesExcludedFromDefaultQueue ) != 0 ) {
|
|
|
|
|
foreach ( $wgJobTypesExcludedFromDefaultQueue as $cmdType ) {
|
|
|
|
|
$conditions[] = "job_cmd != " . $dbr->addQuotes( $cmdType );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$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();
|
2009-12-13 17:57:21 +00:00
|
|
|
$dbw->commit();
|
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();
|
2009-12-13 17:57:21 +00:00
|
|
|
$dbw->commit();
|
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 );
|
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
|
|
|
*/
|
|
|
|
|
static function factory( $command, $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
|
|
|
|
2006-06-18 12:42:16 +00:00
|
|
|
static function makeBlob( $params ) {
|
|
|
|
|
if ( $params !== false ) {
|
|
|
|
|
return serialize( $params );
|
|
|
|
|
} else {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
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
|
|
|
|
|
$dbw->begin();
|
|
|
|
|
$dbw->insert( 'job', $rows, __METHOD__, 'IGNORE' );
|
|
|
|
|
$dbw->commit();
|
|
|
|
|
$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
|
2008-07-22 22:44:34 +00:00
|
|
|
$dbw->begin();
|
2007-04-21 12:33:41 +00:00
|
|
|
$dbw->insert( 'job', $rows, __METHOD__, 'IGNORE' );
|
|
|
|
|
$dbw->commit();
|
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
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 ) ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
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
|
|
|
|
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(),
|
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 );
|
|
|
|
|
$dbw->begin();
|
|
|
|
|
$dbw->delete( 'job', $fields, __METHOD__ );
|
|
|
|
|
$affected = $dbw->affectedRows();
|
|
|
|
|
$dbw->commit();
|
|
|
|
|
if ( $affected ) {
|
|
|
|
|
wfIncrStats( 'job-dup-delete', $affected );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|