3 * @defgroup JobQueue JobQueue
6 if ( !defined( 'MEDIAWIKI' ) ) {
7 die( "This file is part of MediaWiki, it is not a valid entry point\n" );
11 * Class to both describe a background job and handle jobs.
23 /*-------------------------------------------------------------------------
25 *------------------------------------------------------------------------*/
29 * @return boolean success
31 abstract function run();
33 /*-------------------------------------------------------------------------
35 *------------------------------------------------------------------------*/
38 * @deprecated use LinksUpdate::queueRecursiveJobs()
41 * static function queueLinksJobs( $titles ) {}
45 * Pop a job of a certain type. This tries less hard than pop() to
46 * actually find a job; it may be adversely affected by concurrent job
49 static function pop_type( $type ) {
50 wfProfilein( __METHOD__ );
52 $dbw = wfGetDB( DB_MASTER );
54 $row = $dbw->selectRow(
57 array( 'job_cmd' => $type ),
62 if ( $row === false ) {
63 wfProfileOut( __METHOD__ );
67 /* Ensure we "own" this row */
68 $dbw->delete( 'job', array( 'job_id' => $row->job_id ), __METHOD__ );
69 $affected = $dbw->affectedRows();
71 if ( $affected == 0 ) {
72 wfProfileOut( __METHOD__ );
76 $namespace = $row->job_namespace;
77 $dbkey = $row->job_title;
78 $title = Title::makeTitleSafe( $namespace, $dbkey );
79 $job = Job::factory( $row->job_cmd, $title, Job::extractBlob( $row->job_params ), $row->job_id );
81 $dbw->delete( 'job', $job->insertFields(), __METHOD__ );
84 wfProfileOut( __METHOD__ );
89 * Pop a job off the front of the queue
91 * @param $offset Integer: Number of jobs to skip
92 * @return Job or false if there's no jobs
94 static function pop( $offset = 0 ) {
95 wfProfileIn( __METHOD__ );
97 $dbr = wfGetDB( DB_SLAVE );
99 /* Get a job from the slave, start with an offset,
100 scan full set afterwards, avoid hitting purged rows
102 NB: If random fetch previously was used, offset
103 will always be ahead of few entries
106 $row = $dbr->selectRow( 'job', '*', "job_id >= ${offset}", __METHOD__,
107 array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ) );
109 // Refetching without offset is needed as some of job IDs could have had delayed commits
110 // and have lower IDs than jobs already executed, blame concurrency :)
112 if ( $row === false ) {
113 if ( $offset != 0 ) {
114 $row = $dbr->selectRow( 'job', '*', '', __METHOD__,
115 array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ) );
118 if ( $row === false ) {
119 wfProfileOut( __METHOD__ );
123 $offset = $row->job_id;
125 // Try to delete it from the master
126 $dbw = wfGetDB( DB_MASTER );
127 $dbw->delete( 'job', array( 'job_id' => $row->job_id ), __METHOD__ );
128 $affected = $dbw->affectedRows();
132 // Failed, someone else beat us to it
133 // Try getting a random row
134 $row = $dbw->selectRow( 'job', array( 'MIN(job_id) as minjob',
135 'MAX(job_id) as maxjob' ), '1=1', __METHOD__ );
136 if ( $row === false || is_null( $row->minjob ) || is_null( $row->maxjob ) ) {
138 wfProfileOut( __METHOD__ );
141 // Get the random row
142 $row = $dbw->selectRow( 'job', '*',
143 'job_id >= ' . mt_rand( $row->minjob, $row->maxjob ), __METHOD__ );
144 if ( $row === false ) {
145 // Random job gone before we got the chance to select it
147 wfProfileOut( __METHOD__ );
150 // Delete the random row
151 $dbw->delete( 'job', array( 'job_id' => $row->job_id ), __METHOD__ );
152 $affected = $dbw->affectedRows();
156 // Random job gone before we exclusively deleted it
158 wfProfileOut( __METHOD__ );
163 // If execution got to here, there's a row in $row that has been deleted from the database
164 // by this thread. Hence the concurrent pop was successful.
165 $namespace = $row->job_namespace;
166 $dbkey = $row->job_title;
167 $title = Title::makeTitleSafe( $namespace, $dbkey );
168 $job = Job::factory( $row->job_cmd, $title, Job::extractBlob( $row->job_params ), $row->job_id );
170 // Remove any duplicates it may have later in the queue
171 // Deadlock prone section
173 $dbw->delete( 'job', $job->insertFields(), __METHOD__ );
176 wfProfileOut( __METHOD__ );
181 * Create the appropriate object to handle a specific job
183 * @param $command String: Job command
184 * @param $title Title: Associated title
185 * @param $params Array: Job parameters
186 * @param $id Int: Job identifier
189 static function factory( $command, $title, $params = false, $id = 0 ) {
190 global $wgJobClasses;
191 if( isset( $wgJobClasses[$command] ) ) {
192 $class = $wgJobClasses[$command];
193 return new $class( $title, $params, $id );
195 throw new MWException( "Invalid job command `{$command}`" );
198 static function makeBlob( $params ) {
199 if ( $params !== false ) {
200 return serialize( $params );
206 static function extractBlob( $blob ) {
207 if ( (string)$blob !== '' ) {
208 return unserialize( $blob );
215 * Batch-insert a group of jobs into the queue.
216 * This will be wrapped in a transaction with a forced commit.
218 * This may add duplicate at insert time, but they will be
219 * removed later on, when the first one is popped.
221 * @param $jobs array of Job objects
223 static function batchInsert( $jobs ) {
224 if( !count( $jobs ) ) {
227 $dbw = wfGetDB( DB_MASTER );
229 foreach( $jobs as $job ) {
230 $rows[] = $job->insertFields();
231 if ( count( $rows ) >= 50 ) {
232 # Do a small transaction to avoid slave lag
234 $dbw->insert( 'job', $rows, __METHOD__, 'IGNORE' );
241 $dbw->insert( 'job', $rows, __METHOD__, 'IGNORE' );
246 /*-------------------------------------------------------------------------
247 * Non-static functions
248 *------------------------------------------------------------------------*/
250 function __construct( $command, $title, $params = false, $id = 0 ) {
251 $this->command = $command;
252 $this->title = $title;
253 $this->params = $params;
256 // A bit of premature generalisation
257 // Oh well, the whole class is premature generalisation really
258 $this->removeDuplicates = true;
262 * Insert a single job into the queue.
265 $fields = $this->insertFields();
267 $dbw = wfGetDB( DB_MASTER );
269 if ( $this->removeDuplicates ) {
270 $res = $dbw->select( 'job', array( '1' ), $fields, __METHOD__ );
271 if ( $dbw->numRows( $res ) ) {
275 $dbw->insert( 'job', $fields, __METHOD__ );
278 protected function insertFields() {
279 $dbw = wfGetDB( DB_MASTER );
281 'job_id' => $dbw->nextSequenceValue( 'job_job_id_seq' ),
282 'job_cmd' => $this->command,
283 'job_namespace' => $this->title->getNamespace(),
284 'job_title' => $this->title->getDBkey(),
285 'job_params' => Job::makeBlob( $this->params )
289 function toString() {
291 if ( $this->params ) {
292 foreach ( $this->params as $key => $value ) {
293 if ( $paramString != '' ) {
296 $paramString .= "$key=$value";
300 if ( is_object( $this->title ) ) {
301 $s = "{$this->command} " . $this->title->getPrefixedDBkey();
302 if ( $paramString !== '' ) {
303 $s .= ' ' . $paramString;
307 return "{$this->command} $paramString";
311 protected function setLastError( $error ) {
312 $this->error = $error;
315 function getLastError() {