5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @defgroup JobQueue JobQueue
22 * @author Aaron Schulz
26 * Class to handle enqueueing and running of background jobs
31 abstract class JobQueue
{
32 protected $wiki; // string; wiki ID
33 protected $type; // string; job type
34 protected $order; // string; job priority for pop()
36 const QoS_Atomic
= 1; // integer; "all-or-nothing" job insertions
39 * @param $params array
41 protected function __construct( array $params ) {
42 $this->wiki
= $params['wiki'];
43 $this->type
= $params['type'];
44 $this->order
= isset( $params['order'] ) ?
$params['order'] : 'random';
48 * Get a job queue object of the specified type.
50 * class : What job class to use (determines job type)
51 * wiki : wiki ID of the wiki the jobs are for (defaults to current wiki)
52 * type : The name of the job types this queue handles
53 * order : Order that pop() selects jobs, either "timestamp" or "random".
54 * If "timestamp" is used, the queue will effectively be FIFO. Note that
55 * pop() will not be exactly FIFO, and even if it was, job completion would
56 * not appear to be exactly FIFO since jobs can take different times to finish.
57 * If "random" is used, pop() will pick jobs in random order. This might be
58 * useful for improving concurrency depending on the queue storage medium.
60 * @param $params array
64 final public static function factory( array $params ) {
65 $class = $params['class'];
66 if ( !MWInit
::classExists( $class ) ) {
67 throw new MWException( "Invalid job queue class '$class'." );
69 $obj = new $class( $params );
70 if ( !( $obj instanceof self
) ) {
71 throw new MWException( "Class '$class' is not a " . __CLASS__
. " class." );
77 * @return string Wiki ID
79 final public function getWiki() {
84 * @return string Job type that this queue handles
86 final public function getType() {
91 * @return bool Quickly check if the queue is empty
93 final public function isEmpty() {
94 wfProfileIn( __METHOD__
);
95 $res = $this->doIsEmpty();
96 wfProfileOut( __METHOD__
);
101 * @see JobQueue::isEmpty()
104 abstract protected function doIsEmpty();
107 * Push a batch of jobs into the queue
109 * @param $jobs array List of Jobs
110 * @param $flags integer Bitfield (supports JobQueue::QoS_Atomic)
113 final public function batchPush( array $jobs, $flags = 0 ) {
114 foreach ( $jobs as $job ) {
115 if ( $job->getType() !== $this->type
) {
116 throw new MWException( "Got '{$job->getType()}' job; expected '{$this->type}'." );
119 wfProfileIn( __METHOD__
);
120 $ok = $this->doBatchPush( $jobs, $flags );
122 wfIncrStats( 'job-insert', count( $jobs ) );
124 wfProfileOut( __METHOD__
);
129 * @see JobQueue::batchPush()
132 abstract protected function doBatchPush( array $jobs, $flags );
135 * Pop a job off of the queue
137 * @return Job|bool Returns false on failure
139 final public function pop() {
140 wfProfileIn( __METHOD__
);
141 $job = $this->doPop();
143 wfIncrStats( 'job-pop' );
145 wfProfileOut( __METHOD__
);
150 * @see JobQueue::pop()
153 abstract protected function doPop();
156 * Acknowledge that a job was completed
161 final public function ack( Job
$job ) {
162 if ( $job->getType() !== $this->type
) {
163 throw new MWException( "Got '{$job->getType()}' job; expected '{$this->type}'." );
165 wfProfileIn( __METHOD__
);
166 $ok = $this->doAck( $job );
167 wfProfileOut( __METHOD__
);
172 * @see JobQueue::ack()
175 abstract protected function doAck( Job
$job );
178 * Wait for any slaves or backup servers to catch up
182 final public function waitForBackups() {
183 wfProfileIn( __METHOD__
);
184 $this->doWaitForBackups();
185 wfProfileOut( __METHOD__
);
189 * @see JobQueue::waitForBackups()
192 protected function doWaitForBackups() {}