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 * Quickly check if the queue is empty.
92 * Queue classes should use caching if they are any slower without memcached.
96 final public function isEmpty() {
97 wfProfileIn( __METHOD__
);
98 $res = $this->doIsEmpty();
99 wfProfileOut( __METHOD__
);
104 * @see JobQueue::isEmpty()
107 abstract protected function doIsEmpty();
110 * Push a batch of jobs into the queue
112 * @param $jobs array List of Jobs
113 * @param $flags integer Bitfield (supports JobQueue::QoS_Atomic)
116 final public function batchPush( array $jobs, $flags = 0 ) {
117 foreach ( $jobs as $job ) {
118 if ( $job->getType() !== $this->type
) {
119 throw new MWException( "Got '{$job->getType()}' job; expected '{$this->type}'." );
122 wfProfileIn( __METHOD__
);
123 $ok = $this->doBatchPush( $jobs, $flags );
125 wfIncrStats( 'job-insert', count( $jobs ) );
127 wfProfileOut( __METHOD__
);
132 * @see JobQueue::batchPush()
135 abstract protected function doBatchPush( array $jobs, $flags );
138 * Pop a job off of the queue
140 * @return Job|bool Returns false on failure
142 final public function pop() {
143 wfProfileIn( __METHOD__
);
144 $job = $this->doPop();
146 wfIncrStats( 'job-pop' );
148 wfProfileOut( __METHOD__
);
153 * @see JobQueue::pop()
156 abstract protected function doPop();
159 * Acknowledge that a job was completed
164 final public function ack( Job
$job ) {
165 if ( $job->getType() !== $this->type
) {
166 throw new MWException( "Got '{$job->getType()}' job; expected '{$this->type}'." );
168 wfProfileIn( __METHOD__
);
169 $ok = $this->doAck( $job );
170 wfProfileOut( __METHOD__
);
175 * @see JobQueue::ack()
178 abstract protected function doAck( Job
$job );
181 * Wait for any slaves or backup servers to catch up
185 final public function waitForBackups() {
186 wfProfileIn( __METHOD__
);
187 $this->doWaitForBackups();
188 wfProfileOut( __METHOD__
);
192 * @see JobQueue::waitForBackups()
195 protected function doWaitForBackups() {}