Merge "Improve docs for Title::getInternalURL/getCanonicalURL"
[lhc/web/wiklou.git] / includes / jobqueue / jobs / EnqueueJob.php
index 5ffb01b..72923ce 100644 (file)
 /**
  * Router job that takes jobs and enqueues them to their proper queues
  *
- * This can be used for several things:
- *   - a) Making multi-job enqueues more robust by atomically enqueueing
- *        a single job that pushes the actual jobs (with retry logic)
- *   - b) Masking the latency of pushing jobs to different queues/wikis
- *   - c) Low-latency enqueues to push jobs from warm to hot datacenters
+ * This can be used for getting sets of multiple jobs or sets of jobs intended for multiple
+ * queues to be inserted more robustly. This is a single job that, upon running, enqueues the
+ * wrapped jobs. If some of those fail to enqueue then the EnqueueJob will be retried. Due to
+ * the possibility of duplicate enqueues, the wrapped jobs should be idempotent.
  *
  * @ingroup JobQueue
  * @since 1.25
@@ -51,22 +50,24 @@ final class EnqueueJob extends Job {
        public static function newFromLocalJobs( $jobs ) {
                $jobs = is_array( $jobs ) ? $jobs : [ $jobs ];
 
-               return self::newFromJobsByWiki( [ wfWikiID() => $jobs ] );
+               return self::newFromJobsByDomain( [
+                       WikiMap::getCurrentWikiDbDomain()->getId() => $jobs
+               ] );
        }
 
        /**
-        * @param array $jobsByWiki Map of (wiki => JobSpecification list)
+        * @param array $jobsByDomain Map of (wiki => JobSpecification list)
         * @return EnqueueJob
         */
-       public static function newFromJobsByWiki( array $jobsByWiki ) {
+       public static function newFromJobsByDomain( array $jobsByDomain ) {
                $deduplicate = true;
 
-               $jobMapsByWiki = [];
-               foreach ( $jobsByWiki as $wiki => $jobs ) {
-                       $jobMapsByWiki[$wiki] = [];
+               $jobMapsByDomain = [];
+               foreach ( $jobsByDomain as $domain => $jobs ) {
+                       $jobMapsByDomain[$domain] = [];
                        foreach ( $jobs as $job ) {
                                if ( $job instanceof JobSpecification ) {
-                                       $jobMapsByWiki[$wiki][] = $job->toSerializableArray();
+                                       $jobMapsByDomain[$domain][] = $job->toSerializableArray();
                                } else {
                                        throw new InvalidArgumentException( "Jobs must be of type JobSpecification." );
                                }
@@ -76,7 +77,7 @@ final class EnqueueJob extends Job {
 
                $eJob = new self(
                        Title::makeTitle( NS_SPECIAL, 'Badtitle/' . __CLASS__ ),
-                       [ 'jobsByWiki' => $jobMapsByWiki ]
+                       [ 'jobsByDomain' => $jobMapsByDomain ]
                );
                // If *all* jobs to be pushed are to be de-duplicated (a common case), then
                // de-duplicate this whole job itself to avoid build up in high traffic cases
@@ -85,13 +86,24 @@ final class EnqueueJob extends Job {
                return $eJob;
        }
 
+       /**
+        * @param array $jobsByWiki
+        * @return EnqueueJob
+        * @deprecated Since 1.33; use newFromJobsByDomain()
+        */
+       public static function newFromJobsByWiki( array $jobsByWiki ) {
+               return self::newFromJobsByDomain( $jobsByWiki );
+       }
+
        public function run() {
-               foreach ( $this->params['jobsByWiki'] as $wiki => $jobMaps ) {
+               $jobsByDomain = $this->params['jobsByDomain'] ?? $this->params['jobsByWiki']; // b/c
+
+               foreach ( $jobsByDomain as $domain => $jobMaps ) {
                        $jobSpecs = [];
                        foreach ( $jobMaps as $jobMap ) {
                                $jobSpecs[] = JobSpecification::newFromArray( $jobMap );
                        }
-                       JobQueueGroup::singleton( $wiki )->push( $jobSpecs );
+                       JobQueueGroup::singleton( $domain )->push( $jobSpecs );
                }
 
                return true;