Merge "Http::getProxy() method to get proxy configuration"
[lhc/web/wiklou.git] / includes / jobqueue / jobs / EnqueueJob.php
1 <?php
2 /**
3 * Router job that takes jobs and enqueues them.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup JobQueue
22 */
23
24 /**
25 * Router job that takes jobs and enqueues them to their proper queues
26 *
27 * This can be used for several things:
28 * - a) Making multi-job enqueues more robust by atomically enqueueing
29 * a single job that pushes the actual jobs (with retry logic)
30 * - b) Masking the latency of pushing jobs to different queues/wikis
31 * - c) Low-latency enqueues to push jobs from warm to hot datacenters
32 *
33 * @ingroup JobQueue
34 * @since 1.25
35 */
36 final class EnqueueJob extends Job {
37 /**
38 * Callers should use the factory methods instead
39 *
40 * @param Title $title
41 * @param array $params Job parameters
42 */
43 function __construct( Title $title, array $params ) {
44 parent::__construct( 'enqueue', $title, $params );
45 }
46
47 /**
48 * @param JobSpecification|JobSpecification[] $jobs
49 * @return EnqueueJob
50 */
51 public static function newFromLocalJobs( $jobs ) {
52 $jobs = is_array( $jobs ) ? $jobs : [ $jobs ];
53
54 return self::newFromJobsByWiki( [ wfWikiID() => $jobs ] );
55 }
56
57 /**
58 * @param array $jobsByWiki Map of (wiki => JobSpecification list)
59 * @return EnqueueJob
60 */
61 public static function newFromJobsByWiki( array $jobsByWiki ) {
62 $deduplicate = true;
63
64 $jobMapsByWiki = [];
65 foreach ( $jobsByWiki as $wiki => $jobs ) {
66 $jobMapsByWiki[$wiki] = [];
67 foreach ( $jobs as $job ) {
68 if ( $job instanceof JobSpecification ) {
69 $jobMapsByWiki[$wiki][] = $job->toSerializableArray();
70 } else {
71 throw new InvalidArgumentException( "Jobs must be of type JobSpecification." );
72 }
73 $deduplicate = $deduplicate && $job->ignoreDuplicates();
74 }
75 }
76
77 $eJob = new self(
78 Title::makeTitle( NS_SPECIAL, 'Badtitle/' . __CLASS__ ),
79 [ 'jobsByWiki' => $jobMapsByWiki ]
80 );
81 // If *all* jobs to be pushed are to be de-duplicated (a common case), then
82 // de-duplicate this whole job itself to avoid build up in high traffic cases
83 $eJob->removeDuplicates = $deduplicate;
84
85 return $eJob;
86 }
87
88 public function run() {
89 foreach ( $this->params['jobsByWiki'] as $wiki => $jobMaps ) {
90 $jobSpecs = [];
91 foreach ( $jobMaps as $jobMap ) {
92 $jobSpecs[] = JobSpecification::newFromArray( $jobMap );
93 }
94 JobQueueGroup::singleton( $wiki )->push( $jobSpecs );
95 }
96
97 return true;
98 }
99 }