MediaWiki  1.34.0
EnqueueJob.php
Go to the documentation of this file.
1 <?php
35 final class EnqueueJob extends Job implements GenericParameterJob {
41  public function __construct( array $params ) {
42  parent::__construct( 'enqueue', $params );
43  }
44 
49  public static function newFromLocalJobs( $jobs ) {
50  $jobs = is_array( $jobs ) ? $jobs : [ $jobs ];
51 
53  WikiMap::getCurrentWikiDbDomain()->getId() => $jobs
54  ] );
55  }
56 
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 
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 }
WikiMap\getCurrentWikiDbDomain
static getCurrentWikiDbDomain()
Definition: WikiMap.php:292
EnqueueJob\__construct
__construct(array $params)
Callers should use the factory methods instead.
Definition: EnqueueJob.php:41
JobSpecification\newFromArray
static newFromArray(array $map)
Definition: JobSpecification.php:171
EnqueueJob\run
run()
Run the job.
Definition: EnqueueJob.php:94
EnqueueJob
Router job that takes jobs and enqueues them to their proper queues.
Definition: EnqueueJob.php:35
Job\$params
array $params
Array of job parameters.
Definition: Job.php:35
Job
Class to both describe a background job and handle jobs.
Definition: Job.php:30
EnqueueJob\newFromJobsByWiki
static newFromJobsByWiki(array $jobsByWiki)
Definition: EnqueueJob.php:90
EnqueueJob\newFromJobsByDomain
static newFromJobsByDomain(array $jobsByDomain)
Definition: EnqueueJob.php:61
JobQueueGroup\singleton
static singleton( $domain=false)
Definition: JobQueueGroup.php:70
$job
if(count( $args)< 1) $job
Definition: recompressTracked.php:50
JobSpecification
Job queue task description base code.
Definition: JobSpecification.php:39
GenericParameterJob
Interface for generic jobs only uses the parameters field and are JSON serializable.
Definition: GenericParameterJob.php:29
EnqueueJob\newFromLocalJobs
static newFromLocalJobs( $jobs)
Definition: EnqueueJob.php:49