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 * @author Aaron Schulz
25 * Class to handle enqueueing of background jobs
32 protected static $instances = array();
34 protected $wiki; // string; wiki ID
36 const TYPE_DEFAULT
= 1; // integer; job not in $wgJobTypesExcludedFromDefaultQueue
37 const TYPE_ANY
= 2; // integer; any job
40 * @param $wiki string Wiki ID
42 protected function __construct( $wiki ) {
47 * @param $wiki string Wiki ID
48 * @return JobQueueGroup
50 public static function singleton( $wiki = false ) {
51 $wiki = ( $wiki === false ) ?
wfWikiID() : $wiki;
52 if ( !isset( self
::$instances[$wiki] ) ) {
53 self
::$instances[$wiki] = new self( $wiki );
55 return self
::$instances[$wiki];
60 * @return JobQueue Job queue object for a given queue type
62 public function get( $type ) {
63 global $wgJobTypeConf;
65 $conf = array( 'wiki' => $this->wiki
, 'type' => $type );
66 if ( isset( $wgJobTypeConf[$type] ) ) {
67 $conf = $conf +
$wgJobTypeConf[$type];
69 $conf = $conf +
$wgJobTypeConf['default'];
72 return JobQueue
::factory( $conf );
76 * Insert jobs into the respective queues of with the belong.
77 * This inserts the jobs into the queue specified by $wgJobTypeConf.
79 * @param $jobs Job|array A single Job or a list of Jobs
83 public function push( $jobs ) {
84 $jobs = is_array( $jobs ) ?
$jobs : array( $jobs );
86 $jobsByType = array(); // (job type => list of jobs)
87 foreach ( $jobs as $job ) {
88 if ( $job instanceof Job
) {
89 $jobsByType[$job->getType()][] = $job;
91 throw new MWException( "Attempted to push a non-Job object into a queue." );
96 foreach ( $jobsByType as $type => $jobs ) {
97 if ( !$this->get( $type )->batchPush( $jobs ) ) {
106 * Pop a job off one of the job queues
108 * @param $type integer JobQueueGroup::TYPE_* constant
109 * @return Job|bool Returns false on failure
111 public function pop( $type = self
::TYPE_DEFAULT
) {
112 $types = ( $type == self
::TYPE_DEFAULT
)
113 ?
$this->getDefaultQueueTypes()
114 : $this->getQueueTypes();
115 shuffle( $types ); // avoid starvation
117 foreach ( $types as $type ) { // for each queue...
118 $job = $this->get( $type )->pop();
120 return $job; // found
124 return false; // no jobs found
128 * Acknowledge that a job was completed
133 public function ack( Job
$job ) {
134 return $this->get( $job->getType() )->ack( $job );
138 * Register the "root job" of a given job into the queue for de-duplication.
139 * This should only be called right *after* all the new jobs have been inserted.
144 public function deduplicateRootJob( Job
$job ) {
145 return $this->get( $job->getType() )->deduplicateRootJob( $job );
149 * Get the list of queue types
151 * @return array List of strings
153 public function getQueueTypes() {
154 global $wgJobClasses;
156 return array_keys( $wgJobClasses );
160 * Get the list of default queue types
162 * @return array List of strings
164 public function getDefaultQueueTypes() {
165 global $wgJobTypesExcludedFromDefaultQueue;
167 return array_diff( $this->getQueueTypes(), $wgJobTypesExcludedFromDefaultQueue );
171 * @return Array List of job types that have non-empty queues
173 public function getQueuesWithJobs() {
175 foreach ( $this->getQueueTypes() as $type ) {
176 if ( !$this->get( $type )->isEmpty() ) {