copyJobQueue.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * Copy all jobs from one job queue system to another.
  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 Maintenance
  22. */
  23. require_once __DIR__ . '/Maintenance.php';
  24. /**
  25. * Copy all jobs from one job queue system to another.
  26. * This uses an ad-hoc $wgJobQueueMigrationConfig setting,
  27. * which is a map of queue system names to JobQueue::factory() parameters.
  28. * The parameters should not have wiki or type settings and thus partial.
  29. *
  30. * @ingroup Maintenance
  31. */
  32. class CopyJobQueue extends Maintenance {
  33. public function __construct() {
  34. parent::__construct();
  35. $this->addDescription( 'Copy jobs from one queue system to another.' );
  36. $this->addOption( 'src', 'Key to $wgJobQueueMigrationConfig for source', true, true );
  37. $this->addOption( 'dst', 'Key to $wgJobQueueMigrationConfig for destination', true, true );
  38. $this->addOption( 'type', 'Types of jobs to copy (use "all" for all)', true, true );
  39. $this->setBatchSize( 500 );
  40. }
  41. public function execute() {
  42. global $wgJobQueueMigrationConfig;
  43. $srcKey = $this->getOption( 'src' );
  44. $dstKey = $this->getOption( 'dst' );
  45. if ( !isset( $wgJobQueueMigrationConfig[$srcKey] ) ) {
  46. $this->fatalError( "\$wgJobQueueMigrationConfig not set for '$srcKey'." );
  47. } elseif ( !isset( $wgJobQueueMigrationConfig[$dstKey] ) ) {
  48. $this->fatalError( "\$wgJobQueueMigrationConfig not set for '$dstKey'." );
  49. }
  50. $types = ( $this->getOption( 'type' ) === 'all' )
  51. ? JobQueueGroup::singleton()->getQueueTypes()
  52. : [ $this->getOption( 'type' ) ];
  53. foreach ( $types as $type ) {
  54. $baseConfig = [ 'type' => $type, 'wiki' => wfWikiID() ];
  55. $src = JobQueue::factory( $baseConfig + $wgJobQueueMigrationConfig[$srcKey] );
  56. $dst = JobQueue::factory( $baseConfig + $wgJobQueueMigrationConfig[$dstKey] );
  57. list( $total, $totalOK ) = $this->copyJobs( $src, $dst, $src->getAllQueuedJobs() );
  58. $this->output( "Copied $totalOK/$total queued $type jobs.\n" );
  59. list( $total, $totalOK ) = $this->copyJobs( $src, $dst, $src->getAllDelayedJobs() );
  60. $this->output( "Copied $totalOK/$total delayed $type jobs.\n" );
  61. }
  62. }
  63. protected function copyJobs( JobQueue $src, JobQueue $dst, $jobs ) {
  64. $total = 0;
  65. $totalOK = 0;
  66. $batch = [];
  67. foreach ( $jobs as $job ) {
  68. ++$total;
  69. $batch[] = $job;
  70. if ( count( $batch ) >= $this->getBatchSize() ) {
  71. $dst->push( $batch );
  72. $totalOK += count( $batch );
  73. $batch = [];
  74. $dst->waitForBackups();
  75. }
  76. }
  77. if ( count( $batch ) ) {
  78. $dst->push( $batch );
  79. $totalOK += count( $batch );
  80. $dst->waitForBackups();
  81. }
  82. return [ $total, $totalOK ];
  83. }
  84. }
  85. $maintClass = CopyJobQueue::class;
  86. require_once RUN_MAINTENANCE_IF_MAIN;