class Job {
var $command,
$title,
- $params,
- $removeDuplicates,
+ $params,
+ $id,
+ $removeDuplicates,
$error;
/*-------------------------------------------------------------------------
*------------------------------------------------------------------------*/
/**
* Add an array of refreshLinks jobs to the queue
- * @param array $titles Array of title objects.
+ * @param array $titles Array of title objects.
* @static
*/
function queueLinksJobs( $titles ) {
$fname = 'Job::queueLinksJobs';
wfProfileIn( $fname );
- foreach ( $titles as $title ) {
- $job = new Job( 'refreshLinks', $title );
- $job->insert();
+ $batchSize = 100;
+ for( $i = 0; $i < count( $titles ); $i += $batchSize ) {
+ $batch = array_slice( $titles, $i, $batchSize, true );
+ $jobs = array();
+ foreach( $batch as $title ) {
+ $jobs[] = new Job( 'refreshLinks', $title );
+ }
+ Job::batchInsert( $jobs );
}
wfProfileOut( $fname );
}
$fname = 'Job::pop';
wfProfileIn( $fname );
- // First check to see if there are any jobs in the slave DB
$dbr =& wfGetDB( DB_SLAVE );
- $id = $dbr->selectField( 'job', 'job_id', '', $fname, array( 'LIMIT' => 1 ) );
- if ( $id === false ) {
+
+ // Get a job from the slave
+ $row = $dbr->selectRow( 'job', '*', '', $fname,
+ array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 )
+ );
+
+ if ( $row === false ) {
wfProfileOut( $fname );
return false;
}
- // Pop an item off the front of the queue
- // Method due to Domas, may not work on all DBMSes
+ // Try to delete it from the master
$dbw =& wfGetDB( DB_MASTER );
- $dbw->immediateBegin();
- $jobTable = $dbw->tableName( 'job' );
- $dbw->query( "DELETE FROM $jobTable WHERE " .
- '(job_cmd = @job_cmd := job_cmd) AND ' .
- '(job_namespace = @job_namespace := job_namespace) AND ' .
- '(job_title = @job_title := job_title) AND ' .
- '(job_params = @job_params := job_params) ' .
- 'LIMIT 1', $fname );
+ $dbw->delete( 'job', array( 'job_id' => $row->job_id ), $fname );
$affected = $dbw->affectedRows();
- // Commit now before 100 other threads pile up behind us
$dbw->immediateCommit();
- if ( !$affected ) {
- wfProfileOut( $fname );
- return false;
- }
- $res = $dbw->query( "SELECT @job_cmd, @job_namespace, @job_title, @job_params", $fname );
- $row = $dbw->fetchRow( $res );
- if ( !$row ) {
- wfProfileOut( $fname );
- return false;
+ if ( !$affected ) {
+ // Failed, someone else beat us to it
+ // Try getting a random row
+ $row = $dbw->selectRow( 'job', array( 'MIN(job_id) as minjob',
+ 'MAX(job_id) as maxjob' ), '', $fname );
+ if ( $row === false || is_null( $row->minjob ) || is_null( $row->maxjob ) ) {
+ // No jobs to get
+ wfProfileOut( $fname );
+ return false;
+ }
+ // Get the random row
+ $row = $dbw->selectRow( 'job', '*',
+ array( 'job_id' => mt_rand( $row->minjob, $row->maxjob ) ), $fname );
+ if ( $row === false ) {
+ // Random job gone before we got the chance to select it
+ // Give up
+ wfProfileOut( $fname );
+ return false;
+ }
+ // Delete the random row
+ $dbw->delete( 'job', array( 'job_id' => $row->job_id ), $fname );
+ $affected = $dbw->affectedRows();
+ $dbw->immediateCommit();
+
+ if ( !$affected ) {
+ // Random job gone before we exclusively deleted it
+ // Give up
+ wfProfileOut( $fname );
+ return false;
+ }
}
-
- $command = $row['@job_cmd'];
- $namespace = $row['@job_namespace'];
- $dbkey = $row['@job_title'];
+
+ // 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.
+ $namespace = $row->job_namespace;
+ $dbkey = $row->job_title;
$title = Title::makeTitleSafe( $namespace, $dbkey );
- $params = $row['@job_params'];
- $job = new Job( $command, $title, $params );
+ $job = new Job( $row->job_cmd, $title, $row->job_params, $row->job_id );
+
+ // Remove any duplicates it may have later in the queue
+ $dbw->delete( 'job', $job->insertFields(), $fname );
+
wfProfileOut( $fname );
return $job;
}
* Non-static functions
*------------------------------------------------------------------------*/
- function Job( $command, $title, $params = '' ) {
+ function Job( $command, $title, $params = '', $id = 0 ) {
$this->command = $command;
$this->title = $title;
$this->params = $params;
+ $this->id = $id;
// A bit of premature generalisation
// Oh well, the whole class is premature generalisation really
$this->removeDuplicates = true;
}
+ /**
+ * Insert a single job into the queue.
+ */
function insert() {
$fname = 'Job::insert';
+
+ $fields = $this->insertFields();
- $fields = array(
+ $dbw =& wfGetDB( DB_MASTER );
+
+ if ( $this->removeDuplicates ) {
+ $res = $dbw->select( 'job', array( '1' ), $fields, $fname );
+ if ( $dbw->numRows( $res ) ) {
+ return;
+ }
+ }
+ $fields['job_id'] = $dbw->nextSequenceValue( 'job_job_id_seq' );
+ $dbw->insert( 'job', $fields, $fname );
+ }
+
+ protected function insertFields() {
+ return array(
'job_cmd' => $this->command,
'job_namespace' => $this->title->getNamespace(),
'job_title' => $this->title->getDBkey(),
'job_params' => $this->params
);
-
- $dbw =& wfGetDB( DB_MASTER );
+ }
+
+ /**
+ * 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 ) {
+ $fname = __CLASS__ . '::' . __FUNCTION__;
- if ( $this->removeDuplicates ) {
- $dbw->delete( 'job', $fields, $fname );
+ if( count( $jobs ) ) {
+ $dbw = wfGetDB( DB_MASTER );
+ $dbw->begin();
+ foreach( $jobs as $job ) {
+ $rows[] = $job->insertFields();
+ }
+ $dbw->insert( 'job', $rows, $fname, 'IGNORE' );
+ $dbw->immediateCommit();
}
- $fields['job_id'] = $dbw->nextSequenceValue( 'job_job_id_seq' );
- $dbw->insert( 'job', $fields, $fname );
}
/**
$retval = $this->refreshLinks();
break;
default:
- $this->error = "Invalid job type {$this->command}, ignoring";
- wfDebug( $this->error . "\n" );
- $retval = false;
+ $retval = true;
+ if( wfRunHooks( 'RunUnknownJob', array( &$this, &$retval ) ) ) {
+ $this->error = "Invalid job type {$this->command}, ignoring";
+ wfDebug( $this->error . "\n" );
+ $retval = false;
+ } else {
+ $retval = true;
+ }
}
wfProfileOut( $fname );
return $retval;
*/
function refreshLinks() {
global $wgParser;
-
+ $fname = 'Job::refreshLinks';
+ wfProfileIn( $fname );
+
+ # FIXME: $dbw never used.
$dbw =& wfGetDB( DB_MASTER );
$linkCache =& LinkCache::singleton();
if ( is_null( $this->title ) ) {
$this->error = "refreshLinks: Invalid title";
+ wfProfileOut( $fname );
return false;
}
$revision = Revision::newFromTitle( $this->title );
if ( !$revision ) {
$this->error = 'refreshLinks: Article not found "' . $this->title->getPrefixedDBkey() . '"';
+ wfProfileOut( $fname );
return false;
}
+ wfProfileIn( "$fname-parse" );
$options = new ParserOptions;
$parserOutput = $wgParser->parse( $revision->getText(), $this->title, $options, true, true, $revision->getId() );
- $update = new LinksUpdate( $this->title, $parserOutput );
+ wfProfileOut( "$fname-parse" );
+ wfProfileIn( "$fname-update" );
+ $update = new LinksUpdate( $this->title, $parserOutput, false );
$update->doUpdate();
+ wfProfileOut( "$fname-update" );
+ wfProfileOut( $fname );
return true;
}
return $s;
} else {
return "{$this->command} {$this->params}";
- }
+ }
}
function getLastError() {
return $this->error;
}
}
+?>