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.
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.
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
21 namespace MediaWiki\JobQueue
;
25 use MediaWiki\Config\ServiceOptions
;
26 use MediaWiki\MainConfigNames
;
27 use MediaWiki\WikiMap\WikiMap
;
28 use Wikimedia\ObjectCache\WANObjectCache
;
29 use Wikimedia\Rdbms\ReadOnlyMode
;
30 use Wikimedia\Stats\IBufferingStatsdDataFactory
;
31 use Wikimedia\UUID\GlobalIdGenerator
;
34 * Factory for JobQueueGroup objects.
39 class JobQueueGroupFactory
{
41 * @internal For use by ServiceWiring
43 public const CONSTRUCTOR_OPTIONS
= [
44 MainConfigNames
::JobClasses
,
45 MainConfigNames
::JobTypeConf
,
46 MainConfigNames
::JobTypesExcludedFromDefaultQueue
,
47 MainConfigNames
::LocalDatabases
,
50 /** @var JobQueueGroup[] */
53 /** @var ServiceOptions */
56 /** @var ReadOnlyMode */
57 private $readOnlyMode;
59 /** @var IBufferingStatsdDataFactory */
60 private $statsdDataFactory;
62 /** @var WANObjectCache */
65 /** @var GlobalIdGenerator */
66 private $globalIdGenerator;
69 * @param ServiceOptions $options
70 * @param ReadOnlyMode $readOnlyMode
71 * @param IBufferingStatsdDataFactory $statsdDataFactory
72 * @param WANObjectCache $wanCache
73 * @param GlobalIdGenerator $globalIdGenerator
75 public function __construct(
76 ServiceOptions
$options,
77 ReadOnlyMode
$readOnlyMode,
78 IBufferingStatsdDataFactory
$statsdDataFactory,
79 WANObjectCache
$wanCache,
80 GlobalIdGenerator
$globalIdGenerator
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;
94 * @param false|string $domain Wiki domain ID. False uses the current wiki domain ID
95 * @return JobQueueGroup
97 public function makeJobQueueGroup( $domain = false ): JobQueueGroup
{
98 if ( $domain === false ) {
99 $domain = WikiMap
::getCurrentWikiDbDomain()->getId();
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 $isCurrentWiki = WikiMap
::isCurrentWikiDbDomain( $domain );
105 if ( !$isCurrentWiki ) {
106 $wikiId = WikiMap
::getWikiIdFromDbDomain( $domain );
107 if ( !in_array( $wikiId, $this->options
->get( MainConfigNames
::LocalDatabases
) ) ) {
108 // Do not enqueue job that cannot be run (T171371)
109 throw new LogicException( "Domain '{$domain}' is not recognized." );
113 if ( !isset( $this->instances
[$domain] ) ) {
114 $localJobClasses = $isCurrentWiki
115 ?
$this->options
->get( MainConfigNames
::JobClasses
)
118 $this->instances
[$domain] = new JobQueueGroup(
122 $this->options
->get( MainConfigNames
::JobTypeConf
),
123 $this->options
->get( MainConfigNames
::JobTypesExcludedFromDefaultQueue
),
124 $this->statsdDataFactory
,
126 $this->globalIdGenerator
130 return $this->instances
[$domain];