aboutsummaryrefslogtreecommitdiffstats
path: root/includes/jobqueue/JobQueueGroupFactory.php
blob: 1ff4edf3ca48a1ea9317f0afcd5de24617a76ae2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 */

namespace MediaWiki\JobQueue;

use LogicException;
use MediaWiki\Config\ServiceOptions;
use MediaWiki\MainConfigNames;
use MediaWiki\WikiMap\WikiMap;
use Wikimedia\ObjectCache\WANObjectCache;
use Wikimedia\Rdbms\ReadOnlyMode;
use Wikimedia\Stats\StatsFactory;
use Wikimedia\UUID\GlobalIdGenerator;

/**
 * Factory for JobQueueGroup objects.
 *
 * @since 1.37
 * @ingroup JobQueue
 */
class JobQueueGroupFactory {
	/**
	 * @internal For use by ServiceWiring
	 */
	public const CONSTRUCTOR_OPTIONS = [
		MainConfigNames::JobClasses,
		MainConfigNames::JobTypeConf,
		MainConfigNames::JobTypesExcludedFromDefaultQueue,
		MainConfigNames::LocalDatabases,
	];

	/** @var JobQueueGroup[] */
	private $instances;

	/** @var ServiceOptions */
	private $options;

	/** @var ReadOnlyMode */
	private $readOnlyMode;

	/** @var StatsFactory */
	private $statsFactory;

	/** @var WANObjectCache */
	private $wanCache;

	/** @var GlobalIdGenerator */
	private $globalIdGenerator;

	/**
	 * @param ServiceOptions $options
	 * @param ReadOnlyMode $readOnlyMode
	 * @param StatsFactory $statsFactory
	 * @param WANObjectCache $wanCache
	 * @param GlobalIdGenerator $globalIdGenerator
	 */
	public function __construct(
		ServiceOptions $options,
		ReadOnlyMode $readOnlyMode,
		StatsFactory $statsFactory,
		WANObjectCache $wanCache,
		GlobalIdGenerator $globalIdGenerator
	) {
		$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
		$this->instances = [];
		$this->options = $options;
		$this->readOnlyMode = $readOnlyMode;
		$this->statsFactory = $statsFactory;
		$this->wanCache = $wanCache;
		$this->globalIdGenerator = $globalIdGenerator;
	}

	/**
	 * @since 1.37
	 *
	 * @param false|string $domain Wiki domain ID. False uses the current wiki domain ID
	 * @return JobQueueGroup
	 */
	public function makeJobQueueGroup( $domain = false ): JobQueueGroup {
		if ( $domain === false ) {
			$domain = WikiMap::getCurrentWikiDbDomain()->getId();
		}

		// Make sure jobs are not getting pushed to bogus wikis. This can confuse
		// the job runner system into spawning endless RPC requests that fail (T171371).
		$isCurrentWiki = WikiMap::isCurrentWikiDbDomain( $domain );
		if ( !$isCurrentWiki ) {
			$wikiId = WikiMap::getWikiIdFromDbDomain( $domain );
			if ( !in_array( $wikiId, $this->options->get( MainConfigNames::LocalDatabases ) ) ) {
				// Do not enqueue job that cannot be run (T171371)
				throw new LogicException( "Domain '{$domain}' is not recognized." );
			}
		}

		if ( !isset( $this->instances[$domain] ) ) {
			$localJobClasses = $isCurrentWiki
				? $this->options->get( MainConfigNames::JobClasses )
				: null;

			$this->instances[$domain] = new JobQueueGroup(
				$domain,
				$this->readOnlyMode,
				$localJobClasses,
				$this->options->get( MainConfigNames::JobTypeConf ),
				$this->options->get( MainConfigNames::JobTypesExcludedFromDefaultQueue ),
				$this->statsFactory,
				$this->wanCache,
				$this->globalIdGenerator
			);
		}

		return $this->instances[$domain];
	}
}