Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.43% covered (success)
96.43%
27 / 28
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
JobQueueGroupFactory
96.43% covered (success)
96.43%
27 / 28
50.00% covered (danger)
50.00%
1 / 2
7
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 makeJobQueueGroup
95.24% covered (success)
95.24%
20 / 21
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21namespace MediaWiki\JobQueue;
22
23use IBufferingStatsdDataFactory;
24use JobQueueGroup;
25use LogicException;
26use MediaWiki\Config\ServiceOptions;
27use MediaWiki\MainConfigNames;
28use MediaWiki\WikiMap\WikiMap;
29use WANObjectCache;
30use Wikimedia\Rdbms\ReadOnlyMode;
31use Wikimedia\UUID\GlobalIdGenerator;
32
33/**
34 * Factory for JobQueueGroup objects.
35 *
36 * @since 1.37
37 * @ingroup JobQueue
38 */
39class JobQueueGroupFactory {
40    /**
41     * @internal For use by ServiceWiring
42     */
43    public const CONSTRUCTOR_OPTIONS = [
44        MainConfigNames::JobClasses,
45        MainConfigNames::JobTypeConf,
46        MainConfigNames::JobTypesExcludedFromDefaultQueue,
47        MainConfigNames::LocalDatabases,
48    ];
49
50    /** @var JobQueueGroup[] */
51    private $instances;
52
53    /** @var ServiceOptions */
54    private $options;
55
56    /** @var ReadOnlyMode */
57    private $readOnlyMode;
58
59    /** @var IBufferingStatsdDataFactory */
60    private $statsdDataFactory;
61
62    /** @var WANObjectCache */
63    private $wanCache;
64
65    /** @var GlobalIdGenerator */
66    private $globalIdGenerator;
67
68    /**
69     * @param ServiceOptions $options
70     * @param ReadOnlyMode $readOnlyMode
71     * @param IBufferingStatsdDataFactory $statsdDataFactory
72     * @param WANObjectCache $wanCache
73     * @param GlobalIdGenerator $globalIdGenerator
74     */
75    public function __construct(
76        ServiceOptions $options,
77        ReadOnlyMode $readOnlyMode,
78        IBufferingStatsdDataFactory $statsdDataFactory,
79        WANObjectCache $wanCache,
80        GlobalIdGenerator $globalIdGenerator
81    ) {
82        $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
83        $this->instances = [];
84        $this->options = $options;
85        $this->readOnlyMode = $readOnlyMode;
86        $this->statsdDataFactory = $statsdDataFactory;
87        $this->wanCache = $wanCache;
88        $this->globalIdGenerator = $globalIdGenerator;
89    }
90
91    /**
92     * @since 1.37
93     *
94     * @param false|string $domain Wiki domain ID. False uses the current wiki domain ID
95     * @return JobQueueGroup
96     */
97    public function makeJobQueueGroup( $domain = false ): JobQueueGroup {
98        if ( $domain === false ) {
99            $domain = WikiMap::getCurrentWikiDbDomain()->getId();
100        }
101
102        // Make sure jobs are not getting pushed to bogus wikis. This can confuse
103        // the job runner system into spawning endless RPC requests that fail (T171371).
104        $wikiId = WikiMap::getWikiIdFromDbDomain( $domain );
105        if (
106            !WikiMap::isCurrentWikiDbDomain( $domain ) &&
107            !in_array( $wikiId, $this->options->get( MainConfigNames::LocalDatabases ) )
108        ) {
109            // Do not enqueue job that cannot be run (T171371)
110            throw new LogicException( "Domain '{$domain}' is not recognized." );
111        }
112
113        $localJobClasses = WikiMap::isCurrentWikiDbDomain( $domain )
114            ? $this->options->get( MainConfigNames::JobClasses )
115            : null;
116
117        if ( !isset( $this->instances[$domain] ) ) {
118            $this->instances[$domain] = new JobQueueGroup(
119                $domain,
120                $this->readOnlyMode,
121                $localJobClasses,
122                $this->options->get( MainConfigNames::JobTypeConf ),
123                $this->options->get( MainConfigNames::JobTypesExcludedFromDefaultQueue ),
124                $this->statsdDataFactory,
125                $this->wanCache,
126                $this->globalIdGenerator
127            );
128        }
129
130        return $this->instances[$domain];
131    }
132}