ParamValidator: Flag as unstable for 1.34
[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 getting sets of multiple jobs or sets of jobs intended for multiple
28 * queues to be inserted more robustly. This is a single job that, upon running, enqueues the
29 * wrapped jobs. If some of those fail to enqueue then the EnqueueJob will be retried. Due to
30 * the possibility of duplicate enqueues, the wrapped jobs should be idempotent.
31 *
32 * @ingroup JobQueue
33 * @since 1.25
34 */
35 final class EnqueueJob extends Job implements GenericParameterJob {
36 /**
37 * Callers should use the factory methods instead
38 *
39 * @param array $params Job parameters
40 */
41 public function __construct( array $params ) {
42 parent::__construct( 'enqueue', $params );
43 }
44
45 /**
46 * @param JobSpecification|JobSpecification[] $jobs
47 * @return EnqueueJob
48 */
49 public static function newFromLocalJobs( $jobs ) {
50 $jobs = is_array( $jobs ) ? $jobs : [ $jobs ];
51
52 return self::newFromJobsByDomain( [
53 WikiMap::getCurrentWikiDbDomain()->getId() => $jobs
54 ] );
55 }
56
57 /**
58 * @param array $jobsByDomain Map of (wiki => JobSpecification list)
59 * @return EnqueueJob
60 */
61 public static function newFromJobsByDomain( array $jobsByDomain ) {
62 $deduplicate = true;
63
64 $jobMapsByDomain = [];
65 foreach ( $jobsByDomain as $domain => $jobs ) {
66 $jobMapsByDomain[$domain] = [];
67 foreach ( $jobs as $job ) {
68 if ( $job instanceof JobSpecification ) {
69 $jobMapsByDomain[$domain][] = $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( [ 'jobsByDomain' => $jobMapsByDomain ] );
78 // If *all* jobs to be pushed are to be de-duplicated (a common case), then
79 // de-duplicate this whole job itself to avoid build up in high traffic cases
80 $eJob->removeDuplicates = $deduplicate;
81
82 return $eJob;
83 }
84
85 /**
86 * @param array $jobsByWiki
87 * @return EnqueueJob
88 * @deprecated Since 1.33; use newFromJobsByDomain()
89 */
90 public static function newFromJobsByWiki( array $jobsByWiki ) {
91 return self::newFromJobsByDomain( $jobsByWiki );
92 }
93
94 public function run() {
95 $jobsByDomain = $this->params['jobsByDomain'] ?? $this->params['jobsByWiki']; // b/c
96
97 foreach ( $jobsByDomain as $domain => $jobMaps ) {
98 $jobSpecs = [];
99 foreach ( $jobMaps as $jobMap ) {
100 $jobSpecs[] = JobSpecification::newFromArray( $jobMap );
101 }
102 JobQueueGroup::singleton( $domain )->push( $jobSpecs );
103 }
104
105 return true;
106 }
107 }