diff options
Diffstat (limited to 'includes/job')
-rw-r--r-- | includes/job/DoubleRedirectJob.php | 10 | ||||
-rw-r--r-- | includes/job/JobQueue.php | 74 | ||||
-rw-r--r-- | includes/job/RefreshLinksJob.php | 2 | ||||
-rw-r--r-- | includes/job/UploadFromUrlJob.php | 61 |
4 files changed, 106 insertions, 41 deletions
diff --git a/includes/job/DoubleRedirectJob.php b/includes/job/DoubleRedirectJob.php index 3b4b0188..d7991f5e 100644 --- a/includes/job/DoubleRedirectJob.php +++ b/includes/job/DoubleRedirectJob.php @@ -13,13 +13,17 @@ */ class DoubleRedirectJob extends Job { var $reason, $redirTitle, $destTitleText; + + /** + * @var User + */ static $user; /** * Insert jobs into the job queue to fix redirects to the given title * @param $reason String: the reason for the fix, see message double-redirect-fixed-<reason> * @param $redirTitle Title: the title which has changed, redirects pointing to this title are fixed - * @param $destTitle Not used + * @param $destTitle bool Not used */ public static function fixRedirects( $reason, $redirTitle, $destTitle = false ) { # Need to use the master to get the redirect table updated in the same transaction @@ -53,6 +57,7 @@ class DoubleRedirectJob extends Job { } Job::batchInsert( $jobs ); } + function __construct( $title, $params = false, $id = 0 ) { parent::__construct( 'fixDoubleRedirect', $title, $params, $id ); $this->reason = $params['reason']; @@ -128,6 +133,9 @@ class DoubleRedirectJob extends Job { /** * Get the final destination of a redirect + * + * @param $title Title + * * @return false if the specified title is not a redirect, or if it is a circular redirect */ public static function getFinalDestination( $title ) { diff --git a/includes/job/JobQueue.php b/includes/job/JobQueue.php index 8eec8215..0d917ba3 100644 --- a/includes/job/JobQueue.php +++ b/includes/job/JobQueue.php @@ -16,8 +16,13 @@ if ( !defined( 'MEDIAWIKI' ) ) { * @ingroup JobQueue */ abstract class Job { + + /** + * @var Title + */ + var $title; + var $command, - $title, $params, $id, $removeDuplicates, @@ -41,21 +46,28 @@ abstract class Job { * 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 * runners. + * + * @param $type string + * + * @return Job */ static function pop_type( $type ) { wfProfilein( __METHOD__ ); $dbw = wfGetDB( DB_MASTER ); + $dbw->begin(); + $row = $dbw->selectRow( 'job', '*', array( 'job_cmd' => $type ), __METHOD__, - array( 'LIMIT' => 1 ) + array( 'LIMIT' => 1, 'FOR UPDATE' ) ); if ( $row === false ) { + $dbw->commit(); wfProfileOut( __METHOD__ ); return false; } @@ -63,20 +75,21 @@ abstract class Job { /* Ensure we "own" this row */ $dbw->delete( 'job', array( 'job_id' => $row->job_id ), __METHOD__ ); $affected = $dbw->affectedRows(); + $dbw->commit(); if ( $affected == 0 ) { wfProfileOut( __METHOD__ ); return false; } + wfIncrStats( 'job-pop' ); $namespace = $row->job_namespace; $dbkey = $row->job_title; $title = Title::makeTitleSafe( $namespace, $dbkey ); $job = Job::factory( $row->job_cmd, $title, Job::extractBlob( $row->job_params ), $row->job_id ); - $dbw->delete( 'job', $job->insertFields(), __METHOD__ ); - $dbw->commit(); + $job->removeDuplicates(); wfProfileOut( __METHOD__ ); return $job; @@ -89,6 +102,7 @@ abstract class Job { * @return Job or false if there's no jobs */ static function pop( $offset = 0 ) { + global $wgJobTypesExcludedFromDefaultQueue; wfProfileIn( __METHOD__ ); $dbr = wfGetDB( DB_SLAVE ); @@ -99,17 +113,27 @@ abstract class Job { NB: If random fetch previously was used, offset will always be ahead of few entries */ + $conditions = array(); + if ( count( $wgJobTypesExcludedFromDefaultQueue ) != 0 ) { + foreach ( $wgJobTypesExcludedFromDefaultQueue as $cmdType ) { + $conditions[] = "job_cmd != " . $dbr->addQuotes( $cmdType ); + } + } + $offset = intval( $offset ); + $options = array( 'ORDER BY' => 'job_id', 'USE INDEX' => 'PRIMARY' ); - $row = $dbr->selectRow( 'job', '*', "job_id >= ${offset}", __METHOD__, - array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ) ); + $row = $dbr->selectRow( 'job', '*', + array_merge( $conditions, array( "job_id >= $offset" ) ), + __METHOD__, + $options + ); // 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 :) // if ( $row === false ) { if ( $offset != 0 ) { - $row = $dbr->selectRow( 'job', '*', '', __METHOD__, - array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ) ); + $row = $dbr->selectRow( 'job', '*', $conditions, __METHOD__, $options ); } if ( $row === false ) { @@ -158,16 +182,14 @@ abstract class Job { // 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. + wfIncrStats( 'job-pop' ); $namespace = $row->job_namespace; $dbkey = $row->job_title; $title = Title::makeTitleSafe( $namespace, $dbkey ); $job = Job::factory( $row->job_cmd, $title, Job::extractBlob( $row->job_params ), $row->job_id ); // Remove any duplicates it may have later in the queue - // Deadlock prone section - $dbw->begin(); - $dbw->delete( 'job', $job->insertFields(), __METHOD__ ); - $dbw->commit(); + $job->removeDuplicates(); wfProfileOut( __METHOD__ ); return $job; @@ -272,6 +294,12 @@ abstract class Job { * Non-static functions *------------------------------------------------------------------------*/ + /** + * @param $command + * @param $title + * @param $params array + * @param int $id + */ function __construct( $command, $title, $params = false, $id = 0 ) { $this->command = $command; $this->title = $title; @@ -298,6 +326,7 @@ abstract class Job { return; } } + wfIncrStats( 'job-insert' ); return $dbw->insert( 'job', $fields, __METHOD__ ); } @@ -312,6 +341,27 @@ abstract class Job { ); } + /** + * 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 ); + } + } + function toString() { $paramString = ''; if ( $this->params ) { diff --git a/includes/job/RefreshLinksJob.php b/includes/job/RefreshLinksJob.php index cc91fa81..910f0c58 100644 --- a/includes/job/RefreshLinksJob.php +++ b/includes/job/RefreshLinksJob.php @@ -119,7 +119,7 @@ class RefreshLinksJob2 extends Job { $update = new LinksUpdate( $title, $parserOutput, false ); $update->doUpdate(); wfProfileOut( __METHOD__.'-update' ); - wfWaitForSlaves( 5 ); + wfWaitForSlaves(); } wfProfileOut( __METHOD__ ); diff --git a/includes/job/UploadFromUrlJob.php b/includes/job/UploadFromUrlJob.php index 63166ef9..3f915245 100644 --- a/includes/job/UploadFromUrlJob.php +++ b/includes/job/UploadFromUrlJob.php @@ -8,7 +8,7 @@ /** * Job for asynchronous upload-by-url. - * + * * This job is in fact an interface to UploadFromUrl, which is designed such * that it does not require any globals. If it does, fix it elsewhere, do not * add globals in here. @@ -17,8 +17,15 @@ */ class UploadFromUrlJob extends Job { const SESSION_KEYNAME = 'wsUploadFromUrlJobData'; - + + /** + * @var UploadFromUrl + */ public $upload; + + /** + * @var User + */ protected $user; public function __construct( $title, $params, $id = 0 ) { @@ -28,20 +35,20 @@ class UploadFromUrlJob extends Job { public function run() { # Initialize this object and the upload object $this->upload = new UploadFromUrl(); - $this->upload->initialize( - $this->title->getText(), + $this->upload->initialize( + $this->title->getText(), $this->params['url'], false ); $this->user = User::newFromName( $this->params['userName'] ); - + # Fetch the file $status = $this->upload->fetchFile(); if ( !$status->isOk() ) { $this->leaveMessage( $status ); return true; } - + # Verify upload $result = $this->upload->verifyUpload(); if ( $result['status'] != UploadBase::OK ) { @@ -49,17 +56,17 @@ class UploadFromUrlJob extends Job { $this->leaveMessage( $status ); return true; } - + # Check warnings if ( !$this->params['ignoreWarnings'] ) { $warnings = $this->upload->checkWarnings(); - if ( $warnings ) { + if ( $warnings ) { wfSetupSession( $this->params['sessionId'] ); - + if ( $this->params['leaveMessage'] ) { - $this->user->leaveUserMessage( + $this->user->leaveUserMessage( wfMsg( 'upload-warning-subj' ), - wfMsg( 'upload-warning-msg', + wfMsg( 'upload-warning-msg', $this->params['sessionKey'], $this->params['url'] ) ); @@ -67,17 +74,17 @@ class UploadFromUrlJob extends Job { $this->storeResultInSession( 'Warning', 'warnings', $warnings ); } - + # Stash the upload in the session $this->upload->stashSession( $this->params['sessionKey'] ); session_write_close(); - + return true; } } - + # Perform the upload - $status = $this->upload->performUpload( + $status = $this->upload->performUpload( $this->params['comment'], $this->params['pageText'], $this->params['watch'], @@ -85,47 +92,47 @@ class UploadFromUrlJob extends Job { ); $this->leaveMessage( $status ); return true; - + } - + /** * Leave a message on the user talk page or in the session according to * $params['leaveMessage']. - * + * * @param $status Status */ protected function leaveMessage( $status ) { if ( $this->params['leaveMessage'] ) { if ( $status->isGood() ) { $this->user->leaveUserMessage( wfMsg( 'upload-success-subj' ), - wfMsg( 'upload-success-msg', + wfMsg( 'upload-success-msg', $this->upload->getTitle()->getText(), - $this->params['url'] + $this->params['url'] ) ); } else { $this->user->leaveUserMessage( wfMsg( 'upload-failure-subj' ), - wfMsg( 'upload-failure-msg', + wfMsg( 'upload-failure-msg', $status->getWikiText(), $this->params['url'] ) ); } } else { - wfSetupSession( $this->params['sessionId'] ); + wfSetupSession( $this->params['sessionId'] ); if ( $status->isOk() ) { - $this->storeResultInSession( 'Success', + $this->storeResultInSession( 'Success', 'filename', $this->upload->getLocalFile()->getName() ); } else { $this->storeResultInSession( 'Failure', 'errors', $status->getErrorsArray() ); } - session_write_close(); + session_write_close(); } } /** * Store a result in the session data. Note that the caller is responsible * for appropriate session_start and session_write_close calls. - * + * * @param $result String: the result (Success|Warning|Failure) * @param $dataKey String: the key of the extra data * @param $dataValue Mixed: the extra data itself @@ -135,7 +142,7 @@ class UploadFromUrlJob extends Job { $session['result'] = $result; $session[$dataKey] = $dataValue; } - + /** * Initialize the session data. Sets the intial result to queued. */ @@ -143,7 +150,7 @@ class UploadFromUrlJob extends Job { $session =& self::getSessionData( $this->params['sessionKey'] ); $$session['result'] = 'Queued'; } - + public static function &getSessionData( $key ) { if ( !isset( $_SESSION[self::SESSION_KEYNAME][$key] ) ) { $_SESSION[self::SESSION_KEYNAME][$key] = array(); |