EnqueueJob.php 3.3 KB

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