| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
use MediaWiki\WikiMap\WikiMap; |
| 25 |
|
| 26 |
|
| 27 |
require_once __DIR__ . '/Maintenance.php'; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
class CopyJobQueue extends Maintenance { |
| 39 |
public function __construct() { |
| 40 |
parent::__construct(); |
| 41 |
$this->addDescription( 'Copy jobs from one queue system to another.' ); |
| 42 |
$this->addOption( 'src', 'Key to $wgJobQueueMigrationConfig for source', true, true ); |
| 43 |
$this->addOption( 'dst', 'Key to $wgJobQueueMigrationConfig for destination', true, true ); |
| 44 |
$this->addOption( 'type', 'Types of jobs to copy (use "all" for all)', true, true ); |
| 45 |
$this->setBatchSize( 500 ); |
| 46 |
} |
| 47 |
|
| 48 |
public function execute() { |
| 49 |
global $wgJobQueueMigrationConfig; |
| 50 |
|
| 51 |
$srcKey = $this->getOption( 'src' ); |
| 52 |
$dstKey = $this->getOption( 'dst' ); |
| 53 |
|
| 54 |
if ( !isset( $wgJobQueueMigrationConfig[$srcKey] ) ) { |
| 55 |
$this->fatalError( "\$wgJobQueueMigrationConfig not set for '$srcKey'." ); |
| 56 |
} elseif ( !isset( $wgJobQueueMigrationConfig[$dstKey] ) ) { |
| 57 |
$this->fatalError( "\$wgJobQueueMigrationConfig not set for '$dstKey'." ); |
| 58 |
} |
| 59 |
|
| 60 |
$types = ( $this->getOption( 'type' ) === 'all' ) |
| 61 |
? $this->getServiceContainer()->getJobQueueGroup()->getQueueTypes() |
| 62 |
: [ $this->getOption( 'type' ) ]; |
| 63 |
|
| 64 |
$dbDomain = WikiMap::getCurrentWikiDbDomain()->getId(); |
| 65 |
foreach ( $types as $type ) { |
| 66 |
$baseConfig = [ 'type' => $type, 'domain' => $dbDomain ]; |
| 67 |
$src = JobQueue::factory( $baseConfig + $wgJobQueueMigrationConfig[$srcKey] ); |
| 68 |
$dst = JobQueue::factory( $baseConfig + $wgJobQueueMigrationConfig[$dstKey] ); |
| 69 |
|
| 70 |
[ $total, $totalOK ] = $this->copyJobs( $src, $dst, $src->getAllQueuedJobs() ); |
| 71 |
$this->output( "Copied $totalOK/$total queued $type jobs.\n" ); |
| 72 |
|
| 73 |
[ $total, $totalOK ] = $this->copyJobs( $src, $dst, $src->getAllDelayedJobs() ); |
| 74 |
$this->output( "Copied $totalOK/$total delayed $type jobs.\n" ); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
protected function copyJobs( JobQueue $src, JobQueue $dst, $jobs ) { |
| 79 |
$total = 0; |
| 80 |
$totalOK = 0; |
| 81 |
$batch = []; |
| 82 |
foreach ( $jobs as $job ) { |
| 83 |
++$total; |
| 84 |
$batch[] = $job; |
| 85 |
if ( count( $batch ) >= $this->getBatchSize() ) { |
| 86 |
$dst->push( $batch ); |
| 87 |
$totalOK += count( $batch ); |
| 88 |
$batch = []; |
| 89 |
$dst->waitForBackups(); |
| 90 |
} |
| 91 |
} |
| 92 |
if ( count( $batch ) ) { |
| 93 |
$dst->push( $batch ); |
| 94 |
$totalOK += count( $batch ); |
| 95 |
$dst->waitForBackups(); |
| 96 |
} |
| 97 |
|
| 98 |
return [ $total, $totalOK ]; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
$maintClass = CopyJobQueue::class; |
| 104 |
require_once RUN_MAINTENANCE_IF_MAIN; |
| 105 |
|