Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
89.66% covered (warning)
89.66%
26 / 29
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
JobQueueGroupFactory
89.66% covered (warning)
89.66%
26 / 29
50.00% covered (danger)
50.00%
1 / 2
7.05
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
86.36% covered (warning)
86.36%
19 / 22
0.00% covered (danger)
0.00%
0 / 1
6.09
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 LogicException;
24use MediaWiki\Config\ServiceOptions;
25use MediaWiki\MainConfigNames;
26use MediaWiki\WikiMap\WikiMap;
27use Wikimedia\ObjectCache\WANObjectCache;
28use Wikimedia\Rdbms\ReadOnlyMode;
29use Wikimedia\Stats\IBufferingStatsdDataFactory;
30use Wikimedia\UUID\GlobalIdGenerator;
31
32/**
33 * Factory for JobQueueGroup objects.
34 *
35 * @since 1.37
36 * @ingroup JobQueue
37 */
38class JobQueueGroupFactory {
39    /**
40     * @internal For use by ServiceWiring
41     */
42    public const CONSTRUCTOR_OPTIONS = [
43        MainConfigNames::JobClasses,
44        MainConfigNames::JobTypeConf,
45        MainConfigNames::JobTypesExcludedFromDefaultQueue,
46        MainConfigNames::LocalDatabases,
47    ];
48
49    /** @var JobQueueGroup[] */
50    private $instances;
51
52    /** @var ServiceOptions */
53    private $options;
54
55    /** @var ReadOnlyMode */
56    private $readOnlyMode;
57
58    /** @var IBufferingStatsdDataFactory */
59    private $statsdDataFactory;
60
61    /** @var WANObjectCache */
62    private $wanCache;
63
64    /** @var GlobalIdGenerator */
65    private $globalIdGenerator;
66
67    /**
68     * @param ServiceOptions $options
69     * @param ReadOnlyMode $readOnlyMode
70     * @param IBufferingStatsdDataFactory $statsdDataFactory
71     * @param WANObjectCache $wanCache
72     * @param GlobalIdGenerator $globalIdGenerator
73     */
74    public function __construct(
75        ServiceOptions $options,
76        ReadOnlyMode $readOnlyMode,
77        IBufferingStatsdDataFactory $statsdDataFactory,
78        WANObjectCache $wanCache,
79        GlobalIdGenerator $globalIdGenerator
80    ) {
81        $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
82        $this->instances = [];
83        $this->options = $options;
84        $this->readOnlyMode = $readOnlyMode;
85        $this->statsdDataFactory = $statsdDataFactory;
86        $this->wanCache = $wanCache;
87        $this->globalIdGenerator = $globalIdGenerator;
88    }
89
90    /**
91     * @since 1.37
92     *
93     * @param false|string $domain Wiki domain ID. False uses the current wiki domain ID
94     * @return JobQueueGroup
95     */
96    public function makeJobQueueGroup( $domain = false ): JobQueueGroup {
97        if ( $domain === false ) {
98            $domain = WikiMap::getCurrentWikiDbDomain()->getId();
99        }
100
101        // Make sure jobs are not getting pushed to bogus wikis. This can confuse
102        // the job runner system into spawning endless RPC requests that fail (T171371).
103        $isCurrentWiki = WikiMap::isCurrentWikiDbDomain( $domain );
104        if ( !$isCurrentWiki ) {
105            $wikiId = WikiMap::getWikiIdFromDbDomain( $domain );
106            if ( !in_array( $wikiId, $this->options->get( MainConfigNames::LocalDatabases ) ) ) {
107                // Do not enqueue job that cannot be run (T171371)
108                throw new LogicException( "Domain '{$domain}' is not recognized." );
109            }
110        }
111
112        if ( !isset( $this->instances[$domain] ) ) {
113            $localJobClasses = $isCurrentWiki
114                ? $this->options->get( MainConfigNames::JobClasses )
115                : null;
116
117            $this->instances[$domain] = new JobQueueGroup(
118                $domain,
119                $this->readOnlyMode,
120                $localJobClasses,
121                $this->options->get( MainConfigNames::JobTypeConf ),
122                $this->options->get( MainConfigNames::JobTypesExcludedFromDefaultQueue ),
123                $this->statsdDataFactory,
124                $this->wanCache,
125                $this->globalIdGenerator
126            );
127        }
128
129        return $this->instances[$domain];
130    }
131}