MediaWiki  master
EnqueueJob.php
Go to the documentation of this file.
1 <?php
25 
37 final class EnqueueJob extends Job implements GenericParameterJob {
43  public function __construct( array $params ) {
44  parent::__construct( 'enqueue', $params );
45  }
46 
51  public static function newFromLocalJobs( $jobs ) {
52  $jobs = is_array( $jobs ) ? $jobs : [ $jobs ];
53 
55  WikiMap::getCurrentWikiDbDomain()->getId() => $jobs
56  ] );
57  }
58 
63  public static function newFromJobsByDomain( array $jobsByDomain ) {
64  $deduplicate = true;
65 
66  $jobMapsByDomain = [];
67  foreach ( $jobsByDomain as $domain => $jobs ) {
68  $jobMapsByDomain[$domain] = [];
69  foreach ( $jobs as $job ) {
70  if ( $job instanceof JobSpecification ) {
71  $jobMapsByDomain[$domain][] = $job->toSerializableArray();
72  } else {
73  throw new InvalidArgumentException( "Jobs must be of type JobSpecification." );
74  }
75  $deduplicate = $deduplicate && $job->ignoreDuplicates();
76  }
77  }
78 
79  $eJob = new self( [ 'jobsByDomain' => $jobMapsByDomain ] );
80  // If *all* jobs to be pushed are to be de-duplicated (a common case), then
81  // de-duplicate this whole job itself to avoid build up in high traffic cases
82  $eJob->removeDuplicates = $deduplicate;
83 
84  return $eJob;
85  }
86 
92  public static function newFromJobsByWiki( array $jobsByWiki ) {
93  return self::newFromJobsByDomain( $jobsByWiki );
94  }
95 
96  public function run() {
97  $jobsByDomain = $this->params['jobsByDomain'] ?? $this->params['jobsByWiki']; // b/c
98 
99  $jobQueueGroupFactory = MediaWikiServices::getInstance()->getJobQueueGroupFactory();
100  foreach ( $jobsByDomain as $domain => $jobMaps ) {
101  $jobSpecs = [];
102  foreach ( $jobMaps as $jobMap ) {
103  $jobSpecs[] = JobSpecification::newFromArray( $jobMap );
104  }
105  $jobQueueGroupFactory->makeJobQueueGroup( $domain )->push( $jobSpecs );
106  }
107 
108  return true;
109  }
110 }
Router job that takes jobs and enqueues them to their proper queues.
Definition: EnqueueJob.php:37
static newFromJobsByWiki(array $jobsByWiki)
Definition: EnqueueJob.php:92
run()
Run the job.
Definition: EnqueueJob.php:96
static newFromLocalJobs( $jobs)
Definition: EnqueueJob.php:51
__construct(array $params)
Callers should use the factory methods instead.
Definition: EnqueueJob.php:43
static newFromJobsByDomain(array $jobsByDomain)
Definition: EnqueueJob.php:63
Job queue task description base code.
static newFromArray(array $map)
Class to both describe a background job and handle jobs.
Definition: Job.php:39
array $params
Array of job parameters.
Definition: Job.php:44
Service locator for MediaWiki core services.
static getCurrentWikiDbDomain()
Definition: WikiMap.php:293
Interface for generic jobs only uses the parameters field and are JSON serializable.
if(count( $args)< 1) $job