4637bd2b229ad78026ba48ab3b8119915003ec62
[lhc/web/wiklou.git] / includes / job / JobQueue.php
1 <?php
2 /**
3 * Job queue base code.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @defgroup JobQueue JobQueue
22 * @author Aaron Schulz
23 */
24
25 /**
26 * Class to handle enqueueing and running of background jobs
27 *
28 * @ingroup JobQueue
29 * @since 1.20
30 */
31 abstract class JobQueue {
32 protected $wiki; // string; wiki ID
33 protected $type; // string; job type
34 protected $order; // string; job priority for pop()
35
36 const QoS_Atomic = 1; // integer; "all-or-nothing" job insertions
37
38 /**
39 * @param $params array
40 */
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';
45 }
46
47 /**
48 * Get a job queue object of the specified type.
49 * $params includes:
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.
59 *
60 * @param $params array
61 * @return JobQueue
62 * @throws MWException
63 */
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'." );
68 }
69 $obj = new $class( $params );
70 if ( !( $obj instanceof self ) ) {
71 throw new MWException( "Class '$class' is not a " . __CLASS__ . " class." );
72 }
73 return $obj;
74 }
75
76 /**
77 * @return string Wiki ID
78 */
79 final public function getWiki() {
80 return $this->wiki;
81 }
82
83 /**
84 * @return string Job type that this queue handles
85 */
86 final public function getType() {
87 return $this->type;
88 }
89
90 /**
91 * @return bool Quickly check if the queue is empty
92 */
93 final public function isEmpty() {
94 wfProfileIn( __METHOD__ );
95 $res = $this->doIsEmpty();
96 wfProfileOut( __METHOD__ );
97 return $res;
98 }
99
100 /**
101 * @see JobQueue::isEmpty()
102 * @return bool
103 */
104 abstract protected function doIsEmpty();
105
106 /**
107 * Push a batch of jobs into the queue
108 *
109 * @param $jobs array List of Jobs
110 * @param $flags integer Bitfield (supports JobQueue::QoS_Atomic)
111 * @return bool
112 */
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}'." );
117 }
118 }
119 wfProfileIn( __METHOD__ );
120 $ok = $this->doBatchPush( $jobs, $flags );
121 if ( $ok ) {
122 wfIncrStats( 'job-insert', count( $jobs ) );
123 }
124 wfProfileOut( __METHOD__ );
125 return $ok;
126 }
127
128 /**
129 * @see JobQueue::batchPush()
130 * @return bool
131 */
132 abstract protected function doBatchPush( array $jobs, $flags );
133
134 /**
135 * Pop a job off of the queue
136 *
137 * @return Job|bool Returns false on failure
138 */
139 final public function pop() {
140 wfProfileIn( __METHOD__ );
141 $job = $this->doPop();
142 if ( $job ) {
143 wfIncrStats( 'job-pop' );
144 }
145 wfProfileOut( __METHOD__ );
146 return $job;
147 }
148
149 /**
150 * @see JobQueue::pop()
151 * @return Job
152 */
153 abstract protected function doPop();
154
155 /**
156 * Acknowledge that a job was completed
157 *
158 * @param $job Job
159 * @return bool
160 */
161 final public function ack( Job $job ) {
162 if ( $job->getType() !== $this->type ) {
163 throw new MWException( "Got '{$job->getType()}' job; expected '{$this->type}'." );
164 }
165 wfProfileIn( __METHOD__ );
166 $ok = $this->doAck( $job );
167 wfProfileOut( __METHOD__ );
168 return $ok;
169 }
170
171 /**
172 * @see JobQueue::ack()
173 * @return bool
174 */
175 abstract protected function doAck( Job $job );
176
177 /**
178 * Wait for any slaves or backup servers to catch up
179 *
180 * @return void
181 */
182 final public function waitForBackups() {
183 wfProfileIn( __METHOD__ );
184 $this->doWaitForBackups();
185 wfProfileOut( __METHOD__ );
186 }
187
188 /**
189 * @see JobQueue::waitForBackups()
190 * @return void
191 */
192 protected function doWaitForBackups() {}
193 }